npm error code EACCES npm error syscall mkdir npm error path /usr/local/lib/node_modules/typescript npm error errno -13 npm error Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript' npm error at async mkdir (node:internal/fs/promises:858:10) npm error at async /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:624:20 npm error at async Promise.allSettled (index 0) npm error at async [reifyPackages] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:325:11) npm error at async Arborist.reify (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:142:5) npm error at async Install.exec (/usr/local/lib/node_modules/npm/lib/commands/install.js:150:5) npm error at async Npm.exec (/usr/local/lib/node_modules/npm/lib/npm.js:207:9) npm error at async module.exports (/usr/local/lib/node_modules/npm/lib/cli/entry.js:74:5) {
This error occurs because you don't have the necessary permissions to write to the /usr/local/lib/node_modules directory, which is a system directory that typically requires administrator privileges.
Here are several solutions to fix this:
1. Use sudo (not recommended for global npm installs)
bash
复制
下载
sudo npm install -g typescript
Note: Using sudo with npm is generally not recommended as it can lead to permission issues later.
2. Change npm's default directory (recommended)
The best practice is to configure npm to use a directory you own for global installations:
bash
复制
下载
mkdir -p ~/.npm-global npm config set prefix '~/.npm-global'
Then add this to your shell config file (~/.bashrc, ~/.zshrc, or ~/.profile):
bash
复制
下载
export PATH=~/.npm-global/bin:$PATH
Reload your shell:
bash
复制
下载
source ~/.bashrc # or ~/.zshrc, etc.
Now try installing again:
bash
复制
下载
npm install -g typescript
3. Fix permissions on the system directory
If you prefer to use the system directory, you can take ownership of it:
bash
复制
下载
sudo chown -R $(whoami) /usr/local/lib/node_modules
Then try installing again:
bash
复制
下载
npm install -g typescript
4. Use a node version manager (best long-term solution)
Tools like nvm (Node Version Manager) or fnm handle permissions properly:
bash
复制
下载
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash # Then restart your terminal and: nvm install --lts npm install -g typescript
The second option (changing npm's prefix) is generally the best balance of simplicity and security for most users.

10万+

被折叠的 条评论
为什么被折叠?



