nx icon indicating copy to clipboard operation
nx copied to clipboard

@nrwl/node:build package.json not including all external dependencies.

Open rs-r2d2 opened this issue 3 years ago โ€ข 12 comments

Current Behavior

Code contains two dependencies

  • express
  • morgan

On serve or build. The generated package.json does not include morgan as a dependency, only express is included in package.json

project.targets.build.options.generatePackageJson = true

{
  "name": "sample-api",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.17.1"
  },
  "main": "main.js"
}

Expected Behavior

I expected the output of package.json to include morgan as a dependency.


{
  "name": "sample-api",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.17.1",
    "morgan": "version"
  },
  "main": "main.js"
}

Steps to Reproduce

  1. create nx workspace.
  2. create new application using @nrwl/node.
  3. create a express server with morgan middleware
import * as express from 'express'
import * as morgan from 'morgan'

const app = express()

app.use(morgan('common'))
app.get('/', (req ,res) => {
  return res.status(200).json({ message:'sample api'})
})

const port = 3000 || process.env.APP_PORT

app.listen(port, () => {
  console.log(`App listening on http://localhost:${port}`)
})

  1. In workspace.json set project.targets.build.options.generatePackageJson = true .
  2. Change build location to apps/app_name/build .
  3. Run nx r sample-api:serve or nx r sample-api:build .
  4. check apps/app_name/build/package.json . package.json does not include morgan as dependency.

Project entry in workspace.json

 "sample-api": {
      "root": "apps/sample-api",
      "sourceRoot": "apps/sample-api/src",
      "projectType": "application",
      "targets": {
        "build": {
          "executor": "@nrwl/node:build",
          "outputs": ["{options.outputPath}"],
          "options": {
            "progress": true,
            "generatePackageJson": true,
            "outputPath": "apps/sample-api/dist",
            "main": "apps/sample-api/src/main.ts",
            "tsConfig": "apps/sample-api/tsconfig.app.json",
            "assets": ["apps/sample-api/src/assets"]
          },
          "configurations": {
            "production": {
              "optimization": true,
              "extractLicenses": true,
              "inspect": false,
              "fileReplacements": [
                {
                  "replace": "apps/sample-api/src/environments/environment.ts",
                  "with": "apps/sample-api/src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "executor": "@nrwl/node:execute",
          "options": {
            "buildTarget": "sample-api:build"
          }
        },
        "lint": {
          "executor": "@nrwl/linter:eslint",
          "options": {
            "lintFilePatterns": ["apps/sample-api/**/*.ts"]
          }
        },
        "test": {
          "executor": "@nrwl/jest:jest",
          "outputs": ["coverage/apps/sample-api"],
          "options": {
            "jestConfig": "apps/sample-api/jest.config.js",
            "passWithNoTests": true
          }
        }
      }
    },

This issue may not be prioritized if details are not provided to help us reproduce the issue.

Failure Logs

N/A

Environment

node 14 ubuntu LTS 20

rs-r2d2 avatar Aug 31 '21 11:08 rs-r2d2

I have the same issue with a lot more packages like nestjs config, gcp storage, luxon

Mistic92 avatar Oct 14 '21 17:10 Mistic92

@Mistic92 yours is probably similar to https://github.com/nrwl/nx/issues/5820.

@rishabh-husky Could you please provide a reproduce sample?

I do the following:

  • yarn create nx-workspace test-express --preset=express
  • cd test-express
  • yarn add morgan
  • Add morgan to main.ts
  • Add generatePackageJson: true to build options
  • nx build image

nartc avatar Nov 11 '21 23:11 nartc

I have the same problem. I use 11 dependencies but only 6 are listed in the package.json. I do not understand how it works but it seems only those directly imported in main.ts are saved. If I add a transitive dependency directly to main.ts it appears in the package.json.

By transitive dependency I just mean another file in the same application.

I'm using 13.2.1 which is the latest at the moment.

Fuco1 avatar Nov 21 '21 16:11 Fuco1

In case it's helpful, we use a custom webpack config + the generate-package-json-webpack-plugin plugin, which has proven more robust than nx implementation in our testing.

Our webpack.config.js looks something like this (we put our "apps" in a services folder, adjust accordingly):

const path = require('path');
const GeneratePackageJsonPlugin = require('generate-package-json-webpack-plugin');

module.exports = config => {
  /**
   * The main entry for services will always look something like `{root}/services/{serviceName}/src/main.ts`
   * We can take advantage of this convention to find the service package.json file
   */
  const entry = config.entry.main[0];
  const packageJSON = require(path.join(entry.split('/src/')[0], 'package.json'));

  packageJSON.dependencies = {
    ...(packageJSON.dependencies || {}),

    // common implicit deps across all packages. service specific implicit deps should go into their respective package.json file
    graphql: '~15.5.0',
  };

  /**
   * The first plugin is `ForkTsCheckerWebpackPlugin` - we do not need to do type checking as part of the dev server.
   * We have other checks for this. Removing this plugin significantly reduces the resource consumption of services during development.
   */
  config.plugins.shift();

  return {
    ...config,

    plugins: [...config.plugins, new GeneratePackageJsonPlugin(packageJSON)],

    // skip minimizing but still tree shake
    optimization: {
      ...config.optimization,

      minimize: false,

      // Non-prod uses webpack cacheUnaffected setting, which cannot be used with usedExports. So only set when building for prod
      usedExports: process.env.NODE_ENV === 'production',
    },
  };
};

marbemac avatar Jan 12 '22 14:01 marbemac

I am having the same issue - it seems like some dependencies, especially those from libs are not included in the generated package.json. This makes containerizing a single application really difficult.

moterink avatar Mar 11 '22 12:03 moterink

This also applies to node:webpack. We have an app that uses type-graphql and also has pg installed to use postgres, but Nx isn't able to infer the usage and I haven't figured out how to include it as an implicit dependency

Jacse avatar Mar 21 '22 16:03 Jacse

@marbemac your solution is nice but it adds a lot of unnecessary packages to package.json and I find it difficult to keep only the used ones in an automated manner.

My workaround is to add export * from 'pg'; in main.ts file

...
import { AppModule } from './app.module';

// workaround to have "pg" added in package.json from dist
export * from 'pg';

async function bootstrap (): Promise<void> {
...

andreidragu avatar May 18 '22 14:05 andreidragu

@andreidragu hmm - it should only add the the packages that are used in your code ๐Ÿค” (it does so for us at least).

marbemac avatar May 18 '22 17:05 marbemac

I'm facing the same issue now that I have migrated the workspace to the latest version. None of my apps have generated dependencies in their respective package.json

{
  "name": "demo-grpc-a",
  "version": "0.0.1",
  "dependencies": {},
  "main": "main.js"
}

dukuo avatar Jun 08 '22 22:06 dukuo

I am having the same issue with pg not being included in the dist package.json. It is used in a repositories library

Mellywins avatar Jul 04 '22 13:07 Mellywins

I am having the same issue with pg not being included in the dist package.json. It is used in a repositories library

@Mellywins were you able to resolve this issue? i'm using 13.9.4 and i have the same issue. thanks!

ferliesypmh avatar Aug 05 '22 06:08 ferliesypmh

@ferliesypmh I had to use a workaround to solve this... export * from 'pg'; I added this line in the main.ts

Mellywins avatar Aug 05 '22 08:08 Mellywins

Thanks @marbemac, that webpack plugin indeed does a much better job!

I'm not sure why this plugin picks up dependencies that NX does not, but definitely seems like a bug.

AlexJWayne avatar Aug 18 '22 21:08 AlexJWayne

I was running into this but after deleting root node_modules and package-lock.json and reinstalling, all dependencies were included in the generated package.json as expected.

bmayen avatar Nov 02 '22 13:11 bmayen

I kept getting this issue as well, my work around was to create an import.ts file in the app src folder, then add this file as anadditionalEntryPoints in the project.json

e.g. import.ts imports all required packages for nestjs, excluding angular related packages.

import 'handlebars';
import 'helmet';
import 'nodemailer';
import 'passport';
import 'passport-jwt';
import 'passport-local';
import 'pg';
import 'reflect-metadata';
import 'rxjs'
import 'tslib';
import 'uuid';

const forceImport = () => {
  console.log('forceImport');
};

export default forceImport;

then add this file in the additional entry points in project.json

        "generatePackageJson": true, <--- still required
        "additionalEntryPoints": [
          {
            "entryName": "import",
            "entryPath": "apps/{YOUR_APP}/src/import.ts" <-- file above
          },
        ]
      },

The package.json file gets generated with all the packages you want included with this app, might be improved but this worked for me

You can discard the imports.js that nx generates after your build runs. ๐Ÿ˜

kornect avatar Feb 15 '23 11:02 kornect

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! ๐Ÿ™

github-actions[bot] avatar Aug 15 '23 00:08 github-actions[bot]

Up

tonivj5 avatar Aug 15 '23 02:08 tonivj5

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! ๐Ÿ™

github-actions[bot] avatar Feb 28 '24 00:02 github-actions[bot]

Still an issue

DerHerrGammler avatar Feb 28 '24 08:02 DerHerrGammler