AWS Lambda(Node.js)配置Sharp Layer仍报“Cannot find module 'sharp'”错误
Node.js 22.x AWS Lambda中Sharp Layer加载失败问题
我在Node.js 22.x版本的AWS Lambda函数中尝试使用Sharp库压缩API Gateway上传的图片,已将Sharp作为Lambda Layer添加并在AWS SAM模板中完成引用,但部署调用后出现Sharp模块加载失败的错误。
SAM模板相关配置
Resources: ImageCompressor: Type: AWS::Serverless::Function Properties: Runtime: nodejs22.x Handler: index.handler CodeUri: . Layers: - !Ref Layer1 MemorySize: 128 Timeout: 3 ... Layer1: Type: AWS::Serverless::LayerVersion Properties: ContentUri: ./sharp LayerName: sharp CompatibleRuntimes: - nodejs18.x - nodejs20.x - nodejs22.x
报错信息
2025-05-08T12:16:07.469Z undefined ERROR Uncaught Exception { "errorType": "Error", "errorMessage": "Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install", "stack": [ "Error: Could not load the \"sharp\" module using the linux-x64 runtime", "Possible solutions:", "- Ensure optional dependencies can be installed:", " npm install --include=optional sharp", "- Ensure your package manager supports multi-platform installation:", " See https://sharp.pixelplumbing.com/install#cross-platform", "- Add platform-specific dependencies:", " npm install --os=linux --cpu=x64 sharp", "- Consult the installation documentation:", " See https://sharp.pixelplumbing.com/install", " at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:121:9)", " at Module._compile (node:internal/modules/cjs/loader:1554:14)", " at Object..js (node:internal/modules/cjs/loader:1706:10)", " at Module.load (node:internal/modules/cjs/loader:1289:32)", " at Function._load (node:internal/modules/cjs/loader:1108:12)", " at TracingChannel.traceSync (node:diagnostics_channel:322:14)", " at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)", " at Module.require (node:internal/modules/cjs/loader:1311:12)", " at require (node:internal/modules/helpers:136:16)", " at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:10:1)" ] }
Lambda处理代码简化版
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3'); const sharp = require('sharp'); console.log('sharp Version: ', require('sharp').version); exports.handler = async (event) => { const buffer = Buffer.from(event.fileBase64, 'base64'); const compressed = await sharp(buffer) .jpeg({ quality: 60, mozjpeg: true }) .toBuffer(); // Upload logic follows... };
解决步骤
1. 修正Layer目录结构
Lambda Layer要求模块放在nodejs/node_modules路径下,这样Lambda会将Layer挂载到/opt目录后,Node.js能自动从/opt/nodejs/node_modules加载模块。检查你的./sharp目录结构,确保是:
./sharp/ └── nodejs/ └── node_modules/ └── sharp/
如果结构不符,调整后重新部署Layer。
2. 排除Lambda部署包中的本地Sharp模块
从错误栈路径/var/task/node_modules/sharp/lib/sharp.js可以看出,Lambda正在尝试加载部署包(/var/task)里的Sharp,而不是Layer中的版本。解决方法:
- 在项目根目录创建
.npmignore文件,添加规则:node_modules/sharp - 或者在SAM模板的
ImageCompressor函数属性中添加排除路径:Properties: ... ExcludePaths: - "node_modules/sharp/**"
确保部署时不会将本地的Sharp打包进去。
3. 确认Layer兼容Node.js 22.x
如果你使用的是第三方Sharp Layer,需确认其已针对Node.js 22.x编译。若不确定,可自行构建Layer:
- 在Linux环境(或Docker中)运行:
npm install --os=linux --cpu=x64 sharp - 将生成的
node_modules/sharp放入./sharp/nodejs/node_modules目录 - 重新部署Layer
4. 验证Layer部署状态
登录AWS控制台,进入Lambda Layer页面,查看Layer1的内容,确认包含完整的Sharp模块文件和对应Linux-x64的二进制文件。
内容的提问来源于stack exchange,提问作者Vin
相关产品推荐
相关产品推荐

