Unable to use Sharp in AWS Lambda

87 Views Asked by At

I use npm install --os=linux --cpu=x64 sharp to download sharp:^0.33.2, and after submit to amplify, it is said:

 "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    yarn add sharp --ignore-engines\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"

And I tried 0.33.0 and 0.33.1, got the same error.

I try to use 0.32.6, got the error said:

    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 [email protected]
gyp info using [email protected] | 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.

And I tried using node 18.17.0 and node 21+, the errors are all the same.

1

There are 1 best solutions below

0
Rachel Duan On

I didn't solve these problems finally, I used many different versions including 0.33.0, 0.33.1, 0.33,2, 0.32.6, and 0.32.1, and also tried the recommendation commands and scripts preinstall.

So I finally decided to use Jimp and svg2png packages and succeeded.