LoginSignup
70
73

More than 3 years have passed since last update.

npm installでエラーが出た時にとりあえずやってみること

Last updated at Posted at 2020-05-16

はじめに

npm installでしょっちゅうエラーにぶち当たるので備忘的に残しておきます。
npmってなんぞ?って方はこちらの記事とか参考になるかと思います。

とりあえず確認しとく項目

1. nodeとnpmのバージョンを見直す
2. node_modules フォルダとpackage-lock.json(場合によっては node-gyp も)を削除
3. npm install 実行するディレクトリを変える

各詳細は後述します。今回はdockerとlaravelを使用して環境構築しています。

発生したエラー

docker-compose up -dでコンテナ作成したあと、
npm installコマンド実行しようとしたら出たエラー。

$ docker run -it --rm -v $(pwd):/app -w /app node npm install
... 
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack   at ChildProcess.onExit (/app/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack   at ChildProcess.emit (events.js:315:20)
gyp ERR! stack   at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
gyp ERR! System Linux 4.19.76-linuxkit
gyp ERR! command "/usr/local/bin/node" "/app/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd /app/node_modules/laravel-elixir/node_modules/node-sass
gyp ERR! node -v v14.2.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
Build failed with error code: 1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@3.13.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the node-sass@3.13.1 postinstall 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/2020-05-11T03_23_06_075Z-debug.log

1. node と npm のバージョンを見直す

いろいろ調べたり試した結果、
nodeとnpmのバージョン変更でほとんど解消できそうな感じでした。
node v12系、14系は同様のエラーが出るみたいな記事があったので
今回は10系にスイッチしました。

node管理ツールはnodebrewを使用しています。
(nodebrew をインストールされてない場合はこちら。

nodeのバージョン変更

 #インストールできるnode.jsのバージョンを確認
$ nodebrew ls-remote 

 #バージョンを指定してインストール
$ nodebrew install-binary v10.16.0

 #使用バージョンの切替
$ nodebrew use v10.16.0

 #使用nodeバージョン確認
$ node -v
v10.16.0

npmのバージョン変更

npmのバージョンはnodeのバージョンに対応して自動的に切り替わってる?みたいなので
手動で変更するときは使用バージョンをインストールしてpackage.jsonを直接編集しました。

 #バージョンを指定してインストール
$ npm install -g npm@6.14.2

 #npmバージョンの確認
$ npm -v
6.14.2

↑バージョン確認して変わってない場合はpackage.jsonを直接編集。

package.json例
{
  "name": "exam",
  "version": "0.9.4",
  "description": "WordPress Theme framework",
  "main": "index.js",
  "scripts": {
    "postinstall": "gulp copy-assets"
  },
  "engines": {
    "npm": "6.9.0"  # ←このバージョンを直接指定する
  },
  ...

もっかいnpm -vしてバージョン変更されてればOK

2. node_modules と package-lock.json を削除

なんかゴミが残ってて邪魔してたりするので、一回きれいにするため
node_modules と package-lock.json を 削除する。
↑2つ削除してもだめそうだったら~/.node-gyp/も削除してみる。
そのあと再度 npm install 実行。

$ rm -rf node_modules
$ rm package-lock.json
$ rm -rf ~/.node-gyp/

$ npm install

3. npm install 実行するディレクトリを変える

windowsの記事だったのでmac使用している人は関係ないかも…?
筆者もmacユーザーですがいろいろ試してる中で
もしかしたらこれが功を奏していたかもしれないので一応記載。

Dockerのコンテナ内でnpm installをコマンド実行した場合、
コンテナにマウントされているディレクトリでnpm installを行うとこのようなエラーになるらしい
とのことだったので、下記の対応をする

 #マウントされてない任意フォルダに移動
$ cd /tmp 

 #元々 npm install しようとしてたディレクトリのpackage.json を移動させる
$ cp /usr/local/hoge/package.json 

 #移動先のフォルダでnpm install(windowsの場合は--no-bin-linksオプションが必要らしい)
$ npm install --no-bin-links
 :
 #node_modulesディレクトリを元々npm installを実行する予定だったディレクトリに丸ごと移動
$ mv ./node_modules /usr/local/hoge/

さいごに

これら一通りやったらなにかしら好転したらいいなと思っている今日この頃。
nodeとnpmの適切バージョンを切り分ける簡単な方法あれば誰か教えてください。。。
エラー解消できないのほんとうざい(´・ω・`)

参考サイト

70
73
2

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
70
73