为什么我无法安装phantomjs(错误:EACCES:权限被拒绝)?

24

之前我在这台机器上安装了phantomjs,但现在它找不到可执行文件了。所以我卸载了它 (npm uninstall phantomjs, npm uninstall phantomjs-prebuild)。

但是重新安装并不能解决问题。请问有人能告诉我为什么会出现这种情况以及如何修复吗?

$ sudo -H npm -g install phantomjs-prebuilt
/usr/bin/phantomjs -> /usr/lib/node_modules/phantomjs-prebuilt/bin/phantomjs

> phantomjs-prebuilt@2.1.15 install /usr/lib/node_modules/phantomjs-prebuilt
> node install.js

Considering PhantomJS found at /usr/bin/phantomjs
Looks like an `npm install -g`
Could not link global install, skipping...
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2
Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Receiving...
  [=======================================-] 98%
Received 22866K total.
Extracting tar contents (via spawned process)
Removing /usr/lib/node_modules/phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1505979849548/phantomjs-2.1.1-linux-x86_64 -> /usr/lib/node_modules/phantomjs-prebuilt/lib/phantom
Phantom installation failed { Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1505979849548/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/phantomjs-prebuilt/lib/phantom'
    at Error (native)
  errno: -13,
  code: 'EACCES',
  syscall: 'link',
  path: '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1505979849548/phantomjs-2.1.1-linux-x86_64',
  dest: '/usr/lib/node_modules/phantomjs-prebuilt/lib/phantom' } Error: EACCES: permission denied, link '/tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1505979849548/phantomjs-2.1.1-linux-x86_64' -> '/usr/lib/node_modules/phantomjs-prebuilt/lib/phantom'
    at Error (native)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! phantomjs-prebuilt@2.1.15 install: `node install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the phantomjs-prebuilt@2.1.15 install 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-09-21T07_44_13_695Z-debug.log

日志显示:

2467 silly install phantomjs-prebuilt@2.1.15
2468 info lifecycle phantomjs-prebuilt@2.1.15~install: phantomjs-prebuilt@2.1.15
2469 verbose lifecycle phantomjs-prebuilt@2.1.15~install: unsafe-perm in lifecycle false
2470 verbose lifecycle phantomjs-prebuilt@2.1.15~install: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/usr/lib/node_modules/phantomjs-prebuilt/node_modules/.bin:/usr/lib/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
2471 verbose lifecycle phantomjs-prebuilt@2.1.15~install: CWD: /usr/lib/node_modules/phantomjs-prebuilt
2472 silly lifecycle phantomjs-prebuilt@2.1.15~install: Args: [ '-c', 'node install.js' ]
2473 silly lifecycle phantomjs-prebuilt@2.1.15~install: Returned: code: 1  signal: null
2474 info lifecycle phantomjs-prebuilt@2.1.15~install: Failed to exec install script
2475 verbose unlock done using /root/.npm/_locks/staging-a072192f34a17023.lock for /usr/lib/node_modules/.staging
2476 verbose stack Error: phantomjs-prebuilt@2.1.15 install: `node install.js`
2476 verbose stack Exit status 1
2476 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:280:16)
2476 verbose stack     at emitTwo (events.js:106:13)
2476 verbose stack     at EventEmitter.emit (events.js:191:7)
2476 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
2476 verbose stack     at emitTwo (events.js:106:13)
2476 verbose stack     at ChildProcess.emit (events.js:191:7)
2476 verbose stack     at maybeClose (internal/child_process.js:891:16)
2476 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
2477 verbose pkgid phantomjs-prebuilt@2.1.15
2478 verbose cwd /tmp
2479 verbose Linux 4.10.0-33-generic
2480 verbose argv "/usr/bin/nodejs" "/usr/bin/npm" "-g" "install" "phantomjs-prebuilt"
2481 verbose node v6.11.2
2482 verbose npm  v5.4.2
2483 error code ELIFECYCLE
2484 error errno 1
2485 error phantomjs-prebuilt@2.1.15 install: `node install.js`
2485 error Exit status 1
2486 error Failed at the phantomjs-prebuilt@2.1.15 install script.
2486 error This is probably not a problem with npm. There is likely additional logging output above.
2487 verbose exit [ 1, true ]

我的系统

  • Ubuntu 16.04
  • npm 5.4.2
5个回答

27

虽然回答有点晚了,但我仍会继续。

我通过以下方法使其工作:

sudo npm install -g phantomjs@2.1.1 --unsafe-perm

在此处检查评论 这里


10

我也遇到了这个错误,找到了两个解决方案:

  1. 通过npm配置更改来修复此问题

    1. 检查您当前的npm-config(在Linux上使用 npm config list --json | grep "user\|unsafe-perm"
    2. 如果必要,更改您的npm-config
      • npm config set user 0 当以root身份运行软件包脚本时,要设置的UID。
      • npm config set unsafe-perm true 设置为true以抑制运行软件包脚本时的UID/GID切换。 如果明确设置为false,则作为非root用户进行安装将失败。
  2. npmjs.com建议针对此错误切换到npm版本管理器,该错误在文章中有描述。


5
想表达的意思是,对我来说设置 npm config set user 0 完全解决了问题。谢谢! - dmanexe
在我的Dockerfile中,这看起来像ENV NPM_CONFIG_USER=0。谢谢! - Noumenon

4

只是一个快速修复:

# Install locally (in my case: ~/node_modules)
$ npm install phantomjs-prebuilt

# Add to path
$ cd ~/bin
~/bin$ ln -s ../node_modules/phantomjs-prebuilt/bin/phantomjs

# Check if it worked
$ phantomjs --version
2.1.1

我基本上做了同样的事情,但我必须确保它真的是全局安装的,所以我只是移动了目录:sudo mv ~/node_modules/phantomjs /usr/lib/node_modules - TeeTrinker

0
在我的情况下,这是一个 node_modules 权限问题。尝试更改你的 node_modules 文件夹权限。
chown ubuntu:ubuntu -R node_modules //for ubuntu only

注意:您可以使用ls -la命令检查您的文件夹权限。

0

我在尝试在Laravel Homestead中运行npm ci时遇到了这个错误。

对我来说,关闭SSL验证解决了这个问题:

npm set strict-ssl false

根据文档,这告诉NPM跳过SSL密钥的验证,在企业环境中可能是必要的,其中代理可能使用自签名的SSL证书来拦截加密流量。
我非常确定在我的情况下没有代理,因为我从未遇到过任何其他SSL问题。这可能只是在虚拟机内运行npm时的一些奇怪副作用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接