我有一个简单的项目,它在我的 src 目录的 app.js 文件中启动一个节点服务器。
项目结构如下:
我的 package.json 包含
1 {
2 "name": "backend",
3 "version": "1.0.0",
4 "description": "",
5 "main": "src/app.js",
6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1",
8 "start": "node ./src/app.js"
9 },
10 "author": "my name",
11 "license": "ISC"
12 }
运行 npm start
时出现以下错误:
> backend@1.0.0 start
> node ./src/app.js
node:internal/modules/cjs/loader:930
throw err;
^
Error: Cannot find module '/Users/myname/dev/produce/personal/healthapp/backend/src/app.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
at Function.Module._load (node:internal/modules/cjs/loader:772:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
从项目根目录(与 package.json 相同)运行 node src/app.js
工作正常,但是