N0Cloud
N0Cloud

Reputation: 251

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.

Upvotes: 22

Views: 31101

Answers (4)

Nir T
Nir T

Reputation: 41

The easiest way I found is to just add the generatePackageJson option to the plugins array in webpack.config.js and it will generate a package.json and package-lock.json files with the dependencies you have for that specific app.

const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/your-app'), // change path as needed
  },
  plugins: [
    new NxAppWebpackPlugin({
      target: 'node',
      compiler: 'tsc',
      main: './src/main.ts',
      tsConfig: './tsconfig.app.json',
      assets: ['./src/assets'],
      generatePackageJson: true // <- Right here
    })
  ],
};

Which is similar to what is written on the Nx documentation only I wanted to use the tsc compiler rather than swc. enter image description here

For more info you can check out the documentation part that helped me on this here.

Hope this will be helpful to anyone in the future entering this thread!

Upvotes: 0

mjarosie
mjarosie

Reputation: 4603

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
        },
      },
      // ...
    },
    // ...
  },
  // ...
}

Upvotes: 6

Shlomi Levi
Shlomi Levi

Reputation: 3315

After I look at the nx source code I found the answer.

Set generatePackageJson to true in workspace.json where <project-name>/targets/build/options.

This will generate you package.json with the necessary dependencies for your app.

Here example:

"node-api": {
      "root": "apps/node-api",
      "sourceRoot": "apps/node-api/src",
      "projectType": "application",
      "prefix": "node-api",
      "targets": {
        "build": {
          "executor": "@nrwl/node:build",
          "outputs": ["{options.outputPath}"],
          "options": {
            "showCircularDependencies": false,
            "outputPath": "dist/apps/node-api",
            "main": "apps/node-api/src/main.ts",
            "tsConfig": "apps/node-api/tsconfig.app.json",
            "assets": ["apps/node-api/src/assets"],
            "generatePackageJson": true <----------------------
          },
....

Upvotes: 29

electrichead
electrichead

Reputation: 1170

Nx encourages a single-version policy and has a single package.json.

If the issue is that you are installing all the dependencies every time in CI before building then you might need to rely on the functionality provided by your CI system to cache these between runs - a lot of existing CI systems do provide these: * Gitlab: https://docs.gitlab.com/ee/ci/caching/ * CircleCI: https://circleci.com/docs/2.0/caching/ * Travis: https://docs.travis-ci.com/user/caching/

However this comes with its own set of issues (e.g. parallel jobs where one or more is changing the dependencies).

We can try to explore having a command in Nx: sort of a "affected:dep-install" that will detect which packages to install as part of the affected command. Please create an issue for it here: https://github.com/nrwl/nx/issues

Upvotes: 0

Related Questions