LoginSignup
0
3

More than 5 years have passed since last update.

Dockerfileでapt-get install npm後にnpm update -g npmで最新にアップデートするとnpmがエラーになってしまう場合の対処法

Posted at

目的

Dockerfileで以下を行いたい。

  • apt-get install npmnpmをインストール
  • npm install -g npm@latestで最新版までアップデートする

問題点

npm install -g npm@latestで最新版まで上げると依存ファイルが足りないらしく、npmが動作しなくなる。
例として以下のようなエラーが出る。
依存関係のあるものを先にインストールしようとしたが、1つや2つではないようなので断念。

RUN apt-get update && apt-get install -y npm

# nをインストールしてnode.jsのstableをインストールする
RUN npm install -g n && n stable
RUN npm install -g npm@latest
module.js:529
    throw err;
    ^

Error: Cannot find module 'npmlog'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at /usr/local/lib/node_modules/npm/bin/npm-cli.js:22:13
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:92:3)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)

解決策

一旦npm@4.0をインストールした後にnpm@latestをインストール時する。

RUN apt-get update && apt-get install -y npm

# nをインストールしてnode.jsのstableをインストールする
RUN npm install -g n && n stable
RUN npm install -g npm@4.0
RUN npm install -g npm@latest

解決するまで結構悩みました…。
おそらくDockerfileではなく、実OSでも使えるテクニックだと思います。
自分用メモですが同じところでつまづいてる人の助けになるといいですね。

0
3
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
0
3