node.js Npm 安装失败,“无法在 wd 中运行”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18136746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Npm install failed with "cannot run in wd"
提问by E.H.
I am trying to get my node environment set up on a new Ubuntu 12.04 instance, with Node 0.8.14 already installed, but I ran into problems when I try to run npm install.
So when I try npm install, it says that I need to run it as root or adminisrator:
我正在尝试在新的 Ubuntu 12.04 实例上设置我的节点环境,并且已经安装了 Node 0.8.14,但是当我尝试运行npm install. 所以当我尝试时npm install,它说我需要以 root 或管理员身份运行它:
Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:297:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
But when try to run it as sudo, it says the following:
但是当尝试以 sudo 运行它时,它会显示以下内容:
npm WARN cannot run in wd [email protected] npm install -g coffee-script node-gyp (wd=/home/ubuntu/PackNode)
In my package.json, it contains the following scripts:
在我的 package.json 中,它包含以下脚本:
"scripts": {
"preinstall": "npm install -g coffee-script node-gyp",
"start": "node server.js",
"test": "mocha --require should --compilers coffee:coffee-script --colors"
},
The rest of devdependencies are valid since I have been installing it all right on my own machine (Mac) Does anyone have a clue why this is happening?
其余的 devdependencies 是有效的,因为我一直在我自己的机器(Mac)上安装它,有没有人知道为什么会发生这种情况?
采纳答案by E.H.
OP here, I have learned a lot more about node since I first asked this question. Though Dmitry's answer was very helpful, what ultimately did it for me is to install node with the correct permissions.
OP 在这里,自从我第一次问这个问题以来,我对 node 有了更多的了解。尽管 Dmitry 的回答非常有帮助,但最终对我来说是安装具有正确权限的节点。
I highly recommend not installing node using any package managers, but rather to compile it yourself so that it resides in a local directory with normal permissions.
我强烈建议不要使用任何包管理器安装 node,而是自己编译它,以便它驻留在具有正常权限的本地目录中。
This article provides a very clear step-by-step instruction of how to do so:
本文提供了一个非常清晰的分步说明,说明如何执行此操作:
回答by Dmitry Pashkevich
The documentationsays (also here):
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the
userconfig, which defaults tonobody. Set theunsafe-permflag to run scripts with root privileges.
如果 npm 是用 root 权限调用的,那么它会将 uid 更改为用户帐户或
user配置指定的 uid ,默认为nobody. 设置unsafe-perm标志以使用 root 权限运行脚本。
Your options are:
您的选择是:
Run
npm installwith the--unsafe-permflag:[sudo] npm install --unsafe-permAdd the
unsafe-permflag to yourpackage.json:"config": { "unsafe-perm":true }Don't use the
preinstallscript to install global modules, install them separately and then run the regularnpm installwithout root privileges:sudo npm install -g coffee-script node-gyp npm install
运行
npm install与--unsafe-perm标志:[sudo] npm install --unsafe-perm将
unsafe-perm标志添加到您的package.json:"config": { "unsafe-perm":true }不要使用
preinstall脚本安装全局模块,单独安装它们然后在npm install没有root权限的情况下运行常规:sudo npm install -g coffee-script node-gyp npm install
Related:
相关:
回答by Undistraction
The only thing that worked for me was adding a .npmrcfile containing:
唯一对我有用的是添加一个.npmrc包含以下内容的文件:
unsafe-perm = true
Adding the same config to package.jsondidn't have any effect.
添加相同的配置package.json没有任何效果。
回答by Yassine Khachlek
I have experienced the same problem when trying to publish my nodejs app in a private server running CentOs using root user. The same error is fired by "postinstall": "./node_modules/bower/bin/bower install" in my package.json file so the only solution that was working for me is to use both options to avoid the error:
我在尝试使用 root 用户在运行 CentOs 的私有服务器中发布我的 nodejs 应用程序时遇到了同样的问题。我的 package.json 文件中的 "postinstall": "./node_modules/bower/bin/bower install" 触发了相同的错误,因此对我有用的唯一解决方案是使用这两个选项来避免错误:
1: use --allow-root option for bower install command
1:使用 --allow-root 选项用于 bower 安装命令
"postinstall": "./node_modules/bower/bin/bower --allow-root install"
2: use --unsafe-perm option for npm install command
2:在 npm install 命令中使用 --unsafe-perm 选项
npm install --unsafe-perm
回答by Daniel O'Leary
I fixed this by changing the ownership of /usr/localand ~/Users/user-namelike so:
我通过改变所有权来解决这个问题/usr/local,~/Users/user-name就像这样:
sudo chown -R my_name /usr/local
sudo chown -R my_name /usr/local
This allowed me to do everything without sudo
这让我可以在没有 sudo

