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

我是研究僧i 提交于 2019-12-03 05:49:07

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 "npm@5.0.0" 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

> hive-updater@1.0.0 setup /usr/hive-updater
> npm install --quiet && npm run build

added 102 packages in 5.156s

> hive-updater@1.0.0 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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!