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

Generated package.json for publishable libs includes wrong peerDependencies #8096

Closed
the-ult opened this issue Dec 10, 2021 · 11 comments
Closed
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@the-ult
Copy link

the-ult commented Dec 10, 2021

Current Behavior

When generating the package.json for a publishable lib it sometimes includes dependencies as peerDependency, which shouldn't.

Expected Behavior

Only include the dependencies in the generated package.json, that are actually needed/used in production.

Steps to Reproduce

For example, in the angular-testing-library, we encountered the problem that @angular/forms and @testing-library/user-event were somehow included in the generated package.json

@angular/forms and @testing-library/user-event are NOT used in the library files, but ARE used in in the tests/spec files.

/// libs/testing-library/package.json

"peerDependencies": {
    "@angular/common": ">= 13.0.0",
    "@angular/platform-browser": ">= 13.0.0",
    "@angular/router": ">= 13.0.0",
    "@angular/core": ">= 13.0.0"
  },
/// GENERATED
/// dist/@testing-library/angular/package.json

"peerDependencies": {
    "@angular/common": ">= 13.0.0",
    "@angular/platform-browser": ">= 13.0.0",
    "@angular/router": ">= 13.0.0",
    "@angular/core": ">= 13.0.0",
    "@angular/forms": "13.0.2",  // should not be included as peerDependency
    "rxjs": "^7.4.0",
    "@testing-library/user-event": "^13.5.0" // should not be included as peerDependency
  },

(moving those 2 dependencies to our devDependencies solves the problem. But is that the correct solution?)

See: testing-library/angular-testing-library#272 (comment) for more information

N.B.
The strange thing is that the problem did not occur while using angular.json, but does occur with workspace.json

Possible Solutions

  • Maybe the solution could be to excluded dependencies only used in tests?
  • Or adding a nx section to the library package.json with an exlcude?
{
 ...
  "nx": {
    "excludeDependencies": ["@angular/forms", ....]
  },
  "peerDependencies":{
    ...
  }
}

Environment

>  NX  Report complete - copy this into the issue template

  Node : 16.13.1
  OS   : darwin x64
  npm  : 7.24.2
  
  nx : 13.2.2
  @nrwl/angular : 13.2.2
  @nrwl/cli : 13.2.2
  @nrwl/cypress : 13.2.2
  @nrwl/devkit : 13.2.2
  @nrwl/eslint-plugin-nx : 13.2.2
  @nrwl/express : undefined
  @nrwl/jest : 13.2.2
  @nrwl/linter : 13.2.2
  @nrwl/nest : undefined
  @nrwl/next : undefined
  @nrwl/node : 13.2.2
  @nrwl/nx-cloud : 12.5.4
  @nrwl/react : undefined
  @nrwl/react-native : undefined
  @nrwl/schematics : undefined
  @nrwl/tao : 13.2.2
  @nrwl/web : undefined
  @nrwl/workspace : 13.2.2
  @nrwl/storybook : 13.2.2
  @nrwl/gatsby : undefined
  typescript : 4.4.4
  rxjs : 7.4.0
  ---------------------------------------
  Community plugins:
         @angular/common: 13.0.2
         @angular/compiler: 13.0.2
         @angular/core: 13.0.2
         @angular/platform-browser: 13.0.2
         @angular/platform-browser-dynamic: 13.0.2
         @angular/router: 13.0.2
         @ngrx/store: 13.0.1
         @angular/animations: 13.0.2
         @angular/cdk: 13.0.2
         @angular/material: 13.0.2
         @angular/forms: 13.0.2
         @angular-devkit/build-angular: 13.0.3
         @angular/cli: 13.0.3
         @angular/compiler-cli: 13.0.2
         @angular/language-service: 13.0.2
         @nrwl/nx-plugin: 13.2.2

Related

Might be related to:

@AgentEnder AgentEnder added the scope: node Issues related to Node, Express, NestJS support for Nx label Dec 10, 2021
@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 14, 2022
@wrslatz
Copy link
Contributor

wrslatz commented Jul 14, 2022

I think this is still an issue that needs further investigation

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

We are encountering a similar issue with wrong peerDependencies, but in our case it has to do with secondary entrypoints.

Our publishable UI library has a couple of "main" peerDependencies, like @angular/core and @angular/common.
Only one of the components has a peerDependency to @angular/material, so we have created a package.json for that specific entrypoint with the following peerDependency:

"@angular/material": "^13.0.0 || ^14.0.0 || ^15.0.0",

But, when building the lib with NX this results in a main package.json with:

"peerDependencies": {
    "@angular/core": "^13.0.0 || ^14.0.0 || ^15.0.0",
    "@angular/common": "^13.0.0 || ^14.0.0 || ^15.0.0",
    "@angular/router": "^13.0.0 || ^14.0.0 || ^15.0.0",
    "@angular/material": "13.3.3" ❌
  },

@tinesoft
Copy link
Contributor

Hi,

I have a similar problems on my nxrocks project project, event after having upgraded to Nx v15.0.4, which includes in particular that fix : #12851.

In my case, I have a publishable library named @nxrocks/common with a **secondary entrypoint ** @nxrocks/common/testing used only in the tests files (*.spec.ts, *.test.ts) of projects that depend on that publishable library.

Here is an excerpt of the root tsconfig file:

tsconfig.base.json

    "paths": {
      "@nxrocks/common": ["packages/common/src/index.ts"],
      "@nxrocks/common/testing": ["packages/common/testing/index.ts"], <-- secondary entry point, used in tests only
      "@nxrocks/nx-flutter": ["packages/nx-flutter/src/index.ts"],
      "@nxrocks/nx-micronaut": ["packages/nx-micronaut/src/index.ts"],
      "@nxrocks/nx-quarkus": ["packages/nx-quarkus/src/index.ts"],
      "nxrocks/nx-spring-boot": ["packages/nx-spring-boot/src/index.ts"]
    }
  }
}

When packaging (nx build common), the external libraries used in @nxrocks/common/testing (like @jest/globals) are still included, even if that package is only imported in test files...

A workaround, is to declare them explicitly as **devDependencies" in the package.json of the publishable library

@nxrocks/common's package.json

  "devDependencies": {
    "@jest/globals": "*"
  },

Is that the appropriate thing to do?

@dpeger
Copy link

dpeger commented Nov 22, 2022

nx-verison: 14.7.5


We're encountering the same issue. The generated package.json for the publishable artifact includes dependencies that are only used in spec-files. Even if we clear the peerDependencies in the lib's package.json, nx will add the testing libraries as peerDependencies to the generated package.json.

I think the spec-files should be ignored by nx's dependency discovery mechanism. There should be no need to include dev dependency information in a publishable artifact.

Adding an option to exclude libs from being included in the generated artifact would be another solution although this manual configuration should not be necessary IMHO.

Relates to/Is duplicated by #4547

@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 May 22, 2023
@wjehring
Copy link

wjehring commented May 22, 2023

Replying to keep this topic active.

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 Nov 19, 2023
@wjehring
Copy link

Replying to keep this topic active.

@github-actions github-actions bot removed the stale label Nov 21, 2023
@FrozenPandaz
Copy link
Collaborator

We have resolved this by changing the way to keep dependencies in sync via an EsLint rule which should allow for you to have dependencies in either dependencies or peerDependencies. Please give that a try. https://nx.dev/nx-api/eslint-plugin/documents/dependency-checks#dependency-checks-rule

Copy link

github-actions bot commented May 3, 2024

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 May 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
Development

No branches or pull requests

8 participants