在Vscode使用命令npm报错-The operation was rejected by your operating system. npm ERR! It's possible that the file was already in use (by a text editor or antivirus)

发布时间 2023-07-02 19:45:25作者: 哩个啷个波

报错信息:

PS D:\disk\xubo\个人博客文章\27-Vue\资料(含课件)\vuedemo\vueproject> npm i pubsub-js
npm ERR! code EPERM
npm ERR! syscall open
npm ERR! path D:\disk\soft\node.js\node_cache_cacache\index-v5\1d\32\0400202fc22af03ff2926f006e455fe92c77b8136b8fbebbad5eee83f240
npm ERR! Error: EPERM: operation not permitted, open 'D:\disk\soft\node.js\node_cache_cacache\index-v5\1d\32\0400202fc22af03ff2926f006e455fe92c77b8136b8fbebbad5eee83f240'
npm ERR! [Error: EPERM: operation not permitted, open 'D:\disk\soft\node.js\node_cache_cacache\index-v5\1d\32\0400202fc22af03ff2926f006e455fe92c77b8136b8fbebbad5eee83f240'] {
npm ERR! errno: -4048,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'open',
npm ERR! path: 'D:\disk\soft\node.js\node_cache\_cacache\index-v5\1d\32\0400202fc22af03ff2926f006e455fe92c77b8136b8fbebbad5eee83f240',
npm ERR! requiredBy: '.'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
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! Log files were not written due to an error writing to the directory: D:\disk\soft\node.js\node_cache_logs
npm ERR! You can rerun the command with --loglevel=verbose to see the logs in your terminal

问题原因:没有权限打开这个目录->operation not permitted, open 'D:\disk\soft\node.js\node_cache_cacache

      没有管理员权限,无法在node_global和node_cache两个文件夹下进行一些必要的操作

解决方式:

方式一:

删除用户下的.npmrc文件,即不改变全局安装的路径(不够好,本质问题仍没有解决,全局还是会安装在C盘)
每次都用管理员身份运行命令行(不够方便,vscode 下的命令行默认也不是管理员身份运行的)

方式二:

直接修改node_global和node_cache文件夹的权限:
右键文件夹,打开属性,切换到安全选项卡