Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm run build > not creating dist folder in NestJs project

Tags:

json

nestjs

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'

like image 761
Tanya Dolgopolova Avatar asked Dec 04 '22 18:12

Tanya Dolgopolova


2 Answers

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"

like image 106
Tanya Dolgopolova Avatar answered Dec 09 '22 14:12

Tanya Dolgopolova


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
like image 25
Rootdevelopper Avatar answered Dec 09 '22 14:12

Rootdevelopper