Build a NestJS app for prod w/ node_modules dependencies in bundle

Issue

I am having trouble running my NestJS app in production mode. After running nest build or nest build --webpack, my dist folder does not contain all the required modules, and I get an error when trying to run node main.js:

Error: Cannot find module '@nestjs/core'

I could not find any clear instructions on https://docs.nestjs.com/ on how to correctly build an app for production, so maybe I am missing something?

Rewritten Issue

I am having trouble running my NestJS app in production mode. After running nest build or nest build --webpack, my dist folder does not contain all the required modules. When I try to run node main.js, I get the following error:

Error: Cannot find module '@nestjs/core'

I looked through the NestJS documentation but I could not find any clear instructions on how to correctly build an app for production, so I think I may be missing something.

Answer

To build a NestJS app for production, you need to make sure that all the required dependencies are included in the dependencies section of your package.json file and not just in the devDependencies section.

To fix the issue, run the following command to move the missing dependencies from devDependencies to dependencies:

npm install --save-dev @nestjs/core @nestjs/common @nestjs/platform-express

Then, rebuild your app using nest build or nest build --webpack. The dist folder should now contain all the required modules, and you should be able to run node main.js without any errors.