npm v6.4.1 not running `prepare` inside docker

♀尐吖头ヾ 提交于 2019-12-08 02:18:29

问题


I am trying to install a package inside of a docker container but the prepare script is not being run.

Here is a Dockerfile which replicates the issue:

FROM ubuntu:18.04
# Replace shell with bash so we can source files to use npm
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get update && apt-get upgrade -y
RUN apt-get install wget git -y
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
RUN source ~/.nvm/nvm.sh; nvm install v10.12.0; nvm use v10.12.0
RUN mkdir -p /usr/app/
WORKDIR /usr/app/
RUN source ~/.nvm/nvm.sh; npm install jcollard/d3-ng2-service#jcollard/add-dist

The above installs nvm and switches to using node v10.12.0 and npm v6.4.1 before attempting to install a package directly from github which should fail.

$ docker build -t npm-hack:latest .
$ docker run --rm -it npm-hack:latest /bin/bash -c 'source ~/.nvm/nvm.sh; npm --version'
6.4.1
$ docker run --rm -it npm-hack:latest /bin/bash -c 'source ~/.nvm/nvm.sh; node --version'
v10.12.0

The final line of the docker command should fail.

The package.json located on that repository branch is here: https://github.com/jcollard/d3-ng2-service/blob/jcollard/add-dist/package.json#L15

You'll see "prepare": "BREAK BREAK BREAK",

When I run this outside of the docker container, this results in the expected error:

$ npm install jcollard/d3-ng2-service#jcollard/add-dist
npm ERR! prepareGitDep 1>
npm ERR! prepareGitDep > d3-ng2-service@2.3.0 prepare /home/jcollard/.npm/_cacache/tmp/git-clone-77d32f21
npm ERR! prepareGitDep > BREAK BREAK BREAK
npm ERR! prepareGitDep
npm ERR! prepareGitDep
npm ERR! prepareGitDep 2> npm WARN install Usage of the `--dev` option is deprecated. Use `--only=dev` instead.
npm ERR! prepareGitDep sh: 1: BREAK: not found
npm ERR! prepareGitDep npm ERR! file sh
npm ERR! prepareGitDep npm ERR! code ELIFECYCLE
npm ERR! prepareGitDep npm ERR! errno ENOENT
npm ERR! prepareGitDep npm ERR! syscall spawn
npm ERR! prepareGitDep npm ERR! d3-ng2-service@2.3.0 prepare: `BREAK BREAK BREAK`
npm ERR! prepareGitDep npm ERR! spawn ENOENT
npm ERR! prepareGitDep npm ERR!
npm ERR! prepareGitDep npm ERR! Failed at the d3-ng2-service@2.3.0 prepare script.
npm ERR! prepareGitDep npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I'm assuming there is some configuration locally that is informing npm to run prepare but I can't seem to find it. Any help would be appreciated.

Thanks!


回答1:


That was an interesting rabbit hole. It is this bug: https://github.com/npm/npm/issues/17346. Prepare doesn't run as root. You could run the container as non-root, but I just used the fix in the issue.

I changed your last line to this

RUN source ~/.nvm/nvm.sh; npm config set unsafe-perm true; npm install jcollard/d3-ng2-service#jcollard/add-dist

Now it fails as expected.



来源:https://stackoverflow.com/questions/52766731/npm-v6-4-1-not-running-prepare-inside-docker

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