nx: JS package build incorrectly includes dependencies of dependencies

Current Behavior

A package built with @nrwl/js which uses the “tsc” build executor will not output the correct dependencies in its resulting package.json file if the package depends on another buildable package in the repo which itself has dependencies. Instead, the package.json will include all the dependencies of that sub package as well.

Expected Behavior

The resulting package.json should include only the dependencies it directly uses, not the dependencies of the other packages it depends on

Steps to Reproduce

  1. Generate buildable JS packages A and B
  2. Add some dependency to package B (say lodash)
  3. Add package B as a dependency of package A
  4. nx build package-a
  5. Package A will include lodash in its package.json even if its only directly used by package B

Environment

 >  NX   Report complete - copy this into the issue template

   Node : 16.13.2
   OS   : darwin x64
   npm  : 8.1.2

   nx : 13.8.8
   @nrwl/angular : undefined
   @nrwl/cli : 13.8.8
   @nrwl/cypress : 13.8.5
   @nrwl/detox : undefined
   @nrwl/devkit : 13.8.8
   @nrwl/eslint-plugin-nx : 13.8.8
   @nrwl/express : undefined
   @nrwl/jest : 13.8.8
   @nrwl/js : 13.8.8
   @nrwl/linter : 13.8.8
   @nrwl/nest : undefined
   @nrwl/next : 13.8.5
   @nrwl/node : 13.8.8
   @nrwl/nx-cloud : undefined
   @nrwl/react : 13.8.8
   @nrwl/react-native : undefined
   @nrwl/schematics : undefined
   @nrwl/storybook : 13.8.8
   @nrwl/tao : 13.8.8
   @nrwl/web : 13.8.8
   @nrwl/workspace : 13.8.8
   typescript : 4.3.5
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

This is still a problem in Nx version 16 …

Same issue on “15.0.0”

Hi,

to anyone interested, I’m managed to fix that issue by using @nx/dependency-checks ESLint rule to manage (almost) automatically the dependencies of your Nx library.

You can follow this great article by my fellow Nx champion 🏆 @LayZeeDK --> https://dev.to/this-is-learning/manage-nx-library-dependencies-with-the-nxdependency-checks-eslint-rule-2lem for more information.

For a real world project where this now recommended approach is implemented, you can have a look at my repo: https://github.com/tinesoft/nxrocks

Hope it helps! ^^

@tinesoft wow, thank you so much for the recommendation! While ideally i’d hoped that nx could have automatically fixed this, this is a good enough solution for me to finally retire my terrible custom tool which pruned the generated package.json afterwards.

Thanks!

any chance of this ever getting fixed? it seems like a pretty big flaw in what I understand to be a core Nx feature

Opened this recommended fix as PR #10600

https://github.com/nrwl/nx/blob/52b47659df4e03b6295f9d867f300d33d32c97e0/packages/js/src/utils/check-dependencies.ts#L21-L27

The fix is a one liner:

 calculateProjectDependencies( 
   projectGraph, 
   context.root, 
   context.projectName, 
   context.targetName, 
   context.configurationName,
+  true
 );