Search code examples
expressnrwlnrwl-nx

Nrwl Nx build for production missing node modules bundle


I have a Nrwl Nx repo with different apps (angular, nodejs with express) and shared libs inside. The repo was created with the nx cli and I want to build for production one of the express apps.

nx build:production myexpressapp

The bundle I get is very nice and runs if I run it (with pm2) from where it was built (dist folder). However, if I get it to production, the node modules are missing and the app does not start. If I copy the node_modules folder above the one with the built dist it works as well.

But I would very much like either of:

  • Getting a big bundle with all the required modules inside of it?
  • Getting another 'vendors' bundle along my main one where all the needed modules are?

I tried using "vendorChunk":true in my production build options but nothing changes.

Any thoughts?


Solution

  • Looking at angular.json (or workspace.json), if your builder is @nrwl/node:build, under options, set externalDependencies to none, like so:

    {
      "projects": {
        "api": {
          "architect": {
            "build": {
              "builder": "@nrwl/node:build",
              "options": {
                "externalDependencies": "none"
                ...
    

    You may experience errors like:

    ERROR in ...
    Module not found: Error: Can't resolve 'some-modules' in ...
    

    Just keep installing what its complaining about, until it stops.

    Reference: Nrwl Nx Node Builder