typrscript安装权限问题解决

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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值