node.js 即使目录可写,全局安装时也会出现 NPM 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14786565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:07:47  来源:igfitidea点击:

NPM error when installing globally even when directories are writable

node.jsnpm

提问by Kokizzu

i have this error when try to install coffee-script using this command:

尝试使用以下命令安装咖啡脚本时出现此错误:

npm install -g --verbose coffee-script opal

these are the error message:

这些是错误消息:

npm ERR! Error: EACCES, symlink '../lib/node_modules/coffee-script/bin/coffee'
npm ERR!  { [Error: EACCES, symlink '../lib/node_modules/coffee-script/bin/coffee']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '../lib/node_modules/coffee-script/bin/coffee' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm info postuninstall [email protected]
npm ERR! Error: EACCES, symlink '../lib/node_modules/opal/bin/opal-node'
npm ERR!  { [Error: EACCES, symlink '../lib/node_modules/opal/bin/opal-node']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '../lib/node_modules/opal/bin/opal-node' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

the folder /usr/local/bin and /usr/local/lib/node_modules are owned and writable by current user, and i do not want to run that npm command using root, how to know which folder that the npm tried to make a symlink to?

文件夹 /usr/local/bin 和 /usr/local/lib/node_modules 由当前用户拥有和可写,我不想使用 root 运行该 npm 命令,如何知道 npm 试图创建哪个文件夹符号链接到?

i'm using npm 1.2.9-1chl1~quantal1 and nodejs 0.8.19-1chl1~quantal1

我正在使用 npm 1.2.9-1chl1~quantal1 和 nodejs 0.8.19-1chl1~quantal1

回答by Pascal Belloncle

your node installation uses system directories. Use sudo when using -g

您的节点安装使用系统目录。使用 -g 时使用 sudo

sudo npm install -g --verbose coffee-script opal

回答by Mohsen

You can chownNPM's binto your user name with this one liner to solve this problem:

您可以使用此 liner 将chownNPM 附加bin到您的用户名来解决此问题:

$ chown -R `whoami` `npm -g bin`

回答by Kokizzu

ah, using this command:

啊,使用这个命令:

npm -g bin

it would output something like this:

它会输出如下内容:

/usr/bin # this is the folder nodejs wanted to write..

then you may chmodor chownit so it can be written for installation.

那么你可以chmodchown它所以它可以被编写用于安装。

回答by HeberLZ

I had a similar problem at NPM modules won't install globally without sudo, the issue was that when i installed node i did it with sudo via chris/lea ppa repo.

在没有 sudo情况下无法全局安装 NPM 模块时遇到了类似的问题,问题是当我安装节点时,我通过 chris/lea ppa repo 使用 sudo 进行了安装

My solution was to uninstall node and then install it this way:

我的解决方案是卸载节点,然后以这种方式安装:

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

从 nodejs.org 下载最新的稳定节点源#in my case node-v0.10.20.tar.gz

tar -zxf node-v0.10.20.tar.gz #uncompress sources

cd node-v0.10.20 #enter uncompressed folder

sudo chown $USER -R /usr/local

./configure --prefix=/usr/local && make && make install

tar -zxf node-v0.10.20.tar.gz #解压源

cd node-v0.10.20 #进入解压文件夹

须藤 chown $USER -R /usr/local

./configure --prefix=/usr/local && make && make install

PD: If you don't want to change ownership of the /usr/local folder, you can install it somewhere you already own. The problem of this approach is that you will have to bind the installation folder with the bash command line so that we can use the node command later on

PD:如果您不想更改 /usr/local 文件夹的所有权,您可以将它安装在您已经拥有的地方。这种方法的问题是你必须用bash命令行绑定安装文件夹,以便我们以后可以使用node命令

mkdir ~/opt

./configure --prefix=~/opt && make && make install

echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc #or ~/.profile or ~/.bash_profile or ~/.zshenv depending on the current Operative System

mkdir ~/opt

./configure --prefix=~/opt && make && make install

echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc #or ~/.profile or ~/.bash_profile or ~/.zshenv 取决于当前的操作系统

With either of those approaches, you will be able to do the following without using sudo

使用这两种方法中的任何一种,您都可以在不使用 sudo 的情况下执行以下操作

npm install -g --verbose coffee-script opal

npm install -g --verbose coffee-script opal

回答by jbasko

Had a similar problem. Turns out I had something in project/node_modulesdirectory installed with sudo. In my case it was some of the dependencies AND ALSO .bindirectory. I deleted these bad directories, then ran npm installagain and it succeeded. I did also reinstall global protractor and phantomjs, but not sure if that was required. I am sure it was the bad (i.e. root-owned) .bindirectory causing this.

有类似的问题。原来我在project/node_modules目录中安装了一些东西sudo。在我的情况下,它是一些依赖项和.bin目录。我删除了这些坏目录,然后npm install再次运行,它成功了。我也重新安装了全局量角器和 phantomjs,但不确定是否需要。我确定这是坏的(即 root 拥有的).bin目录导致的。