安装Material UI时npm install卡住并出现Cannot find module错误
问题重现
执行安装命令:npm install @mui/material @emotion/react @emotion/styled
安装接近完成时卡顿约1分钟,随后抛出错误:
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.Error: Cannot find module 'C:\Windows\install.js'
已尝试以下常规修复但无效:
- 删除
node_modules和package-lock.json后重新执行npm install - 执行
npm cache clear --force清理缓存
可行解决方案
切换到本地磁盘目录操作
不要在网络共享的UNC路径(如\\server\project)下运行npm命令,将项目文件夹复制到本地磁盘(如C:\dev\my-react-app),打开终端切换到该本地目录后重新执行安装命令。修正npm的prefix和cache路径配置
先执行命令查看当前npm配置:npm config list
若发现prefix或cache配置为UNC路径,将其修改为本地路径:npm config set prefix "C:\Users\你的用户名\AppData\Roaming\npm" npm config set cache "C:\Users\你的用户名\AppData\Roaming\npm-cache"替换为你实际的用户目录路径,修改后重新执行安装。
改用PowerShell执行命令
PowerShell对UNC路径的兼容性优于CMD,打开PowerShell并切换到项目目录(即使是UNC路径),再执行安装命令测试。检查package.json的自定义脚本
查看项目根目录的package.json,确认是否存在自定义的install脚本,若有异常脚本导致npm错误调用Windows目录下的install.js,删除该脚本后重新安装。
内容的提问来源于stack exchange,提问作者Florin

