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