使用concurrently执行npm run both同时运行前后端时报错求助
问题:同时运行前后端脚本报错
问题场景
我在package.json中配置了名为both的脚本,想要同时运行前后端,脚本内容为concurrently "npm run start" "nodemon backend/index.js",但执行npm run both时出现报错。报错包含nodemon的使用提示,以及npm无法找到C:\Users\Shakir\package.json的ENOENT错误。
报错日志
npm run both npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. > inotebook@0.1.0 both > concurrently "npm run start" "nodemon backend/index.js" [1] Usage: nodemon [nodemon options] [script.js] [args] [1] [1] See "nodemon --help" for more. [1] [1] nodemon backend/index.js exited with code 0 [0] npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. [0] npm ERR! code ENOENT [0] npm ERR! syscall open [0] npm ERR! path C:\Users\Shakir/package.json [0] npm ERR! errno -4058 [0] npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\Shakir\package.json' [0] npm ERR! enoent This is related to npm not being able to find a file. [0] npm ERR! enoent
脚本配置
"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "both": "concurrently \"npm run start\" \"nodemon backend/index.js\"" },
解决方案
- 修正终端工作目录:确保执行
npm run both时,终端当前处于项目根目录(即包含package.json的文件夹)。如果不在,用cd命令切换到对应目录后再运行脚本。 - 检查nodemon目标文件路径:确认
backend/index.js存在于项目根目录下,路径拼写无错误(比如大小写、文件夹名称)。如果文件不存在,创建后端入口文件;如果路径错误,修改脚本中的路径。 - 本地安装nodemon:执行
npm install nodemon --save-dev,优先使用本地安装的nodemon,避免全局安装带来的路径问题。 - 调整concurrently命令格式(Windows环境):Windows系统下可尝试将脚本中的双引号替换为单引号,避免转义问题:
"both": "concurrently 'npm run start' 'nodemon backend/index.js'"
内容的提问来源于stack exchange,提问作者Shakir Ahmad
相关产品推荐
相关产品推荐

