11

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?

Rares P
  • 303
  • 3
  • 15

1 Answers1

10

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

A. K. Tolentino
  • 2,112
  • 3
  • 25
  • 33
  • Impressive! I have been living in the dark for so long. :) It does indeed work. Thanks – Rares P Jul 13 '20 at 18:35
  • One of the few times in which I got the perfect answer I needed in no time! – Adriano di Lauro Dec 01 '20 at 16:45
  • Thank you. This was so hard to find. – Doug Jan 08 '21 at 23:58
  • 3
    This seems to bundle everyting in the node_modules, even things the app itself does not need... I am using NestJS and it complains that @nestjs/microservices cannot be found, even though I'm not using it in the project. When I install them, it complains that grpc, mqtt, redis and bunch of others are also not installed. I don't think I want to bundle everything with the app, only the modules it actually uses. – Papooch Jun 02 '21 at 14:23
  • I have explicitly specified the externalDependencies: "all" in build target options (workspace.json) and I am still experiencing the issue. – Akshay Kumar Aug 02 '21 at 10:16
  • correct doc reference https://nx.dev/node/webpack#externaldependencies – Bender Feb 25 '22 at 16:16