Gatsby.jsでCannot find module '../build/Release/sharp.node'エラーの解決策

created
updated

Gatsby.jsでCannot find module '../build/Release/sharp.node'のエラーが発生したので、その対応方法をまとめました。

エラー内容

$ npm run develop

> sample_app@1.0.0 develop /app
> export NODE_ENV=development && gatsby develop -H 0.0.0.0

 ERROR

Error in "/app/node_modules/gatsby-plugin-manifest/gatsby-node.js":
Something went wrong installing the "sharp" module

Cannot find module '../build/Release/sharp.node'
Require stack:
- /app/node_modules/sharp/lib/constructor.js
- /app/node_modules/sharp/lib/index.js
- /app/node_modules/gatsby-plugin-manifest/safe-sharp.js
- /app/node_modules/gatsby-plugin-manifest/gatsby-node.js
- /app/node_modules/gatsby/dist/bootstrap/resolve-module-exports.js
- /app/node_modules/gatsby/dist/bootstrap/load-plugins/validate.js
- /app/node_modules/gatsby/dist/bootstrap/load-plugins/load.js
- /app/node_modules/gatsby/dist/bootstrap/load-plugins/index.js
- /app/node_modules/gatsby/dist/bootstrap/load-config-and-plugins.js
- /app/node_modules/gatsby/dist/services/initialize.js
- /app/node_modules/gatsby/dist/services/index.js
- /app/node_modules/gatsby/dist/state-machines/develop/services.js
- /app/node_modules/gatsby/dist/state-machines/develop/index.js
- /app/node_modules/gatsby/dist/commands/develop-process.js
- /app/.cache/tmp-151-kWSQylcbIxhO

- Remove the "node_modules/sharp" directory then run
  "npm install --ignore-scripts=false --verbose sharp" and look for errors
- Consult the installation documentation at https://sharp.pixelplumbing.com/install
- Search for this error at https://github.com/lovell/sharp/issues

Error: Something went wrong installing the "sharp" module
  Cannot find module '../build/Release/sharp.node'
  Require stack:
  - /app/node_modules/sharp/lib/constructor.js
  - /app/node_modules/sharp/lib/index.js
  - /app/node_modules/gatsby-plugin-manifest/safe-sharp.js
  - /app/node_modules/gatsby-plugin-manifest/gatsby-node.js
  - /app/node_modules/gatsby/dist/bootstrap/resolve-module-exports.js
  - /app/node_modules/gatsby/dist/bootstrap/load-plugins/validate.js
  - /app/node_modules/gatsby/dist/bootstrap/load-plugins/load.js
  - /app/node_modules/gatsby/dist/bootstrap/load-plugins/index.js
  - /app/node_modules/gatsby/dist/bootstrap/load-config-and-plugins.js
  - /app/node_modules/gatsby/dist/services/initialize.js
  - /app/node_modules/gatsby/dist/services/index.js
  - /app/node_modules/gatsby/dist/state-machines/develop/services.js
  - /app/node_modules/gatsby/dist/state-machines/develop/index.js
  - /app/node_modules/gatsby/dist/commands/develop-process.js
  - /app/.cache/tmp-151-kWSQylcbIxhO
  - Remove the "node_modules/sharp" directory then run
    "npm install --ignore-scripts=false --verbose sharp" and look for errors
  - Consult the installation documentation at https://sharp.pixelplumbing.com/install
  - Search for this error at https://github.com/lovell/sharp/issues

  - constructor.js:32 Object.<anonymous>
    [app]/[sharp]/lib/constructor.js:32:9

  - v8-compile-cache.js:192 Module._compile
    [app]/[v8-compile-cache]/v8-compile-cache.js:192:30

  - loader.js:1158 Object.Module._extensions..js
    internal/modules/cjs/loader.js:1158:10

  - loader.js:986 Module.load
    internal/modules/cjs/loader.js:986:32

  - loader.js:879 Function.Module._load
    internal/modules/cjs/loader.js:879:14

  - loader.js:1026 Module.require
    internal/modules/cjs/loader.js:1026:19

  - v8-compile-cache.js:159 require
    [app]/[v8-compile-cache]/v8-compile-cache.js:159:20

  - index.js:3 Object.<anonymous>
    [app]/[sharp]/lib/index.js:3:15

  - v8-compile-cache.js:192 Module._compile
    [app]/[v8-compile-cache]/v8-compile-cache.js:192:30

  - loader.js:1158 Object.Module._extensions..js
    internal/modules/cjs/loader.js:1158:10

  - loader.js:986 Module.load
    internal/modules/cjs/loader.js:986:32

  - loader.js:879 Function.Module._load
    internal/modules/cjs/loader.js:879:14

  - loader.js:1026 Module.require
    internal/modules/cjs/loader.js:1026:19

  - v8-compile-cache.js:159 require
    [app]/[v8-compile-cache]/v8-compile-cache.js:159:20

  - safe-sharp.js:132 Object.<anonymous>
    [app]/[gatsby-plugin-manifest]/safe-sharp.js:132:11

  - v8-compile-cache.js:192 Module._compile
    [app]/[v8-compile-cache]/v8-compile-cache.js:192:30


not finished open and validate gatsby-configs, load plugins - 3.731s

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sample_app@1.0.0 develop: `export NODE_ENV=development && gatsby develop -H 0.0.0.0`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the sample_app@1.0.0 develop script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-07-11T07_38_58_873Z-debug.log

解決方法

下記ディレクトリを一度削除し、もう一度起動するとエラーが解消されました。

$ rm -rf ./.cache ./public ./node_modules
$ npm run develop
TOP