I was about to start some node.js development at home, but stumbled on some weird behavior when installing npm packages globally.
我准备在家里开始一些node.js开发,但在全局安装npm软件包时偶然发现了一些奇怪的行为。
I use WebStorm as my IDE, and want to use ECMAScript6 features, which means I have to transpile the sources, which WebStorm does by using Babel.
我使用WebStorm作为我的IDE,并希望使用ECMAScript6功能,这意味着我必须通过使用Babel来转换WebStorm所做的来源。
So I tried to install Babel globally:
所以我尝试在全球范围内安装Babel:
$ sudo npm install -g babel-cli
What happens is that the files are all accessible fine from the root user, and the babel-cli
directory and all files and directories in it look fine as long as I look as root
. The problem comes when I try to look at the package, or use it, as another user:
发生的事情是,root用户可以很好地访问这些文件,只要我看作root用户,babel-cli目录及其中的所有文件和目录看起来都很好。当我尝试查看包或使用它作为另一个用户时出现问题:
$ ls -l /usr/lib/node_modules/babel-cli/
ls: cannot access '/usr/lib/node_modules/babel-cli/lib': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/index.js': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/scripts': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/node_modules': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/package.json': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/README.md': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/bin': Permission denied
total 0
d????????? ? ? ? ? ? bin/
d????????? ? ? ? ? ? lib/
d????????? ? ? ? ? ? node_modules/
d????????? ? ? ? ? ? scripts/
-????????? ? ? ? ? ? index.js
-????????? ? ? ? ? ? package.json
-????????? ? ? ? ? ? README.md
It doesn't matter which package I install, they all become like this.
我安装哪个包无关紧要,它们都变成这样。
I am using node.js v6.3.0 and npm 3.10.3, on Ubuntu 16.04.
我在Ubuntu 16.04上使用node.js v6.3.0和npm 3.10.3。
I installed node.js freshly as instructed here. If I try to update npm
then the same happens to the global npm directory and npm becomes unavailable except to root
.
我按照这里的说明新安装了node.js。如果我尝试更新npm,那么全局npm目录也会发生同样的情况,并且除了root之外,npm变得不可用。
When I list the global node_modules
as a user I see
当我将全局node_modules列为我看到的用户时
$ ll /usr/lib/node_modules/
total 8,0K
drwxr--r-- 6 nobody root 4,0K jul 15 23:50 babel-cli/
drwxr-xr-x 10 root root 4,0K jul 15 23:21 npm/
Besides the directory being owned by nobody
I see nothing special.
除了没有人拥有的目录,我没有看到什么特别的。
There are of course no errors when I install the package.
安装软件包时当然没有错误。
Is it something wrong with npm and/or node? Is it something wrong with Ubuntu? Or how I installed node? What could be the reason for something like that to happen?
npm和/或节点有问题吗? Ubuntu有问题吗?或者我如何安装节点?这样的事情发生的原因是什么?
Installing locally works fine, and for the babel-cli
package it is an acceptable workaround to install it as a local development package. I would still like to know what happens when installing packages globally, and why.
在本地安装工作正常,对于babel-cli软件包,将其安装为本地开发包是一种可接受的解决方法。我仍然想知道在全球安装软件包时会发生什么,以及为什么。
Problem answered here. Problem was unrelated to npm
and node
.
这里回答的问题。问题与npm和node无关。
2 个解决方案
#1
5
If you want to install a package globally, just use a command without sudo
like:
如果要全局安装包,只需使用不带sudo的命令:
$ npm install -g <package>
If you're getting EACCES or permissions errors, the use of sudo
should be avoided but you should instead fix your permissions so npm can run without sudo
.
如果您收到EACCES或权限错误,应该避免使用sudo,但您应该修改您的权限,以便npm可以在没有sudo的情况下运行。
These errors are raised if you don't have the permissions to write to the folder that npm uses to store global packages. To fix that, you can start by finding what's the path to npm's default directory:
如果您没有写入npm用于存储全局包的文件夹的权限,则会引发这些错误。要解决这个问题,您可以先找到npm默认目录的路径:
$ npm config get prefix
On most systems, it'll be /usr/local
and you'll be able to fix permissions on this folder. If the folder is /usr
or /usr/lib
, you shouldn't change permissions on this directory since it'll cause some problems and in that case where you do not want to change permissions of the default directory, you can configure npm to use a different directory.
在大多数系统上,它将是/ usr / local,您将能够修复此文件夹的权限。如果文件夹是/ usr或/ usr / lib,则不应更改此目录的权限,因为它会导致一些问题,在这种情况下,您不希望更改默认目录的权限,您可以将npm配置为使用不同的目录。
If you can and want to change permissions on the default folder, you can use:
如果您可以并且想要更改默认文件夹的权限,则可以使用:
$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
It'll change the owner of npm's directories to the name of the current user.
它会将npm目录的所有者更改为当前用户的名称。
If you want to change npm's default directory, you can start by creating this new folder:
如果要更改npm的默认目录,可以先创建这个新文件夹:
$ mkdir ~/.npm-packages
Then, you configure npm to use this new folder:
然后,配置npm以使用此新文件夹:
$ npm config set prefix '~/.npm-packages'
You'll also need to add this new folder to your PATH
(for example using ~/.profile):
您还需要将此新文件夹添加到PATH中(例如使用〜/ .profile):
export PATH=~/.npm-packages/bin:$PATH
At this point, you can update your system variables with source ~/.profile
and test to install a package globally without using sudo
.
此时,您可以使用source~ / .profile更新系统变量并测试以在不使用sudo的情况下全局安装软件包。
You should be able to install the package without getting any permissions errors since npm will use the ~/.npm-packages
folder.
您应该能够安装该软件包而不会出现任何权限错误,因为npm将使用〜/ .npm-packages文件夹。
You can find more informations regarding this matter on the npm documentation.
您可以在npm文档中找到有关此问题的更多信息。
#2
0
If you change the user than you must do one of the following because when you installed npm at that time you are installed as Root user but after tat you change the user and that gives you permission error because this indicates that you do not have permission to write to the directories that npm uses to store global packages and commands.you can fix this via:-
如果您更改用户而不是必须执行下列操作之一,因为当您安装npm时,您将以root用户身份安装,但是在用户之后您更改了用户并且这会给您权限错误,因为这表明您没有权限写入npm用于存储全局包和命令的目录。您可以通过以下方式解决此问题: -
Use a package manager that takes care of this for you.
使用包管理器为您解决此问题。
If you're doing a fresh install of node on Mac OS you can avoid this problem altogether by using the Homebrew package manager. Homebrew sets things up out of the box with the correct permissions.
如果您在Mac OS上进行全新的节点安装,则可以使用Homebrew包管理器完全避免此问题。 Homebrew使用正确的权限开箱即用。
brew install node
OR
Change npm's default directory to another directory
将npm的默认目录更改为另一个目录
There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.
有时您不希望更改npm使用的默认目录(即/ usr)的所有权,因为这可能会导致一些问题,例如,如果您与其他用户共享系统。
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
相反,您可以配置npm以完全使用不同的目录。在我们的例子中,这将是我们的主文件夹中的隐藏目录。
Make a directory for global installations:
为全局安装创建一个目录:
mkdir ~/.npm-global
Configure npm to use the new directory path:
配置npm以使用新的目录路径:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
打开或创建一个〜/ .profile文件并添加以下行:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
回到命令行,更新系统变量:
source ~/.profile
Test: Download a package globally without using sudo.
测试:在不使用sudo的情况下全局下载软件包。
npm install -g babel-cli
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
您也可以使用相应的ENV变量代替步骤2-4(例如,如果您不想修改〜/ .profile):
NPM_CONFIG_PREFIX=~/.npm-global
#1
5
If you want to install a package globally, just use a command without sudo
like:
如果要全局安装包,只需使用不带sudo的命令:
$ npm install -g <package>
If you're getting EACCES or permissions errors, the use of sudo
should be avoided but you should instead fix your permissions so npm can run without sudo
.
如果您收到EACCES或权限错误,应该避免使用sudo,但您应该修改您的权限,以便npm可以在没有sudo的情况下运行。
These errors are raised if you don't have the permissions to write to the folder that npm uses to store global packages. To fix that, you can start by finding what's the path to npm's default directory:
如果您没有写入npm用于存储全局包的文件夹的权限,则会引发这些错误。要解决这个问题,您可以先找到npm默认目录的路径:
$ npm config get prefix
On most systems, it'll be /usr/local
and you'll be able to fix permissions on this folder. If the folder is /usr
or /usr/lib
, you shouldn't change permissions on this directory since it'll cause some problems and in that case where you do not want to change permissions of the default directory, you can configure npm to use a different directory.
在大多数系统上,它将是/ usr / local,您将能够修复此文件夹的权限。如果文件夹是/ usr或/ usr / lib,则不应更改此目录的权限,因为它会导致一些问题,在这种情况下,您不希望更改默认目录的权限,您可以将npm配置为使用不同的目录。
If you can and want to change permissions on the default folder, you can use:
如果您可以并且想要更改默认文件夹的权限,则可以使用:
$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
It'll change the owner of npm's directories to the name of the current user.
它会将npm目录的所有者更改为当前用户的名称。
If you want to change npm's default directory, you can start by creating this new folder:
如果要更改npm的默认目录,可以先创建这个新文件夹:
$ mkdir ~/.npm-packages
Then, you configure npm to use this new folder:
然后,配置npm以使用此新文件夹:
$ npm config set prefix '~/.npm-packages'
You'll also need to add this new folder to your PATH
(for example using ~/.profile):
您还需要将此新文件夹添加到PATH中(例如使用〜/ .profile):
export PATH=~/.npm-packages/bin:$PATH
At this point, you can update your system variables with source ~/.profile
and test to install a package globally without using sudo
.
此时,您可以使用source~ / .profile更新系统变量并测试以在不使用sudo的情况下全局安装软件包。
You should be able to install the package without getting any permissions errors since npm will use the ~/.npm-packages
folder.
您应该能够安装该软件包而不会出现任何权限错误,因为npm将使用〜/ .npm-packages文件夹。
You can find more informations regarding this matter on the npm documentation.
您可以在npm文档中找到有关此问题的更多信息。
#2
0
If you change the user than you must do one of the following because when you installed npm at that time you are installed as Root user but after tat you change the user and that gives you permission error because this indicates that you do not have permission to write to the directories that npm uses to store global packages and commands.you can fix this via:-
如果您更改用户而不是必须执行下列操作之一,因为当您安装npm时,您将以root用户身份安装,但是在用户之后您更改了用户并且这会给您权限错误,因为这表明您没有权限写入npm用于存储全局包和命令的目录。您可以通过以下方式解决此问题: -
Use a package manager that takes care of this for you.
使用包管理器为您解决此问题。
If you're doing a fresh install of node on Mac OS you can avoid this problem altogether by using the Homebrew package manager. Homebrew sets things up out of the box with the correct permissions.
如果您在Mac OS上进行全新的节点安装,则可以使用Homebrew包管理器完全避免此问题。 Homebrew使用正确的权限开箱即用。
brew install node
OR
Change npm's default directory to another directory
将npm的默认目录更改为另一个目录
There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.
有时您不希望更改npm使用的默认目录(即/ usr)的所有权,因为这可能会导致一些问题,例如,如果您与其他用户共享系统。
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
相反,您可以配置npm以完全使用不同的目录。在我们的例子中,这将是我们的主文件夹中的隐藏目录。
Make a directory for global installations:
为全局安装创建一个目录:
mkdir ~/.npm-global
Configure npm to use the new directory path:
配置npm以使用新的目录路径:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
打开或创建一个〜/ .profile文件并添加以下行:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
回到命令行,更新系统变量:
source ~/.profile
Test: Download a package globally without using sudo.
测试:在不使用sudo的情况下全局下载软件包。
npm install -g babel-cli
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
您也可以使用相应的ENV变量代替步骤2-4(例如,如果您不想修改〜/ .profile):
NPM_CONFIG_PREFIX=~/.npm-global