Fixing npm permissions
修補(bǔ)npm權(quán)限
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.
當(dāng)你在全局安裝一個(gè)包時(shí)汛闸,你可能會(huì)收到一個(gè)EACCES錯(cuò)誤棺弊。這表明盒卸,你沒有被允許寫一個(gè),npm用來存儲(chǔ)全局包和命令的路徑转砖。
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.
你可以通過如下三個(gè)方法中的一個(gè)捡需,來修復(fù)這個(gè)問題:
- 修改npm默認(rèn)目錄的權(quán)限坑傅。
- 修改npm的默認(rèn)目錄到其他目錄呜呐。
- 安裝帶有一個(gè)包管理器的node,它會(huì)罩著你的充边。
你應(yīng)該在進(jìn)行下一步前庸推,讓你的電腦進(jìn)行備份。
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).
選項(xiàng)一:修改npm默認(rèn)路徑的權(quán)限
- 找到npm目錄的路徑:
npm config get prefix
對(duì)于很多系統(tǒng),路徑會(huì)是/usr/local贬媒。
注意:如果顯示的路徑是/usr刮吧, 切換到選項(xiàng)二,否則你會(huì)把你的權(quán)限搞得一團(tuán)糟掖蛤。 - 將npm目錄的所有者改為當(dāng)前用戶的名字(你的名字):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
這將會(huì)改變npm的子目錄和其他工具((lib/node_modules, bin, and share).)的權(quán)限杀捻。
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
選項(xiàng)二:修改npm的默認(rèn)目錄到其他目錄
當(dāng)你不想修改npm的默認(rèn)目錄的所有權(quán)時(shí)(比如 /usr),因?yàn)檫@會(huì)引發(fā)一些問題蚓庭,比如如果你正在與其他用戶分享這個(gè)系統(tǒng)致讥。
作為替代,你可以根據(jù)你的情況配置npm使用其他目錄器赞,這個(gè)目錄回事我們的home文件夾下的一個(gè)隱藏目錄垢袱。
- 創(chuàng)建一個(gè)全局安裝的默認(rèn)路徑:
mkdir ~/.npm-global
- 配置npm到新目錄的路徑:
npm config set prefix '~/.npm-global'
- 代開或創(chuàng)建~/配置文件并且加上這一行:
export PATH=~/.npm-global/bin:$PATH
- 返回命令行,升級(jí)你的系統(tǒng)變量:
source ~/.profile
測(cè)試:不用sudo
全局地下載一個(gè)包:
npm install -g jshint
不使用步驟2到4港柜,你也可以使用相應(yīng)的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
選項(xiàng)三:使用一個(gè)會(huì)解決這個(gè)問題的包管理器
如果剛在Mac OS上安裝新的node,你可以選擇使用 Homebrew 包管理器來避免這個(gè)問題夏醉。Homebrew剛安裝好時(shí)爽锥,便用了正確的權(quán)限設(shè)置。
brew install node