NPM在docker容器中升级后中断

时间:2023-02-05 23:39:43

I need to create a docker container with node v6.10.3, but with the latest npm (currently v5.4.1) to use new npm features for local packages.

我需要使用node v6.10.3创建一个docker容器,但是使用最新的npm(目前是v5.4.1)来为本地包使用新的npm特性。

Such installation runs without any problem on my Mac, but when I try to create a docker image with such an installation, after updating npm, the npm tool gets broken and throws a bunch of errors about missing packages.

这样的安装在我的Mac上运行没有任何问题,但是当我试图用这样的安装创建docker映像时,在更新了npm之后,npm工具就会被破坏,并抛出大量关于丢失包的错误。

Here is the example of the Dockerfile, with which I can reproduce this issue (note that my real Dockerfile is more complex):

下面是Dockerfile的示例,我可以用它来重现这个问题(注意,我的实际Dockerfile更加复杂):

FROM ubuntu:xenial

RUN apt-get update
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN npm i -g npm
RUN npm i -g lerna

When the build process gets to the line RUN npm i -g lerna it throws a bunch of errors like:

当构建过程到达直线运行npm i -g lerna时,它会抛出一系列错误,比如:

Error: Cannot find module 'process-nextick-args'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

Any other npm script would result in the same errors. Reinstalling all the packages on which npm depends, does not seem as a solution for me.

任何其他npm脚本都会导致相同的错误。重新安装npm所依赖的所有包对我来说似乎不是一个解决方案。

I have also tried to install node inside the container using nvm, but I got the same errors.

我还尝试使用nvm在容器内安装节点,但我也有同样的错误。

My docker version:

我的码头工人版本:

Docker version 17.06.2-ce, build cec0b72

What's wrong with this Dockerfile and what am I missing?

这个Dockerfile有什么问题,我漏掉了什么?

1 个解决方案

#1


2  

I have found a workaround for this issue, using yarn.

我用纱线找到了解决这个问题的方法。

It looks weird, but it works:

它看起来很奇怪,但却很管用:

FROM ubuntu:xenial

RUN apt-get update
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN npm i -g yarn
RUN npm uninstall npm -g
RUN yarn global add npm
RUN npm i -g lerna

Still, it would be great if somebody can explain why the original solution did not work, and/or help find a better way to fix it.

尽管如此,如果有人能解释为什么原始的解决方案不起作用,并/或帮助找到更好的解决方法,那就太好了。

#1


2  

I have found a workaround for this issue, using yarn.

我用纱线找到了解决这个问题的方法。

It looks weird, but it works:

它看起来很奇怪,但却很管用:

FROM ubuntu:xenial

RUN apt-get update
RUN apt-get install -y curl

RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN npm i -g yarn
RUN npm uninstall npm -g
RUN yarn global add npm
RUN npm i -g lerna

Still, it would be great if somebody can explain why the original solution did not work, and/or help find a better way to fix it.

尽管如此,如果有人能解释为什么原始的解决方案不起作用,并/或帮助找到更好的解决方法,那就太好了。