Simple CircleCI 2.0 configuration fails for global NPM package installation

☆樱花仙子☆ 提交于 2019-12-06 11:13:56

As said the Dockerfile from the top is not fully identical with the one in the CircleCI-config. In the Dockerfile the base image is node which by default runs under the root user.

The circleci/node image on the on the other hand drops to the unprivileged circleci user. So a 100% identical Dockerfile based on the node image would look like this:

FROM node:10
RUN useradd -m circleci
USER circleci
RUN npm set unsafe-perm true
RUN npm install -g '@oresoftware/r2g@0.0.132'

And with this Dockerfile the same error appears as in CircleCI.

One solution would be to use sudo, the problem with this is that you would have to use sudo on every command which makes use of the node package you installed (since with sudo it would actually be installed in the /root directory which is not accessible with the circleci user).

I think the better option would be to install the package in the circleci home directory.

{
  "version": 2,
  "jobs": {
    "build": {
      "docker": [
        {
          "image": "circleci/node:10"
        }
      ],
      "steps": [
        {
          "run": "npm set prefix=/home/circleci/npm && echo 'export PATH=$HOME/circleci/npm/bin:$PATH' >> /home/circleci/.bashrc"
        },
        {
          "run": "npm install -g --loglevel=warn '@oresoftware/r2g@0.0.132'"
        }
      ]
    }
  }
}

This way you don't have to sudo everytime you want to use the package.

On CircleCI you'd need to use sudo. The default user is circleci which has passwordless sudo access.

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