如何修复npm start时出现的'sh: index.js: command not found'错误?
如何修复npm start时出现的"sh: index.js: command not found"错误
问题背景
我在运行npm start时遇到了如下错误,项目的package.json内容和错误日志如下:
package.json内容:
{ "name": "trelloclone-client", "version": "1.0.0", "main": "index.js", "scripts": { "start": "index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/hlee686/TrelloClone-client.git" }, "author": "", "license": "ISC", "bugs": { "url": "https://github.com/hlee686/TrelloClone-client/issues" }, "homepage": "https://github.com/hlee686/TrelloClone-client#readme", "dependencies": { "dependencies": "0.0.1", "node_module": "0.0.0", "start": "^5.1.0" }, "devDependencies": {}, "description": "" }
错误日志:
Jakes-MacBook-Air:TrelloClone-client jslee$ npm start trelloclone-client@1.0.0 start /Users/jslee/Desktop/TrelloClone-client index.js sh: index.js: command not found npm ERR! code ELIFECYCLE npm ERR! syscall spawn npm ERR! file sh npm ERR! errno ENOENT npm ERR! trelloclone-client@1.0.0 start: index.js npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the trelloclone-client@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /Users/jslee/.npm/_logs/2020-04-09T10_18_00_148Z-debug.log
解决方案
嘿,这个问题其实挺常见的,我来帮你一步步解决:
1. 核心修复:给start脚本加上node命令
你现在的start脚本直接写了index.js,系统会把它当成一个系统级命令去查找,但index.js是Node.js脚本,必须用node命令来解释执行才行。
打开你的package.json,找到scripts区块里的start字段,修改成这样:
"scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }
2. 确认入口文件存在
接下来检查你的项目根目录下是否真的有index.js文件,如果这个文件不存在,哪怕脚本写对了也会报错。如果文件不在根目录,要把脚本里的路径改成正确的(比如node src/index.js)。
3. 清理无效依赖(可选但推荐)
我注意到你的dependencies里有两个奇怪的包:dependencies和node_module,这两个根本不是合法的npm包,应该是误加的。把它们删掉,让dependencies变成这样:
"dependencies": { "start": "^5.1.0" }
然后运行npm install更新一下依赖,避免后续出现莫名其妙的问题。
验证修复
完成上面的步骤后,再运行npm start,应该就能正常启动你的项目了!
内容的提问来源于stack exchange,提问作者Sean Lee
相关产品推荐
相关产品推荐

