LoginSignup
276
219

【初心者】npm ERR! の対処法

Last updated at Posted at 2019-07-05

npm ERR! と格闘しました。
7つのケースがあったのでまとめておきます。
とりあえず解消したい、という方は参考にしてください。

ケース1

ステップ1

$ npm install -g npm

npm自体のアップデートをする。

ステップ2

$ rm -rf node_modules

既存のモジュールを削除する。

ステップ3

$ npm install

プロジェクトの依存関係を再インストールする。

ケース2

エラーログ

To fix the dependency tree, try following the steps below in the exact order:

  1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
  2. Delete node_modules in your project folder.
  3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json file in your project folder.
  4. Run npm install or yarn, depending on the package manager you use.

In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:

  5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
     This may help because npm has known issues with package hoisting which may get resolved in future versions.

  6. Check if /Users/tagawahirotaka/node_modules/babel-loader is outside your project directory.
     For example, you might have accidentally installed something in your home folder.

  7. Try running npm ls babel-loader in your project folder.
     This will tell you which other package (apart from the expected react-scripts) installed babel-loader.

If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.

P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

1~4を試しても、エラーは解消されない。

ホームフォルダ(/Users/<name>/)にあるnode_modulesをすべて削除して再度試したらエラーが解消された。

ケース3

エラーログ

> @ webpack /Users/tagawahirotaka/Desktop/webpack
> webpack src/index.js public/bundle.js

Hash: 180ac7bf541799553075
Version: webpack 4.35.3
Time: 233ms
Built at: 2019-07-09 10:10:51
 1 asset
Entrypoint main = main.js
[0] multi ./src/index.js public/bundle.js 40 bytes {0} [built]
[1] ./src/index.js + 1 modules 161 bytes {0} [built]
    | ./src/index.js 104 bytes [built]
    | ./src/module.js 57 bytes [built]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in multi ./src/index.js public/bundle.js
Module not found: Error: Can't resolve 'public/bundle.js' in '/Users/tagawahirotaka/Desktop/webpack'
 @ multi ./src/index.js public/bundle.js main[1]
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! @ webpack: `webpack src/index.js public/bundle.js`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the @ webpack 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!     /Users/tagawahirotaka/.npm/_logs/2019

The 'mode' option has not been set, webpack will fallback to 'production' for this value.

v4系以降はmodeを指定しないと、webpack実行時に警告が出るらしいのでwebpack.config.jsmodeを記述した。

module.exports = {
  mode: 'development',
  ...

ケース4

エラーログ

npm ERR! code ELIFECYCLE
npm ERR! errno 1

ステップ1

rm -rf node_modules
インストールされたjsモジュールを全部消す。

ステップ2

rm package-lock.json yarn.lock
インストールされたjsモジュールのバージョン情報を消す。

ステップ3

npm cache clear --force
npmのキャッシュをクリアする。

ステップ4

npm install
再度jsモジュールを全部入れ直して、実行する。

ケース5

エラーログ

npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "backstopjs" under a package
npm ERR! also called "backstopjs". Did you name your project the same
npm ERR! as the dependency you're installing?

Refusing to install package with name "backstopjs" under a package
package.json"name""backstopjs"以外にしないといけない。

{
  "name": "hoge"
  ...
}

ケース6

npm install -g create-react-appを実行すると、checkPermissionsのエラーが出た。
これは、長くなるので別記事にまとめています。
npm install -g create-react-appでcheckPermissions が出た時の対処法

ケース7

npm installによりパッケージをインストールしようとしたら、permission deniedされた。
これも別記事にまとめてます。
npm install で permission denied

まとめ

私の場合はこれでエラーが解消しました。
何か誤りがあればご指摘お願いします。

276
219
1

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
276
219