Fixing npm permissions
修补npm权限
You may receive an EACCES
error when you try to install a package globally. This indicates that you do not have permission to write to the directories that npm uses to store global packages and commands.
当你在全局安装一个包时,你可能会收到一个EACCES错误。这表明,你没有被允许写一个,npm用来存储全局包和命令的路径。
You can fix this problem using one of three options:
1.Change the permission to npm's default directory.
2.Change npm's default directory to another directory.
3.Install node with a package manager that takes care of this for you.
You should back-up your computer before moving forward.
你可以通过如下三个方法中的一个,来修复这个问题:
- 修改npm默认目录的权限。
- 修改npm的默认目录到其他目录。
- 安装带有一个包管理器的node,它会罩着你的。
你应该在进行下一步前,让你的电脑进行备份。
Option 1: Change the permission to npm's default directory
- Find the path to npm's directory:
npm config get prefix
For many systems, this will be /usr/local.
WARNING: If the displayed path is just /usr
, switch to Option 2 or you will mess up your permissions. - Change the owner of npm's directories to the name of the current user (your username!):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin
, and share).
选项一:修改npm默认路径的权限
- 找到npm目录的路径:
npm config get prefix
对于很多系统,路径会是/usr/local。
注意:如果显示的路径是/usr, 切换到选项二,否则你会把你的权限搞得一团糟。 - 将npm目录的所有者改为当前用户的名字(你的名字):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
这将会改变npm的子目录和其他工具((lib/node_modules, bin, and share).)的权限。
Option 2: Change npm's default directory to another directory
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.
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
1.Make a directory for global installations:
mkdir ~/.npm-global
2.Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
3.Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
4.Back on the command line, update your system variables:
source ~/.profile
Test: Download a package globally without using sudo.
npm install -g jshint
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile
):
NPM_CONFIG_PREFIX=~/.npm-global
选项二:修改npm的默认目录到其他目录
当你不想修改npm的默认目录的所有权时(比如 /usr),因为这会引发一些问题,比如如果你正在与其他用户分享这个系统。
作为替代,你可以根据你的情况配置npm使用其他目录,这个目录回事我们的home文件夹下的一个隐藏目录。
- 创建一个全局安装的默认路径:
mkdir ~/.npm-global
- 配置npm到新目录的路径:
npm config set prefix '~/.npm-global'
- 代开或创建~/配置文件并且加上这一行:
export PATH=~/.npm-global/bin:$PATH
- 返回命令行,升级你的系统变量:
source ~/.profile
测试:不用sudo
全局地下载一个包:
npm install -g jshint
不使用步骤2到4,你也可以使用相应的ENV变量(比如,如果你不想修改~/.profile)
NPM_CONFIG_PREFIX=~/.npm-global
Option 3: 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.
brew install node
选项三:使用一个会解决这个问题的包管理器
如果刚在Mac OS上安装新的node,你可以选择使用 Homebrew 包管理器来避免这个问题。Homebrew刚安装好时,便用了正确的权限设置。
brew install node
网友评论