1.问题
PS C:\Enviroment\node-v20.17.0> npm install -g @angular/cli
npm error code EPERM
npm error syscall mkdir
npm error path C:\Enviroment\node-v20.17.0\_cacache
npm error errno EPERM
npm error FetchError: Invalid response body while trying to fetch https://registry.npmjs.org/@angular%2fcli: EPERM: operation not permitted, mkdir 'C:\Enviroment\node-v20.17.0\_cacache'
npm error at C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\minipass-fetch\lib\body.js:170:15
npm error at async Response.json (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\minipass-fetch\lib\body.js:75:17)
npm error at async RegistryFetcher.packument (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\pacote\lib\registry.js:98:25)
npm error at async RegistryFetcher.manifest (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\pacote\lib\registry.js:128:23)
npm error at async #fetchManifest (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:1199:20)
npm error at async #nodeFromEdge (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:1037:19)
npm error at async #buildDepStep (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:901:11)
npm error at async Arborist.buildIdealTree (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:181:7)
npm error at async Arborist.reify (C:\Enviroment\node-v20.17.0\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\reify.js:131:5)
npm error at async Install.exec (C:\Enviroment\node-v20.17.0\node_modules\npm\lib\commands\install.js:150:5) {
npm error code: 'EPERM',
npm error errno: 'EPERM',
npm error syscall: 'mkdir',
npm error path: 'C:\\Enviroment\\node-v20.17.0\\_cacache',
npm error type: 'system',
npm error requiredBy: '.'
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 notice
npm notice New patch version of npm available! 10.8.2 -> 10.8.3
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.3
npm notice To update run: npm install -g npm@10.8.3
npm notice
npm error Log files were not written due to an error writing to the directory: C:\Enviroment\node-v20.17.0\_logs
npm error You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
PS C:\Enviroment\node-v20.17.0>
2.原因:
这个错误信息表明你在尝试全局安装 Angular CLI 时遇到了权限问题。
3.解决方法
1.以管理员身份运行命令提示符:
在 Windows 中,找到“命令提示符”或“PowerShell”,右键单击它,然后选择“以管理员身份运行”。然后再尝试运行 npm install -g @angular/cli。
2. 检查文件权限:
确保你有权限访问 C:\Enviroment\node-v20.17.0\ 目录。你可以右键单击该目录,选择“属性”,然后查看“安全”选项卡以检查你的用户是否具有完全控制权限。
3.关闭可能使用该目录的程序:
确保没有其他程序(例如文本编辑器或杀毒软件)正在使用该目录中的文件。
4.改变 NPM 缓存路径:
如果权限问题持续存在,可以考虑将 NPM 缓存目录更改到一个你有权限访问的位置。你可以通过以下命令更改 NPM 的缓存目录:
npm config set cache C:\Users\<YourUsername>\AppData\Roaming\npm-cache --global
替换 为你的实际用户名。
5.更新 NPM:
你收到的通知中提到有一个新的 NPM 版本,可以尝试更新 NPM,可能会解决一些问题。运行以下命令来更新:
npm install -g npm@latest
6.使用 Node Version Manager (NVM):
- 如果你经常遇到权限问题,可以考虑使用 Node Version Manager (NVM),它可以帮助你更轻松地管理 Node.js 和 NPM 的版本,并避免权限问题。
验证安装:
安装完成后,可以通过运行以下命令来验证 Angular CLI 是否安装成功:
ng version