Nrwl Nx build for production missing node modules bundle
Asked Answered
T

2

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?

Tobitobiah answered 20/3, 2020 at 10:53 Comment(0)
P
11

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

Palomino answered 12/7, 2020 at 5:38 Comment(6)
Impressive! I have been living in the dark for so long. :) It does indeed work. ThanksTobitobiah
One of the few times in which I got the perfect answer I needed in no time!Verret
Thank you. This was so hard to find.Cocke
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.Peppy
I have explicitly specified the externalDependencies: "all" in build target options (workspace.json) and I am still experiencing the issue.Liss
correct doc reference nx.dev/node/webpack#externaldependenciesBria
T
0

If you're missing the node_modules folder entirely, you need to add the generatePackageJson option to your project.json

Like so:

"targets": {
    "build": {
      "executor": "@nrwl/webpack:webpack",
      "options": {
        "outputPath": "...",
        "main": "...",
        "tsConfig": "...",
        "assets": [...],
        "generatePackageJson": true,
        "target": "node",
        "compiler": "tsc"
      },
    ...
Tristram answered 8/9, 2023 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.