nx: "generatePackageJson" does not include dependencies from `libs/*`
Current Behavior
I’m trying to create a production Nest.js build with a minimal dependency tree. “generatePackageJson” does not include dependencies from a library within my own workspace.
Scenario:
apps/graphql imports libs/config. libs/config imports dotenv (3rd party). dotenv is not included in the package.json of apps/graphql generated file.
Expected Behavior
I expect dotenv to be listed in package.json.
Steps to Reproduce
- Create nest.js app
- Create
confignest.js Library withimport { config } from 'dotenv'; - Import
configlibrary - Add
"generatePackageJson": trueoption to app build target. - Build
- Notice that
dotenvdoes not exist inpackage.json
Failure Logs
Error: Cannot find module 'dotenv'
Environment
> NX Report complete - copy this into the issue template
Node : 16.3.0
OS : darwin x64
npm : 7.15.1
nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 12.7.2
@nrwl/cypress : Not Found
@nrwl/devkit : 12.7.2
@nrwl/eslint-plugin-nx : 12.7.2
@nrwl/express : Not Found
@nrwl/jest : 12.7.2
@nrwl/linter : 12.7.2
@nrwl/nest : 12.7.2
@nrwl/next : Not Found
@nrwl/node : 12.7.2
@nrwl/nx-cloud : Not Found
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 12.7.2
@nrwl/web : Not Found
@nrwl/workspace : 12.7.2
@nrwl/storybook : Not Found
@nrwl/gatsby : Not Found
typescript : 4.3.5
Extra Credit
Generated package.json should also either lock to exact package numbers or create a package.lock file. It’s probably easier to just pin down exact package numbers: "some-pkg": "1.2.3" instead of "some-pkg": "^1.2.0"
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 14
- Comments: 39 (15 by maintainers)
In case it helps anyone else, my solution to this issue was a bit different.
The build executor is
@nrwl/webpack:webpackfor me and@nrwl/webpackwas missing from my devDeps in package.json. Somehow it was still building to dist/, but without the correct deps.npm i -D @nrwl/webpackfixed things.I ran into this issue and it turns out I had a dependency listed in both
devDependenciesanddependenciesin mypackage.json, I must have pasted them there by accident, or inadvertently add a-Dor something.However, once I removed them from the
devDependenciessection my generatedpackage.jsonhad everything it was supposed to!I had the exact same problem with the
dotenvpackage missing from the generatedpackage.jsonfile when using the optiongeneratePackageJson.Removing the
dotenvpackage from thedevDependenciespart of the rootpackage.jsonsolved the problem.I needed this functionality as well and accomplished it by writing my own executor. Within an executor you can do:
and the
dependenciesvariable will have everything you need to populate dependencies fromlibs/*. To see the bulk of logic for howgeneratePackageJsonworks you can refer to this file. I would submit a PR but I don’t have the time, but I needed the same thing and accomplished it this way.Thanks @svaterlaus that worked for me! @nwrl/webpack was in node_modules because it was a dependency of another module (I guess), but installing it explicitly resolves my issue. I had none of my nest project dependencies in the generated package.json file and now it’s OK! 👍
Has anyone found any solutions for that?
Hi all, the original scenario (about
dotenv) seems to have been solved already (I can’t reproduce it with the issue author’s reproduce step).nrwl/nest:appnrwl/nest:libdotenv)npx nx build appwithgeneratePackageJson: truedist/apps/app/package.jsondoes havedotenvas one of the dependencies.If anyone runs into a similar issue, please share your reproduce steps.
In my case there were 2 problem:
I had my
peerDependenciesso I moved them todependencyand it worked like charmOne of my dependencies itself had
peerDependenciesExample: My
package.json@celo-tools/celo-ethers-wrapper’s package.jsonSolution: Explicitly import peerDependency
Make sure you updated to the current version of the executor(s).
For example
@nrwl/node:build executoris replaced with@nrwl/node:webpackand supportsgeneratePackageJsonoption (source code for schema).A few of them were renamed here: https://github.com/nrwl/nx/pull/9086
That said, I still have the issue outlined in the initial issue, and even with some packages that are not part of my code base they aren’t being picked up in a nest project despite being directly imported in non-unit test code paths.
I’m facing a similar issue. I generated a publishable lib that uses a MUI component but the generated
package.jsondoes not include the required dependencies.Here is a repo with the minimal setup:
What I’m missing?
Update
Finally solved it https://stackoverflow.com/questions/73293762/nx-missing-dependencies-on-generated-package-json/73306728#73306728
Thanks for the clarifications.
As for this issue, I’ll leave it open for a bit longer to see if anything comes up. Thanks again for your time
@nartc sorry; I haven’t been actively developing a NX project lately and since have switched to Vite and Turborepo, however, may consider converting back to Nx as Turborepo docker builds are insanely slow when “pruning” dependencies. My NX project I have in production uses webpack plugins to create pruned package.json files, so it’s great to see that this may work out of the box now.
It happened to me and I found out that the missing module is not added to the root package.json. I cleaned the node_modules and reinstall all the dependencies and it’s working now.
I face same issue. Anyone has solution for this issue?. so for now, I must copy root package.json for each project to handle missing modules
Alright well I didn’t have to use a custom executor, all my deps are working now.
I’m guessing at one point the problematic dependencies were a devDependency in my root package.json and the project graph didn’t want to detect that.
Would be curious if this helps anyone else assuming you’re on latest nx and using the correct executors. It’s working for me with
node:webpackI need to control versioning on explicit application for allowing different versions on each build. For allowing me push different distributions to different pops.