5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Mac]npm install時にエラーが出る(解決法2種)

5
Last updated at Posted at 2019-04-29

node.jsとnpm installしたパッケージのバージョンが不一致で起こるエラーが原因であることが多い印象。

TL;DR

いただきものpackage.jsonの場合
→nodebrewでnodeのバージョンを変えてみる

nodeのバージョンを変えても動かないor自分でpackage.jsonを変えた場合
→package.jsonに記入しているパッケージのバージョンをnodeに合わせる

いただきものpackage.jsonの場合

nodebrewでnodeのバージョンを変えてみる

homebrewが入っていることが前提です。
入っていない場合はこちらを参考にnode.jsをアンインストールの上、
homebrewとnodebrewをインストールしてください。

0.Homebrewとnodebrewのインストール

terminal:Homebrewのインストール
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ brew -v
Homebrew 1.9.1
Homebrew/homebrew-core (git revision 61f95; last commit 2019-01-11)
terminal:nodebrewのインストール
$ brew install nodebrew

$ nodebrew -v
nodebrew 1.0.1
/*以下略*/

1.nodebrewでnodeのバージョンを変更する

いただきものpackage.jsonの場合は、先方にnodeのバージョンをお尋ねください。
仮に、現在のnodeが8系、先方の使用しているnodeが10系だとします。

terminal:インストール済みのnodeのバージョンを確認
$ nodebrew -ls
v8.9.4

current: v8.9.4
terminal:インストール可能なnodeのバージョンを確認
$ nodebrew ls-remote
/*中略*/

v10.0.0   v10.1.0   v10.2.0   v10.2.1   v10.3.0   v10.4.0   v10.4.1   v10.5.0
v10.6.0   v10.7.0   v10.8.0   v10.9.0   v10.10.0  v10.11.0  v10.12.0  v10.13.0
v10.14.0  v10.14.1  v10.14.2  v10.15.0  v10.15.1  v10.15.2  v10.15.3  

/*以下略*/
terminal:nodeの10系をインストールする
$ nodebrew install v10
Fetching: https://nodejs.org/dist/v10.15.3/node-v10.15.3-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

$ nodebrew ls //インストールされているか確認
v8.9.4
v10.15.3

current: v8.9.4
terminal:使用するnodeを10系に切り替える。
$ nodebrew use v10
use v10.15.3

$ nodebrew ls //切り替わっているか確認
v8.9.4
v10.15.3

current: v10.15.3

nodeのバージョンを変えた状態でnpm installを正常終了できれば成功。

nodeのバージョンを変えても動かないor自分でpackage.jsonを変えた場合

package.jsonに記入しているモジュールのバージョンをnodeに合わせる

npm installしたらnoticeやwarningが出る場合。

terminal
$ npm install
・・・
npm notice created a lockfile as package-lock.json. You should commit this file.
added 913 packages from 636 contributors and audited 12116 packages in 38.344s
found 8 vulnerabilities (3 low, 1 moderate, 4 high)
  run `npm audit fix` to fix them, or `npm audit` for details

こちらの記事(参考:npm installしたパッケージの更新確認とアップデート(npm-check-updates))を参考に、
package.json内のパッケージのバージョンを更新します。

0.npm-check-updatesコマンドをグローバルインストールする

terminal
$ npm install -g npm-check-updates

1.対応していないパッケージを調べる

terminal
$ npm outdated
Package            Current  Wanted  Latest  Location
del                  3.0.0   3.0.0   4.0.0  anken-name
gulp-autoprefixer    4.0.0   4.0.0   6.0.0  anken-name
gulp-plumber         1.2.0   1.2.0   1.2.1  anken-name
gulp-sass            3.1.0   3.1.0   4.0.2  anken-name
run-sequence         2.2.0   2.2.0   2.2.1  anken-name

2.npm-check-updatesコマンドを実行する

terminal:コマンド実行
$ ncu -u
Upgrading /ほにゃらら/dev/frontend/gulp/package.json
[====================] 11/11 100%

 browser-sync       ^2.3.1  →  ^2.26.3 
 del                 3.0.0  →    4.0.0 
 gulp-autoprefixer   4.0.0  →    6.0.0 
 gulp-plumber        1.2.0  →    1.2.1 
 gulp-sass            ~3.1  →     ~4.0 
 run-sequence        2.2.0  →    2.2.1 

Run npm install to install new versions.
terminal:package.jsonが更新されたのでupdate
$ npm update

3.outdatedに引っかかるパッケージが消えたか確認する

terminal
$ npm outdated

何も表示されなくなったのでOK。

5
6
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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?