Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"generatePackageJson" does not include dependencies from libs/* #6840

Closed
j opened this issue Aug 24, 2021 · 39 comments
Closed

"generatePackageJson" does not include dependencies from libs/* #6840

j opened this issue Aug 24, 2021 · 39 comments
Assignees
Labels
blocked: repro needed blocked: retry with latest Retry with latest release or head. outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@j
Copy link

j commented Aug 24, 2021

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

  1. Create nest.js app
  2. Create config nest.js Library with import { config } from 'dotenv';
  3. Import config library
  4. Add "generatePackageJson": true option to app build target.
  5. Build
  6. Notice that dotenv does not exist in package.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"

@j j added the type: bug label Aug 24, 2021
@hiepxanh
Copy link
Contributor

I guess you can try to wrap it by something then import it?

@j
Copy link
Author

j commented Aug 25, 2021

@hiepxanh I'm not understanding. The package.json should include all possible dependencies needed for the app.

@leosvelperez leosvelperez added the scope: node Issues related to Node, Express, NestJS support for Nx label Aug 27, 2021
@edcs
Copy link

edcs commented Sep 16, 2021

I'm having the same issue - I have a Node.js based app which uses a lib that imports @apollo/federation. When I build the app with generatePackageJson set to true, the package.json file looks like this:

{
  "name": "services-subgraphs-people",
  "version": "0.0.1",
  "dependencies": {
    "type-graphql": "^1.1.1",
    "graphql": "^15.5.1",
    "@typegoose/typegoose": "^8.2.0",
    "mongoose": "^5.13.8",
    "mongoose-lean-id": "^0.2.0",
    "tslog": "^3.2.1",
    "reflect-metadata": "^0.1.13",
    "aws-sdk": "^2.977.0",
    "apollo-server": "^3.3.0"
  },
  "main": "main.js"
}

Meaning that when I run it, I get an error message stating that @apollo/federation is missing. (There are other dependencies missing which are imported by my lib too).

@DeadEye0112
Copy link

DeadEye0112 commented Sep 20, 2021

I had the exact same problem with the dotenv package missing from the generated package.json file when using the option generatePackageJson.

Removing the dotenv package from the devDependencies part of the root package.json solved the problem.

@nartc
Copy link
Contributor

nartc commented Nov 11, 2021

Duplicate of #5820

@nartc nartc marked this as a duplicate of #5820 Nov 11, 2021
@HackPoint
Copy link

@vsavkin @jeffbcross 'generatePackageJson' is not found in schema in latest would you have a solution to this? Seems like it's not documented in anywhere.

@nartc
Copy link
Contributor

nartc commented Feb 23, 2022

@HackPoint can you provide a simple reproduce?

@HackPoint
Copy link

@HackPoint can you provide a simple reproduce?

It was a simple yarn create nx-workspace pure angular project which is not created a simple workspace.json file even.

@nartc
Copy link
Contributor

nartc commented Feb 23, 2022

@HackPoint oh you're talking about initializing a new workspace with the Angular preset would yield this error?

@HackPoint
Copy link

@HackPoint oh you're talking about initializing a new workspace with the Angular preset would yield this error?

Actually tried it on an Empty project: added angular app and a single library and it's gives me the same error.

@nartc
Copy link
Contributor

nartc commented Feb 23, 2022

@HackPoint Thank you. What is the command that triggers the error? A build command? or a run command?

@HackPoint
Copy link

"generatePackageJson": true

nx run :build:production after I have added "generatePackageJson": true to project.json

@HackPoint
Copy link

@HackPoint Thank you. What is the command that triggers the error? A build command? or a run command?

image

@nartc
Copy link
Contributor

nartc commented Feb 23, 2022

sandbox is an Angular project? generatePackageJson isn't in Angular's build executor schema. Why do you need generatePackageJson with an Angular project?

@HackPoint
Copy link

HackPoint commented Feb 23, 2022

I need to control versioning on explicit application for allowing different versions on each build. For allowing me push different distributions to different pops.

@deeeed
Copy link

deeeed commented Mar 3, 2022

@vsavkin @jeffbcross 'generatePackageJson' is not found in schema in latest would you have a solution to this? Seems like it's not documented in anywhere.

+1, can't find any doc and facing the same issue

@trevordilley
Copy link

I ran into this issue and it turns out I had a dependency listed in both devDependencies and dependencies in my package.json, I must have pasted them there by accident, or inadvertently add a -D or something.

However, once I removed them from the devDependencies section my generated package.json had everything it was supposed to!

@Gabriellji
Copy link

@vsavkin @jeffbcross 'generatePackageJson' is not found in schema in latest would you have a solution to this? Seems like it's not documented in anywhere.

Has anyone found any solutions for that?

@jensbodal
Copy link
Contributor

Make sure you updated to the current version of the executor(s).

For example @nrwl/node:build executor is replaced with @nrwl/node:webpack and supports generatePackageJson option (source code for schema).

A few of them were renamed here: #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.

@31i0t
Copy link

31i0t commented May 21, 2022

I needed this functionality as well and accomplished it by writing my own executor. Within an executor you can do:

import { readCachedProjectGraph } from "@nrwl/devkit";
const { dependencies } = readCachedProjectGraph();

and the dependencies variable will have everything you need to populate dependencies from libs/*. To see the bulk of logic for how generatePackageJson works 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.

@jensbodal
Copy link
Contributor

jensbodal commented May 21, 2022

So I took a look at that, at least one of my issues seems to stem from the project graph, so looking more into that. Looking at the dependencies that are output is missing one of my imports entirely.

We have n+1 nx monorepos. We use a commons one to publish the majority of shared code and then a mono repo for each full-stack application (api+web+cdk). There are other packages published by the commons package that show up just fine, but for whatever reason one of them is missing despite it being in dependencies section of root package json.

@jensbodal
Copy link
Contributor

jensbodal commented May 21, 2022

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.

NX_CACHE_PROJECT_GRAPH=false nx build api --clear-cache

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:webpack

@vietkute02
Copy link

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

@Keith-Hon
Copy link

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.

@npwork
Copy link

npwork commented Jun 13, 2022

In my case there were 2 problem:

  1. I had my peerDependencies so I moved them to dependency and it worked like charm

  2. One of my dependencies itself had peerDependencies

Example:
My package.json
Screen Shot 2022-06-13 at 8 23 05 AM

@celo-tools/celo-ethers-wrapper's package.json

Screen Shot 2022-06-13 at 8 23 18 AM

Solution:
Explicitly import peerDependency
Screen Shot 2022-06-13 at 8 23 48 AM

@nartc nartc self-assigned this Jun 17, 2022
@nartc
Copy link
Contributor

nartc commented Jun 25, 2022

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).

  • Create a new workspace
  • Generate a NestJS application with nrwl/nest:app
  • Generate a library with nrwl/nest:lib
  • Use a dependency in the library (eg: dotenv)
  • Use the library in the app
  • Run npx nx build app with generatePackageJson: true
  • dist/apps/app/package.json does have dotenv as one of the dependencies.

image

`LibModule` uses `date-fns`

image

`BuildableLibModule` uses `dotenv`

image

Both are imported in `AppModule`

image

Both dependencies show up in `dist/apps/api/package.json`

If anyone runs into a similar issue, please share your reproduce steps.

@github-actions
Copy link

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 github-actions bot added the stale label Jul 10, 2022
@yasin-kismet
Copy link

any updates?

@nartc
Copy link
Contributor

nartc commented Jul 12, 2022

@yasinntza I asked for a new reproduce to investigate further as the original issue seems to have been solved already. Check my previous message. Can you share a reproduce?

@github-actions github-actions bot removed the stale label Jul 13, 2022
@github-actions
Copy link

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 github-actions bot added the stale label Jul 27, 2022
@j
Copy link
Author

j commented Jul 27, 2022

@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.

@github-actions github-actions bot removed the stale label Jul 28, 2022
@dan-cooke
Copy link
Contributor

dan-cooke commented Jul 30, 2022

@nartc I can still reproduce this issue with OP's reproduction steps.

  1. npx create-nx-workspace --preset=nest

image

  1. nx g @nrwl/nest:lib env

image

  1. npm i dotenv
  2. Add the following to libs/env/src/index.ts
import { config } from 'dotenv';

config();

image

  1. Add the following line to the nest application
import '@nx-bug-generate-package/env'
  1. Add generatePackageJson to the webpack options

image

  1. Run nx build api

The output package.json does not include dotenv
image

Report:


   Node : 16.13.0
   OS   : darwin x64
   npm  : 8.1.0

   nx : 13.10.6
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 13.10.6
   @nrwl/eslint-plugin-nx : 13.10.6
   @nrwl/express : Not Found
   @nrwl/jest : 13.10.6
   @nrwl/js : 13.10.6
   @nrwl/linter : 13.10.6
   @nrwl/nest : 13.10.6
   @nrwl/next : Not Found
   @nrwl/node : 13.10.6
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 13.10.6
   typescript : 4.6.4
   rxjs : 7.5.6
   ---------------------------------------
   Community plugins:

Can we remove repro needed and retry with latest labels please - this is still a very active bug.

Edit.

Okay I have figured out what is causing this issue...

It is related to baseUrl which is incredibly annoying - as NX just does not play well with baseUrl

I will post another issue

@nartc
Copy link
Contributor

nartc commented Jul 30, 2022

@dan-cooke Thank you for the steps. However, your reproduction is against 13.10.6 which is an old version. Can you try the steps again with the latest 14.5.x? If it's still an issue with 14.5.x, then I'll remove the retry-with-latest label.

@dan-cooke
Copy link
Contributor

@nartc As for OP's issue - I cannot actually reproduce, however ther are some things to note:

  1. OPs issue still exists if you do not actually export from the lib, ie.
import '@repo/env'
  1. My reproduction was actually a red herring, and thte issue is part of an underlying issue with how nx integrates with tsconfig paths and baseUrl I have opened another issue askig for some guidance here Guidance/support for using baseUrl and paths #11374

I believe this current issue can be closed however, as with OP's steps (As long as you import a named export) it cannot be reproduced

@nartc
Copy link
Contributor

nartc commented Jul 30, 2022

Thanks for the clarifications.

  1. I feel like Nx can (and should) make this case work but it can be a different issue.
  2. For sure, please feel free to tag me in the issue you're talking about here and we'll be on it

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 nartc closed this as completed Aug 4, 2022
@oncet
Copy link

oncet commented Aug 9, 2022

I'm facing a similar issue. I generated a publishable lib that uses a MUI component but the generated package.json does 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

@svaterlaus
Copy link

svaterlaus commented Oct 5, 2022

In case it helps anyone else, my solution to this issue was a bit different.

The build executor is @nrwl/webpack:webpack for me and @nrwl/webpack was missing from my devDeps in package.json. Somehow it was still building to dist/, but without the correct deps. npm i -D @nrwl/webpack fixed things.

@Rafarel
Copy link

Rafarel commented Oct 31, 2022

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!
👍

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: repro needed blocked: retry with latest Retry with latest release or head. outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
Development

No branches or pull requests