在AWS Lambda中无法使用Sharp库的问题求助
解决AWS Amplify部署sharp库的跨平台问题
问题背景
我用npm install --os=linux --cpu=x64 sharp安装了sharp:^0.33.2,提交到Amplify后报错:
"Could not load the "sharp" module using the linux-x64 runtime
Possible solutions:
- Ensure optional dependencies can be installed:
npm install --include=optional sharp
yarn add sharp --ignore-engines- 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"
试过0.33.0、0.33.1版本,错误一致。换成0.32.6版本后,出现新报错:
Packaging lambda function failed with the error Command failed with exit code 1: yarn --no-bin-links --production warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json. error E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp: Command failed.Exit code: 1 Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy) Arguments: Directory: E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp Output: sharp: Using cached D:\nodejs\node_cache\_libvips\libvips-8.14.5-win32-x64.tar.br sharp: Integrity check passed for win32-x64 sharp: Creating E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp\build\Release sharp: Copying DLLs from E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp\vendor\8.14.5\win32-x64\lib to E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp\build\Release 'prebuild-install' is not recognized as an internal or external command, operable program or batch file. E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp>if not defined npm_config_node_gyp (node "D:\nvm\nodejs\node_modules\npm\bin\node-gyp-bin\\..\\..\\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node "" rebuild ) gyp info it worked if it ends with ok gyp info using node-gyp@9.3.1 gyp info using node@18.17.0 | win32 | x64 gyp info find Python using Python version 3.8.4 found at "D:\python\python.exe" gyp http GET https://nodejs.org/download/release/v18.17.0/node-v18.17.0-headers.tar.gz gyp http 200 https://nodejs.org/download/release/v18.17.0/node-v18.17.0-headers.tar.gz gyp http GET https://nodejs.org/download/release/v18.17.0/SHASUMS256.txt gyp http GET https://nodejs.org/download/release/v18.17.0/win-x86/node.lib gyp http GET https://nodejs.org/download/release/v18.17.0/win-arm64/node.lib gyp http GET https://nodejs.org/download/release/v18.17.0/win-x64/node.lib gyp http 200 https://nodejs.org/download/release/v18.17.0/SHASUMS256.txt gyp http 200 https://nodejs.org/download/release/v18.17.0/win-x64/node.lib gyp http 200 https://nodejs.org/download/release/v18.17.0/win-x86/node.lib gyp http 404 https://nodejs.org/download/release/v18.17.0/win-arm64/node.lib gyp info find VS using VS2022 (17.7.34024.191) found at: gyp info find VS "D:\vs2022\IDE" gyp info find VS run with --verbose for detailed information gyp info spawn D:\python\python.exe gyp info spawn args [ gyp info spawn args 'D:\\nvm\\install\\v18.17.0\\node_modules\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py', gyp info spawn args 'binding.gyp', gyp info spawn args '-f', gyp info spawn args 'msvs', gyp info spawn args '-I', gyp info spawn args 'E:\\workplace\\vscode\\Quote-generator-aws-amplify-fullstack\\amplify\\backend\\function\\quoteLambda\\src\\node_modules\\sharp\\build\\config.gypi', gyp info spawn args '-I', gyp info spawn args 'D:\\nvm\\install\\v18.17.0\\node_modules\\npm\\node_modules\\node-gyp\\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\\Users\\Rachel Duan\\AppData\\Local\\node-gyp\\Cache\\18.17.0\\include\\node\\common.gypi', gyp info spawn args '-Dlibrary=shared_library', gyp info spawn args '-Dvisibility=default', gyp info spawn args '-Dnode_root_dir=C:\\Users\\Rachel Duan\\AppData\\Local\\node-gyp\\Cache\\18.17.0', gyp info spawn args '-Dnode_gyp_dir=D:\\nvm\\install\\v18.17.0\\node_modules\\npm\\node_modules\\node-gyp', gyp info spawn args '-Dnode_lib_file=C:\\\\Users\\\\Rachel Duan\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\18.17.0\\\\<(target_arch)\\\\node.lib', gyp info spawn args '-Dmodule_root_dir=E:\\workplace\\vscode\\Quote-generator-aws-amplify-fullstack\\amplify\\backend\\function\\quoteLambda\\src\\node_modules\\sharp', gyp info spawn args '-Dnode_engine=v8', gyp info spawn args '--depth=.', gyp info spawn args '--no-parallel', gyp info spawn args '--generator-output', gyp info spawn args 'E:\\workplace\\vscode\\Quote-generator-aws-amplify-fullstack\\amplify\\backend\\function\\quoteLambda\\src\\node_modules\\sharp\\build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] gyp info spawn D:\vs2022\IDE\MSBuild\Current\Bin\MSBuild.exe gyp info spawn args [ gyp info spawn args 'build/binding.sln', gyp info spawn args '/clp:Verbosity=minimal', gyp info spawn args '/nologo', gyp info spawn args '/p:Configuration=Release;Platform=x64' gyp info spawn args ] D:\vs2022\IDE\MSBuild\Microsoft\VC\v170\Microsoft.Cpp.WindowsSDK.targets(46,5): error MSB8036: The Windows SDK version 10.0.22621.0 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp\node-addon-api\nothing.vcxproj] D:\vs2022\IDE\MSBuild\Microsoft\VC\v170\Microsoft.Cpp.WindowsSDK.targets(46,5): error MSB8036: The Windows SDK version 10.0.22621.0 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp\build\libvips-cpp.vcxproj] gyp ERR! build error gyp ERR! stack Error: `D:\vs2022\IDE\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (D:\nvm\install\v18.17.0\node_modules\npm\node_modules\node-gyp\lib\build.js:203:23) gyp ERR! stack at ChildProcess.emit (node:events:514:28) gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:291:12) gyp ERR! System Windows_NT 10.0.19045 gyp ERR! command "D:\\nvm\\nodejs\\node.exe" "D:\\nvm\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" gyp ERR! cwd E:\workplace\vscode\Quote-generator-aws-amplify-fullstack\amplify\backend\function\quoteLambda\src\node_modules\sharp gyp ERR! node -v v18.17.0 gyp ERR! node-gyp -v v9.3.1 gyp ERR! not ok yarn install v1.22.22 $ npm install --arch=x64 --platform=linux sharp up to date, audited 151 packages in 5s 15 packages are looking for funding run `npm fund` for details found 0 vulnerabilities [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... [4/4] Building fresh packages... info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
已尝试Node.js 18.17.0和21+版本,问题依旧。
解决方案
1. 统一包管理器,避免混合使用
- 删除项目中多余的锁文件,只保留你常用的那一个(比如删
package-lock.json留yarn.lock,或反过来) - 重新安装sharp,指定目标平台:
# 用npm的情况 rm package-lock.json npm install --os=linux --cpu=x64 sharp --include=optional # 用yarn的情况 rm package-lock.json yarn add sharp --ignore-engines --platform=linux --arch=x64
2. 在Amplify构建阶段强制安装对应平台的sharp
修改项目根目录的amplify.yml,在后端构建步骤中添加sharp的平台指定安装命令:
backend: phases: build: commands: - cd amplify/backend/function/quoteLambda/src - npm install --os=linux --cpu=x64 sharp --include=optional - cd ../../../../ - amplify push --yes
3. 配置npm强制跨平台安装
在项目根目录创建.npmrc文件,添加以下内容:
sharp_platform=linux sharp_arch=x64 sharp_libvips_binary_host=https://github.com/lovell/sharp-libvips/releases/download
然后重新执行npm install sharp。
4. 针对0.32.6版本的临时修复(不推荐优先使用)
如果必须用0.32.6版本,需要安装Windows SDK 10.0.22621.0:
- 打开VS2022安装器,选择"修改",添加"Windows SDK 10.0.22621.0"组件
- 安装完成后重新执行依赖安装命令
内容的提问来源于stack exchange,提问作者Rachel Duan
相关产品推荐
相关产品推荐

