nestjsnrwl-nx

Generate package.json on nx build / deployment


I've a monorepo using nx with multiple node/nestjs apps. Some of the apps doesn't require all the packages used in the other apps. Because it's a monorepo, I need to install all packages for every apps during the deployment.

Is there a way generate a package.json on build that would contain only the packages needed for the app that I'm building?

I've tryed to use "generate-package-json-webpack-plugin" to generate the package.json, but it only detect half the dependencies.

I've also tried to build a single js file containing all the apps, but it doesn't seem to work and always require tslib.


Solution

  • Following up on Shlomi's answer - as workspace.json is now deprecated, generatePackageJson option moved to @nx/webpack:webpack package so a modern (as of 2023) way to make it work is to either:

    1. Define a default in nx.json:
    {
      "targetDefaults": {
        "build": {
          "executor": "@nx/webpack:webpack",
          "options": {
            "generatePackageJson": true
          },
          // ...
        },
        // ...
      },
      // ...
    }
    
    1. Define a project-specific setting in project.json:
    {
      "targets": {
        "build": {
          "executor": "@nx/webpack:webpack",
          "options": {
            "generatePackageJson": true
          },
          //...
        },
        // ...
      }
      // ...
    }
    
    1. Define a project-specific setting in package.json:
    {
      "name": "...",
      "scripts": {
        // ...
      },
      "nx": {
        "targets": {
          "build": {
            "executor": "@nx/webpack:webpack",
            "options": {
              "generatePackageJson": true
            },
          },
          // ...
        },
        // ...
      },
      // ...
    }