nodejs 是用npm install安装依赖时报错Error: EACCES: permission denied

本文提供两种解决方案来处理在全局安装npm包时遇到的EACCES权限错误问题。第一种方案是重新安装npm,第二种方案是通过更改npm的默认路径以避免权限问题。文章还提供了详细的步骤和官方文档链接。

解决方式有两种:
1、重装npm,按照这个步骤 https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
2、改变npm默认路径以降低文件权限问题发生的概率

mkdir ~/.npm-global
npm config set prefix ‘~/.npm-global’
export PATH=~/.npm-global/bin:$PATH
source ~/.profile(该步不一定需要,视具体部署方式而定)
npm install -g jshint

可参考官方文档:https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

┌──(kali㉿kali)-[~/Downloads/antSword-master] └─$ npm install -g electron npm ERR! code EACCES npm ERR! syscall mkdir npm ERR! path /usr/local/lib/node_modules npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules' npm ERR! at async mkdir (node:internal/fs/promises:858:10) npm ERR! at async /usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:521:20 npm ERR! at async Promise.allSettled (index 0) npm ERR! at async [reifyPackages] (/usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:234:11) npm ERR! at async Arborist.reify (/usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:158:5) npm ERR! at async Install.exec (/usr/share/nodejs/npm/lib/commands/install.js:146:5) npm ERR! at async module.exports (/usr/share/nodejs/npm/lib/cli.js:133:5) npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules' npm ERR! at async mkdir (node:internal/fs/promises:858:10) npm ERR! at async /usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:521:20 npm ERR! at async Promise.allSettled (index 0) npm ERR! at async [reifyPackages] (/usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:234:11) npm ERR! at async Arborist.reify (/usr/share/nodejs/@npmcli/arborist/lib/arborist/reify.js:158:5) npm ERR! at async Install.exec (/usr/share/nodejs/npm/lib/commands/install.js:146:5) npm ERR! at async module.exports (/usr/share/nodejs/npm/lib/cli.js:133:5) { npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR! syscall: 'mkdir', npm ERR! path: '/usr/local/lib/node_modules' npm ERR! } npm ERR! npm ERR! The operation was rejected by your operating system. npm ERR! It is likely you do not have the permissions to access this file as the current user npm ERR! npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator. npm ERR! A complete log of this run can be found in: npm ERR! /home/kali/.npm/_logs/2025-07-16T09_27_50_382Z-debug-0.log如何解决?
07-17
npm error code 1 npm error path C:\Users\h60098905\hjx\ohos_react_native\react-native-harmony-cli npm error command failed npm error command C:\Windows\system32\cmd.exe /d /s /c npm run build npm error A complete log of this run can be found in: C:\Users\h60098905\AppData\Local\npm-cache\_logs\2025-08-13T02_54_55_103Z-debug-0.log PS C:\Users\h60098905\hjx\ohos_react_native\react-native-harmony-cli> npm install -g npm@latest npm error code EACCES npm error errno EACCES npm error FetchError: request to https://mirrors.huaweicloud.com/repository/npm/npm failed, reason: npm error at ClientRequest.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\minipass-fetch\lib\index.js:130:14) npm error at ClientRequest.emit (node:events:518:28) npm error at emitErrorEvent (node:_http_client:104:11) npm error at _destroy (node:_http_client:898:9) npm error at onSocketNT (node:_http_client:918:5) npm error at process.processTicksAndRejections (node:internal/process/task_queues:91:21) { npm error code: 'EACCES', npm error errno: 'EACCES', npm error type: 'system' npm error } npm error npm error The operation was rejected by your operating system. npm error It's possible that the file was already in use (by a text editor or antivirus), npm error or that you lack permissions to access it. npm error npm error If you believe this might be a permissions issue, please double-check the npm error permissions of the file and its containing directories, or try running npm error the command again as root/Administrator. npm error A complete log of this run can be found in: C:\Users\h60098905\AppData\Local\npm-cache\_logs\2025-08-13T02_58_13_278Z-debug-0.log
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值