sh: 1: node: Permission denied

我的博客是采用 hexo 搭建的,想着每次都要手动发布文章太过繁琐。作为程序员,懒是程序员的通病,凡事能程序解决的问题,就不要想手动解决。于是想到采用 Git 的 hooks 进行自动发布,说干就干,买了台服务器这就动手搞起。此处省略了 Git 服务搭建,想了解的请自行百度,这里直奔主题,开始安装 hexo-cli。这是在我的 Vultr VPS 上安装 hexo-cli 时候,发生的 “sh: 1: node: Permission denied” 错误信息。

完整错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
root@jp02:~# npm install -g hexo-cli
/root/.nvm/versions/node/v7.10.0/bin/hexo -> /root/.nvm/versions/node/v7.10.0/lib/node_modules/hexo-cli/bin/hexo

> dtrace-provider@0.8.2 install /root/.nvm/versions/node/v7.10.0/lib/node_modules/hexo-cli/node_modules/dtrace-provider
> node scripts/install.js

sh: 1: node: Permission denied

> hexo-util@0.6.0 postinstall /root/.nvm/versions/node/v7.10.0/lib/node_modules/hexo-cli/node_modules/hexo-util
> npm run build:highlight

sh: 1: npm: Permission denied
/root/.nvm/versions/node/v7.10.0/lib
`-- (empty)

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/hexo-cli/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: dtrace-provider@0.8.2 (node_modules/hexo-cli/node_modules/dtrace-provider):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: dtrace-provider@0.8.2 install: `node scripts/install.js`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: spawn ENOENT
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! hexo-util@0.6.0 postinstall: `npm run build:highlight`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the hexo-util@0.6.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-05-30T09_53_44_036Z-debug.log
root@jp02:~#

可以清楚的看出,是权限不足导致的。但是我用的是 root 用户进行安装,为什么会权限不足呢?然后,去 Google 发现 npm 默认安装用户为 nobody,unsafe-perm 默认为 false,以非 root 用户 安装时会失败。然后执行了如下命令:

1
2
npm config set user 0
npm config set unsafe-perm true

然后,成功安装了 hexo-cli,翻看 npm 文档看到对 user 和 unsafe-perm 的说明:

user:

  • Default: “nobody”
  • Type: String or Number

The UID to set to when running package scripts as root.

unsafe-perm:

  • Default: false if running as root, true otherwise
  • Type: Boolean

Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.