我试图使用命令npm run start:prod
启动我的项目,但出现了以下错误:
Error: Cannot find module {path to my project}\dist\main.js'
.
我已经尝试将我的项目中所有文件的路径从src/myController
重命名为../myController
。
我的package.json文件(scripts部分)如下:
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "concurrently \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.json\" ",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && npm run build",
"start:prod": "node dist/main.js"
我的tsconfig.json文件
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"rootDir": ".",
"outDir": "../dist",
"baseUrl": "./src"
},
"include": [ "./src/**/*.ts", ],
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/__test__/*"]
}
实际输出:
nest-typescript-starter@1.0.0 prestart:prod {我的项目路径} rimraf dist && npm run build
nest-typescript-starter@1.0.0 build {我的项目路径} tsc -p tsconfig.build.json
nest-typescript-starter@1.0.0 start:prod {我的项目路径} node dist/main.js
internal/modules/cjs/loader.js:584 throw err; ^
错误:无法找到模块 {我的项目路径}\dist\main.js'
src
之外的模式中生成类型。但是我不太喜欢看到整个dist/src
,尽管这又是另一回事... - ciekawy"start:prod": "node dist/main"
将其更新为"start:prod": "node dist/src/main"
解决了我的问题。 - Andy