npm install express -g npm ERR! code EPERM npm ERR! syscall mkdir npm ERR! path D:\software\node.js\node_cache\_cacache npm ERR! errno -4048 npm ERR! Error: EPERM: operation not permitted, mkdir 'D:\software\node.js\node_cache\_cacache' npm ERR! [Error: EPERM: operation not permitted, mkdir 'D:\software\node.js\node_cache\_cacache'] { npm ERR! errno: -4048, npm ERR! code: 'EPERM', npm ERR! syscall: 'mkdir', npm ERR! path: 'D:\\software\\node.js\\node_cache\\_cacache' 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:\software\node.js\node_cache\_logs npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
时间: 2025-07-08 20:36:05 浏览: 8
在全局安装 `express` 时遇到 `Error: EPERM: operation not permitted, mkdir 'D:\Program Files\nodejs\node_cache\_cacache'` 错误,通常是由于权限不足、文件被占用或路径配置问题引起的。以下是几种有效的解决方法:
### 更改 npm 缓存目录
默认情况下,npm 使用系统路径作为缓存目录,例如 `C:\Users\用户名\AppData\npm-cache` 或者你当前 node 安装路径下的 `node_cache` 目录。你可以通过以下命令更改 npm 的缓存目录到一个你有写入权限的位置:
```bash
npm config set cache "D:\npm-cache"
```
确保 `D:\npm-cache` 是一个你拥有写入权限的目录。
### 以管理员身份运行命令行工具
在 Windows 上,某些目录(如 `Program Files`)受系统保护,普通用户可能没有权限在这些目录中创建或修改文件。可以尝试使用管理员权限运行命令提示符(CMD)或 PowerShell,然后再次执行安装命令:
```bash
npm install -g express
```
### 清理 npm 缓存
有时缓存文件可能会损坏或被其他进程锁定,清理缓存可能有助于解决问题。执行以下命令来清理 npm 缓存:
```bash
npm cache clean --force
```
如果仍然无法清理,请检查是否有程序正在使用缓存目录,并关闭它们后再试一次。
### 删除 `.npmrc` 文件
`.npmrc` 文件保存了 npm 的配置信息,有时旧的配置可能导致问题。可以在以下位置找到并删除该文件:
- Windows:`C:\Users\<你的用户名>\.npmrc`
- macOS/Linux:`~/.npmrc`
删除后,npm 会使用默认配置重新生成一个新的 `.npmrc` 文件。
### 检查文件占用情况
如果 `node_cache` 目录被其他程序(如文本编辑器、杀毒软件等)占用,也会导致操作失败。请关闭所有可能占用该目录的程序,或者暂时禁用杀毒软件再进行安装。
### 修改目录权限
如果你希望继续使用当前目录作为缓存目录,可以尝试修改其权限设置。右键点击目标目录(如 `D:\Program Files\nodejs\node_cache`),选择“属性” > “安全”选项卡,点击“编辑”,为当前用户添加“完全控制”权限。
---
阅读全文