8
0

More than 1 year has passed since last update.

AWS Lambdaのnodejsランタイムでsharpを使うと"Something went wrong installing the \"sharp\" module"でエラーになる場合の解決法

Posted at

結論

$ npm install
$ rm -r node_modules/sharp/
$ npm rebuild --arch=x64 --platform=linux sharp

sharp

画像リサイズやフォーマット変換してくれる。
しかし、そのままnpm installしても AWS Lambda でエラーになる。

エラー内容

Lambdaのエラーメッセージ

Something went wrong installing the "sharp" module

Cannot find module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/node_modules/sharp/lib/sharp.js
- /var/task/node_modules/sharp/lib/constructor.js
- /var/task/node_modules/sharp/lib/index.js
- /var/task/handler.js
- /var/runtime/index.mjs

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install",

やったこと

npm install --platform=linux --arch=x64 sharpしろって書いてる。
でも、そのまま実行してもsharp-linux-x64.nodeは作られなかった。

image.png

npm rebuild sharp --arch=x64 --platform=linux を実行すると、sharp-drawin-x64.nodeが残ってしまい、無駄な容量になった。

image.png

なので、いったん node_modeles の sharp を削除しました。

$ npm install
$ rm -r node_modules/sharp/
$ npm rebuild --arch=x64 --platform=linux sharp

image.png

Serverless Frameworkの場合

設定ファイルのpackagerOptionsscriptsに書いたらできると思います。

  custom: {
    webpack: {
      webpackConfig: './webpack.config.js',
      includeModules: true,
      packagerOptions: {
        scripts: [
          'rm -r node_modules/sharp/',
          'npm rebuild --arch=x64 --platform=linux sharp'
        ],
      },
    },
  },
8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0