Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upgrade npm to npm@5 on the latest node docker image?

Tags:

npm

docker

alpine

Locally, I have successfully installed npm@5 via:

$ npm install npm@5 -g
$ npm -v
$ 5.0.0

And locally, I can run the npm setup just fine (it's basically npm i && tsc)

$ npm run setup 
updated 102 packages in 3.499s

Yet now I also have a Dockerfile based upon the node:7.10-alpine image which breaks if I try to install npm@5 there.

My Dockerfile looks like this:

FROM node:7.10-alpine
WORKDIR /usr/hive-updater/
ENV LAST_UPDATED=2016-12-08 NPM_CONFIG_LOGLEVEL=warn TERM=xterm PATH="$PATH:/usr/hive-updater/node_modules/.bin"
RUN npm install npm@5 -g && npm -v
COPY ./ ./
RUN npm run setup
CMD ["node"]

This will fail during npm -v with:

module.js:472
    throw err;
    ^

Error: Cannot find module 'semver'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/utils/unsupported.js:2:14)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

How to get the latest npm on my docker container?

like image 257
k0pernikus Avatar asked May 30 '17 18:05

k0pernikus


1 Answers

I found out that the node's alpine image ships with yarn.

Yarn is Facebook's npm replacement and you can use it to globally install npm@5:

RUN npm -v
RUN yarn global add npm@5
RUN npm -v
COPY ./ ./
RUN npm run setup

(The version calls are superfluous and only to highlight that the upgrade works.)

And now it works:

Step 4/9 : RUN npm -v
 ---> Running in dca435fbec59
4.2.0
 ---> f6635e6c92a3
Removing intermediate container dca435fbec59
Step 5/9 : RUN yarn global add npm@5
 ---> Running in fac7216ccd91
yarn global v0.24.4
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "[email protected]" with binaries:
      - npm
Done in 10.47s.
 ---> b6b2e0f3fc36
Removing intermediate container fac7216ccd91
Step 6/9 : RUN npm -v
 ---> Running in 38a9ee95b9f0
5.0.0
 ---> d1632fc97b7e
Removing intermediate container 38a9ee95b9f0
Step 7/9 : COPY ./ ./
 ---> b9b62f53ca48
Removing intermediate container e9dd065c022f
Step 8/9 : RUN npm run setup
 ---> Running in aec36af706d4

> [email protected] setup /usr/hive-updater
> npm install --quiet && npm run build

added 102 packages in 5.156s

> [email protected] build /usr/hive-updater
> tsc

So if you have npm below version 5 and its upgrade method breaks for you, install yarn to upgrade npm ¯\_(ツ)_/¯


Sidenote:

It might be better to just use yarn instead of npm@5. It still has a strong performance advantage.

Compare these runs, both cached:

yarn install v0.24.5
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.31s.

with npm@5:

npm install
updated 102 packages in 3.069s

I didn't know that yarn was already shipped with the alpine image.

like image 53
k0pernikus Avatar answered Oct 23 '22 16:10

k0pernikus