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 adds @angular/{common/core} to Node project #4939

Closed
the-ult opened this issue Mar 3, 2021 · 12 comments
Closed

generatePackageJson adds @angular/{common/core} to Node project #4939

the-ult opened this issue Mar 3, 2021 · 12 comments
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@the-ult
Copy link

the-ult commented Mar 3, 2021

Current Behavior

We've got an Apollo/Express graphql-server and are using "generatePackageJson": true, to generate the package.json for the production build of the graphql-server.

The generated package.json adds @angular/common and @angular/core as dependencies, while it isn't an Angular project. And @angular/... is not used in the project.

Expected Behavior

The generated package.json should not include the @angular/.. dependencies

Steps to Reproduce

The generated package.json

{
  "name": "my-graphql-server",
  "version": "0.0.1",
  "main": "main.js",
  "dependencies": {
    "@angular/common": "11.2.3",  // would not expect the package.json to have these
    "@angular/core": "11.2.3",  // would not expect the package.json to have these
    "apollo-datasource-rest": "0.10.0",
    "apollo-server-express": "2.21.0",
    "compression": "1.7.4",
    "express": "4.17.1",
    "graphql-import-node": "0.0.4",
    "graphql-modules": "1.4.1",
    "graphql-scalars": "~1.8.0",
    "graphql-subscriptions": "1.2.0",
    "graphql": "15.5.0",
    "js-yaml": "4.0.0",
    "log4js": "6.3.0",
    "reflect-metadata": "0.1.13",
    "rxjs": "6.6.6"
  }
}

angular.json

"my-graphql-server": {
      "root": "apps/my-graphql-server",
      "sourceRoot": "apps/my-graphql-server/src",
      "projectType": "application",
      "prefix": "my",
      "architect": {
        "build": {
          "builder": "@nrwl/node:build",
          "outputs": ["{options.outputPath}"],
          "options": {
            "webpackConfig": "apps/my-graphql-server/webpack.config.js",
            "outputPath": "dist/apps/my-graphql-server",
            "main": "apps/my-graphql-server/src/main.ts",
            "tsConfig": "apps/my-graphql-server/tsconfig.app.json",
            "assets": ["apps/my-graphql-server/src/assets"],
            "generatePackageJson": true,
            "buildLibsFromSource": true
          },
          "configurations": {
            "production": {
              "optimization": true,
              "extractLicenses": true,
              "inspect": false,
              "fileReplacements": [
                {
                  "replace": "apps/my-graphql-server/src/environments/environment.ts",
                  "with": "apps/my-graphql-server/src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@nrwl/node:execute",
          "options": {
            "buildTarget": "my-graphql-server:build"
          }
        },
        ...
       }
    },

Environment

❯ nx report

>  NX  Report complete - copy this into the issue template

  Node : 14.15.4
  OS   : darwin x64
  npm  : 6.14.11
  
  nx : Not Found
  @nrwl/angular : 11.4.0
  @nrwl/cli : 11.4.0
  @nrwl/cypress : 11.4.0
  @nrwl/devkit : 11.4.0
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : 11.4.0
  @nrwl/jest : 11.4.0
  @nrwl/linter : 11.4.0
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : 11.4.0
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 11.4.0
  @nrwl/web : Not Found
  @nrwl/workspace : 11.4.0
  typescript : 4.1.5
@juristr juristr added the scope: node Issues related to Node, Express, NestJS support for Nx label Mar 5, 2021
@bekos
Copy link
Contributor

bekos commented Mar 26, 2021

@the-ult it would be really helpful if you can share a repro repo. Can you also check if your issue is solved using 11.6.0-beta.4?

@the-ult
Copy link
Author

the-ult commented Mar 26, 2021

Hi, sharing the repo would be difficult.
Might be able to reproduce it in stackblitz or something..

But I wil try the beta.4 👍

@the-ult
Copy link
Author

the-ult commented Mar 29, 2021

The beta still seems to include the @angular/ libraries and zone.js
Even more than before 🤔

❯ nx build ddb-graphql-server --prod

> nx run ddb-graphql-server:build:production 
❯ nx report

>  NX  Report complete - copy this into the issue template

  Node : 14.16.0
  OS   : darwin x64
  npm  : 6.14.11
  
  nx : Not Found
  @nrwl/angular : 11.6.0-beta.4
  @nrwl/cli : 11.6.0-beta.4
  @nrwl/cypress : 11.6.0-beta.4
  @nrwl/devkit : 11.6.0-beta.4
  @nrwl/eslint-plugin-nx : 11.6.0-beta.4
  @nrwl/express : 11.6.0-beta.4
  @nrwl/jest : 11.6.0-beta.4
  @nrwl/linter : 11.6.0-beta.4
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : 11.6.0-beta.4
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 11.6.0-beta.4
  @nrwl/web : Not Found
  @nrwl/workspace : 11.6.0-beta.4
  @nrwl/storybook : Not Found
  @nrwl/gatsby : Not Found
  typescript : 4.1.5

Screenshot 2021-03-29 at 10 14 04

However.
the environment.[prod].ts are using a model from our shared-module. (but only the model, not the module itself is included. And de model is exported in the index.ts, not in de module.

Screenshot 2021-03-29 at 10 31 20

environment.prod.ts

import { GraphQLEnvironment } from '@ddb/shared/environment';

export const environment: GraphQLEnvironment = {
..
}

The SharedEnvironModule is importing @angular/core, etc, but is not used/imported in the graphql-server

/// This module is NOT imported in the ddb-graphql-server

import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ClientEnvironment } from './environment.model';
import { ENV } from './environment.token';

@NgModule({
  imports: [CommonModule],
})
export class EnvironmentModule {
  static forRoot(environment: Partial<ClientEnvironment>): ModuleWithProviders<EnvironmentModule> {
    return {
      ngModule: EnvironmentModule,
      providers: [{ provide: ENV, useValue: environment }],
    };
  }
}

--
Tested it, with removing the model and it results in a proper package.json

environment.prod.ts

export const environment = {
  ...
}

Screenshot 2021-03-29 at 10 28 18

I figured, since we are only importing the model and not the the SharedEnvironmentModule, there would be no 'connection' to @angular/core. 🤔
Or is that an invalid assumption?

@bekos
Copy link
Contributor

bekos commented Mar 29, 2021

@the-ult Thanks for trying it out and debugging this 👍 This sounds like an issue with our project graph calculation 🤔 tbh, I am not super familiar with that part of the code but if you can create a a minimal repro I can investigate it.

@the-ult
Copy link
Author

the-ult commented Mar 29, 2021

Thanks.
I will try and create a minimal test repo 👍
(might take a couple days)

@the-ult
Copy link
Author

the-ult commented Mar 31, 2021

Hi @bekos
Here is a sample repo: https://github.com/the-ult/nx-node-package-problem

npm run build express-app -- --prod
produces:

{
  "name": "express-app",
  "version": "0.0.1",
  "dependencies": {
    "@angular/core": "~11.2.7",
    "rxjs": "~6.6.3",
    "zone.js": "^0.10.2",
    "@angular/common": "~11.2.7",
    "@angular/platform-browser-dynamic": "~11.2.7",
    "@angular/compiler": "~11.2.7",
    "@angular/platform-browser": "~11.2.7",
    "@angular/animations": "~11.2.7",
    "express": "4.17.1"
  },
  "main": "main.js"
}

Screenshot 2021-03-31 at 20 37 16

the shared-environment is an angular-lib.
Screenshot 2021-03-31 at 20 43 18

the environment.model is imported in both the express-app and sample-ng. But the module is not imported anywhere

❯ nx report

>  NX  Report complete - copy this into the issue template

  Node : 14.16.0
  OS   : darwin x64
  npm  : 6.14.11
  
  nx : Not Found
  @nrwl/angular : 11.6.0
  @nrwl/cli : 11.6.0
  @nrwl/cypress : 11.6.0
  @nrwl/devkit : 11.6.0
  @nrwl/eslint-plugin-nx : 11.6.0
  @nrwl/express : 11.6.0
  @nrwl/jest : 11.6.0
  @nrwl/linter : 11.6.0
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : 11.6.0
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 11.6.0
  @nrwl/web : Not Found
  @nrwl/workspace : 11.6.0
  @nrwl/storybook : Not Found
  @nrwl/gatsby : Not Found
  typescript : 4.0.7

@the-ult
Copy link
Author

the-ult commented Apr 15, 2021

@bekos have you had any chance to try the sample repo?

@bekos
Copy link
Contributor

bekos commented Apr 16, 2021

@the-ult I took a look, and based on the current implementation, I'm afraid there is no workaround, as Nx calculates dependencies based on the relation of the files and not on the actual imports.

An alternative suggestion is to use something like depcheck on the build files to calculate the actual dependencies, or populate the package.json yourself 😞

@the-ult
Copy link
Author

the-ult commented Apr 16, 2021

Thanks for looking into it

@bekos
Copy link
Contributor

bekos commented May 14, 2021

Closing as it is not actionable atm, imo the best approach in these cases is to generate (even manually) the appropriate package.json.

@the-ult
Copy link
Author

the-ult commented Jan 27, 2022

Ok.. found out what caused the problem.

We were using/importing an environment.model.ts from shared/environment which had the Interface for the environment.

import { GraphQLEnvironment } from '@ddb/shared/environment';

export const environment: GraphQLEnvironment = { 
  ...
}

However.. The shared/environment package also has an shared-environment.module.ts and EnvironmentService for our Angular projects.

// shared/environment/src/index.ts
export * from './lib/shared-environment.module';
export * from './lib/environment-versions';
export * from './lib/environment.model';
export { DdbEnvironmentService } from './lib/environment.service';

I was in the (wrong) assumption that only actually used/imported files would be used. But it seems when you import 1 item from the package.. Alle files/dependencies of the index.ts will be used and seen as peerDependency.

Will this not be the case if we use (better) secondary-entrypoints / barrel files?

related to: #6574

@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 23, 2023
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

3 participants