WSL环境下执行npm install cypress --save-dev报错退出码1
Cypress安装报错(退出码1)解决方法
问题描述
按照Cypress官方文档尝试将其安装为项目开发依赖,执行npm install cypress --save-dev时出现退出码1错误,自动切换到CMD尝试安装仍失败。操作及报错日志如下:
dev123@DESKTOP-BG104ST:~$ npm init -y Wrote to \\wsl$\Ubuntu\home\dev123\package.json: { "name": "dev123", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "description": "" } dev123@DESKTOP-BG104ST:~$ npm install cypress --save-dev npm ERR! code 1 npm ERR! path \\wsl$\Ubuntu\home\dev123\node_modules\cypress npm ERR! command failed npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node index.js --exec install npm ERR! '\\wsl$\Ubuntu\home\dev123\node_modules\cypress' npm ERR! CMD.EXE was started with the above path as the current directory. npm ERR! UNC paths are not supported. Defaulting to Windows directory. npm ERR! node:internal/modules/cjs/loader:959 npm ERR! throw err; npm ERR! ^ npm ERR! npm ERR! Error: Cannot find module 'C:\Windows\index.js' npm ERR! at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15) npm ERR! at Function.Module._load (node:internal/modules/cjs/loader:804:27) npm ERR! at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) npm ERR! at node:internal/main/run_main_module:17:47 { npm ERR! code: 'MODULE_NOT_FOUND', npm ERR! requireStack: [] npm ERR! } npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\OfficeUser\AppData\Local\npm-cache\_logs\2022-10-13T16_32_19_739Z-debug-0.log
报错原因
核心问题是CMD不支持UNC路径(\wsl$开头的跨系统共享路径),安装过程中调用Windows的CMD时,无法识别WSL的共享目录,默认切换到Windows系统目录后找不到安装所需的index.js文件,导致安装失败。
解决步骤
- 确保在WSL终端中直接操作本地路径,不要通过Windows文件管理器访问WSL目录后打开终端。直接在WSL终端进入项目目录:
cd /home/dev123 - 检查当前使用的npm是否为WSL环境下的版本,执行
which npm,输出应为类似/usr/bin/npm的WSL本地路径,而非Windows路径(如C:\Program Files\nodejs\npm.cmd)。如果是Windows的npm,需切换到WSL的node环境(可通过nvm或直接在WSL中重新安装node) - 重新执行安装命令,可添加
--force参数强制安装:npm install cypress --save-dev --force - 若仍失败,可单独安装Cypress二进制文件:
npx cypress install - 还可以修改npm的缓存和安装目录到WSL本地,避免跨系统路径问题:
npm config set cache /home/dev123/.npm-cache npm config set prefix /home/dev123/.npm-global
内容的提问来源于stack exchange,提问作者varez254
相关产品推荐
相关产品推荐

