首页 > 解决方案 > 全局 NPM 包安装的简单 CircleCI 2.0 配置失败

问题描述

我有一个运行良好的 Dockerfile:

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

但与上述 Dockerfile 相同的 CircleCI config.yml 文件不起作用:

{
  "version": 2,
  "jobs": {
    "build": {
      "docker": [
        {
          "image": "circleci/node:10"
        }
      ],
      "steps": [
        {
          "run": "npm set unsafe-perm true"
        },
        {
          "run": "npm install -g --loglevel=warn '@oresoftware/r2g@0.0.132'"
        }
      ]
    }
  }
}

我使用上面的 config.yml 文件在 CircleCI 上收到以下错误:

#!/bin/bash -eo pipefail
npm install -g --loglevel=warn @oresoftware/r2g
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/circleci/.npm/_logs/2018-06-18T18_26_53_651Z-debug.log
Exited with code 243

CircleCI 2.0 应该使用 Docker,所以我不确定为什么会发生此权限错误。

标签: node.jsdockernpmcirclecicircleci-2.0

解决方案


tldr - 使用以下前缀:

npm install --prefix=$HOME/.local --global serverless
  • 替换serverless为您自己的全局包要求。

背景:

  • 经过一些实验,上面似乎是我发现的最干净的方法。
  • CircleCI 的当前circleci/node:lts-buster图像在路径上有以下内容:

    /home/circleci/.local/bin:/home/circleci/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

  • /home/circleci/bin由于写入权限被阻止,我无法写入。

  • 我能够写信给/home/circleci/.local/bin
  • --prefix=$HOME/.local选项添加到npm install命令意味着然后将全局包安装到/home/circleci/.local/bin
  • 安装后,就我而言,该命令serverless是可执行的。

推荐阅读