TravisCI 在 Node BCrypt 上失败并出现 node-gyp 错误

TravisCI failing on Node BCrypt with node-gyp errors

我正在尝试让我的 Node.js 7 项目在 TravisCI 上运行。一切都在本地工作,但是当 travis 尝试测试项目时,我得到:

gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/travis/.nvm/versions/node/v6.9.1/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:276:23)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Linux 4.8.7-040807-generic
gyp ERR! command "/home/travis/.nvm/versions/node/v6.9.1/bin/node" "/home/travis/.nvm/versions/node/v6.9.1/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/travis/build/actuallymentor/react-node-boilerplate/node_modules/bcrypt
gyp ERR! node -v v6.9.1
gyp ERR! node-gyp -v v3.4.0
gyp ERR! not ok 

并且:

npm ERR! bcrypt@0.8.7 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the bcrypt@0.8.7 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the bcrypt package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild

这导致我的构建甚至在 运行 测试之前就失败了。

这里的问题是 bcrypt 需要使用 c++ 库。 Travis 没有安装这个。为了让它发挥作用,我们需要:

  1. 安装库
  2. 设置一个环境变量表明我们要使用它

此外,node-gyp 喜欢全局安装,因此我们也可以一次性配置它。

在你的 .travis.yml 中输入:

sudo: required
before_install:
  - npm install -g node-gyp
addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    packages:
    - g++-5

此外,请务必在您的 travis 项目设置中设置此环境变量:

CXX=g++-5

请注意,任何高于 4.8 的 g++ 版本都应该可以使用。

这对我有用!!!!

nodejs 4.6.2,travis 上的 bcryp 1.0.1

env:
  - CXX=g++-4.8
addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    packages:
    - g++-4.8