10

I tried to start my project with npm run start:prod command but got

Error: Cannot find module {path to my project}\dist\main.js'.

I have tried to rename the path to all my files in the project from src/myController to ../myController .

My 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"

My 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__/*"]
}

The actual output:

[email protected] prestart:prod {path to my project} rimraf dist && npm run build

[email protected] build {path to my project} tsc -p tsconfig.build.json

[email protected] start:prod {path to my project} node dist/main.js

internal/modules/cjs/loader.js:584 throw err; ^

Error: Cannot find module {path to my project}\dist\main.js'

2 Answers 2

25

Okeey, I was a little dumb. The answer is very easy. In package.json you need change line from "start:prod": "node dist/main.js" to "start:prod": "node dist/src/main.js". In tsconfig.json you need change "outDir": "../dist" to "outDir": "./dist"

Sign up to request clarification or add additional context in comments.

3 Comments

You can accept your own answer as the correct solution, by the way, to help others in the future know what response to look at.
This helped me as well. I believe in my case its due to a fact I'm generating typings from schema outside of the src. Still I don't like much seeing dist/src at all yet that's another story...
I had the same problem, it looks like NestJs ships with the following in the package.json "start:prod": "node dist/main" updating to "start:prod": "node dist/src/main" solved my issue
2

you can just add a new command: under the scripts in package.json :

"start:live": "rimraf dist && nest build && node dist/src/main", 

then you can run:

npm run start:live

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.