[Gatsby.js]Error: Command failed with exit code 1: npm install

Gatsby.jsでスターターを用いて新規プロジェクトを作成する時に、以下のエラーが出たのでこのエラーの原因と解消方法をご紹介します。

$ gatsby new my-gatsby-project https://github.com/gatsbyjs/gatsby-starter-default
info Creating new site from git:
https://github.com/gatsbyjs/gatsby-starter-default.git

Cloning into 'my-gatsby-project'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 34 (delta 0), reused 26 (delta 0), pack-reused 0
success Created starter directory layout
info Installing packages...

npm ERR! code 1
npm ERR! path /Users/my-gatsby-project/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
npm ERR! TOUCH Release/obj.target/libvips-cpp.stamp
npm ERR!   CXX(target) Release/obj.target/sharp/src/common.o
npm ERR! info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.7.4/libvips-8.7.4-darwin-x64.tar.gz
...

 ERROR 

Command failed with exit code 1: npm install



  Error: Command failed with exit code 1: npm install
...

このエラーが出た時の各種バージョンは以下の通りです。

$ node -v
v16.2.0
$ yarn -v
1.22.10
$ gatsby -v
Gatsby CLI version: 3.8.0
目次

解決方法は安定版のnodeにバージョンを下げて新規プロジェクトを立ち上げる

現在のnodeのバージョンは16でしたが、nodeのバージョンを現時点での安定版の14に下げると、Gatsbyの新規プロジェクトを作ることができます。

以下のRelease scheduleで今の安定版を確認してください。

https://github.com/nodejs/Release#nodejs-release-working-group

nodeのバージョンを安定版に下げるにはnを使います。
次の手順でnodeのバージョンを下げることができます。

$ n list
node/12.14.1
node/16.2.0
$ sudo n stable
Password:

  installing : node-v14.17.1
       mkdir : /usr/local/n/versions/node/14.17.1
       fetch : https://nodejs.org/dist/v14.17.1/node-v14.17.1-darwin-x64.tar.gz
   installed : v14.17.1 (with npm 6.14.13)
$ node -v
v12.14.1

すでに安定版のnodeが入っている方はn stableで安定版に切り替えられます。

次にGatsbyの新規プロジェクトを作るコマンドを叩くと新規プロジェクトが作られます。

$ gatsby new my-gatsby-project https://github.com/gatsbyjs/gatsby-starter-default
info Creating new site from git:
https://github.com/gatsbyjs/gatsby-starter-default.git

Cloning into 'my-gatsby-project'...
remote: Enumerating objects: 28, done.
...
Your new Gatsby site has been successfully bootstrapped. Start developing it by
running:

  cd my-gatsby-project
  gatsby develop

原因はGatsby.jsが最新版のnodeに対応していないから

Gatsby.jsは最新版のnodeには対応していません。
公式にも記載されている通り、安定版をインストールすることを推奨しています。

Generally, it’s recommended to use the Node version whose status is Active LTS (Node 14 at time of writing).

一般に、ステータスがアクティブなLTS(執筆時のノード14)のノードバージョンを使用することをお勧めします。

https://www.gatsbyjs.com/docs/upgrading-node-js/#upgrading-from-nodejs-version-10

注意 安定版のnodeでもインストールできいないこともある

Gatsby.jsの公式では安定版のインストールすることを推奨していますが、スターターによっては安定版のnodeでもインストールできないものもあります。

こちらのスターターは安定版のnodeで新規プロジェクトを作れませんでした。

https://www.gatsbyjs.com/starters/app-generator/gatsbyjs-starter-tailwindplay

$ node -v
v14.17.1
$ gatsby new my-gatsby-project https://github.com/app-generator/gatsbyjs-starter-tailwindplay
info Creating new site from git:
https://github.com/app-generator/gatsbyjs-starter-tailwindplay.git

Cloning into 'my-gatsby-project'...
remote: Enumerating objects: 34, done.
...
pm ERR! gyp ERR! cwd /Users/my-gatsby-project/node_modules/sharp
npm ERR! gyp ERR! node -v v14.17.1
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:

 ERROR 

Command failed with exit code 1: npm install



  Error: Command failed with exit code 1: npm install
  
  - error.js:56 makeError
...

このときはnodeのバージョンをもう一つ下げて、node12にします。

次の手順でnodeのバージョンを12にすることができます。

$ n list
node/12.14.1
node/16.2.0
$ n 12.14.1
$ node -v
v12.14.1

node12では正常にインストールすることができました。

$ gatsby new project-name https://github.com/app-generator/gatsbyjs-starter-tailwindplay
info Creating new site from git:
https://github.com/app-generator/gatsbyjs-starter-tailwindplay.git

Cloning into 'project-name'...
remote: Enumerating objects: 34, done.
...
Your new Gatsby site has been successfully bootstrapped. Start developing it by
running:

  cd project-name
  gatsby develop

まとめ

初心者にはいきなりハマりポイントとなってしまいます。

nを使ってnodeのバージョンを柔軟に切り替えながらスターターをインストールできるか試してみるしかありません。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする


The reCAPTCHA verification period has expired. Please reload the page.

目次