13
votes

After nest build or nest build --webpack dist folder does not contain all required modules and I got Error: Cannot find module '@nestjs/core' when trying to run node main.js.

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

1
Are you trying to deploy the artifact/dist folder directly? You should note that some libraries have machine specific code and have to be built on the target machine, e.g. bcrypt. When I deploy my production application I run nest build on the target server (after npm install). - Kim Kern
The problem is the absent of code, machine specific or not. You'll get the same error even with simple app generated by nest new my_project if you'll move resulting dist to other location on the same machine for example. - Dimanoid
node_modules is not bundled, no. This should be possible with webpack though. I assume you want to remove the source code and only keep the dist folder, right? Why? - Kim Kern
Strange question. Why people build "binaries"? To minimize dependencies, size, number of files to deploy. What's the profit of building then if need the same complex environment as for just nest start? - Dimanoid
Ok, thank you! Can you format it as a short answer so I can accept it. May be this will save some time for others... - Dimanoid

1 Answers

6
votes

Out of the box, nest cli does not support including the node_modules dependencies into the dist bundle.


However, there are some community examples of custom webpack configs that include the dependencies in the bundle, e.g. bundled-nest. As described in this issue, it is necessary to include the webpack.IgnorePlugin to whitelist unused dynamic libraries.