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

@nrwl/node:build package.json not including all external dependencies. #6901

Open
rs-r2d2 opened this issue Aug 31, 2021 · 19 comments
Open

@nrwl/node:build package.json not including all external dependencies. #6901

rs-r2d2 opened this issue Aug 31, 2021 · 19 comments
Assignees
Labels
scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@rs-r2d2
Copy link

rs-r2d2 commented Aug 31, 2021

Current Behavior

Code contains two dependencies

  • express
  • morgan

On serve or build. The generated package.json does not include morgan as a dependency, only express is included in package.json

project.targets.build.options.generatePackageJson = true

{
  "name": "sample-api",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.17.1"
  },
  "main": "main.js"
}

Expected Behavior

I expected the output of package.json to include morgan as a dependency.


{
  "name": "sample-api",
  "version": "0.0.1",
  "dependencies": {
    "express": "^4.17.1",
    "morgan": "version"
  },
  "main": "main.js"
}

Steps to Reproduce

  1. create nx workspace.
  2. create new application using @nrwl/node.
  3. create a express server with morgan middleware
import * as express from 'express'
import * as morgan from 'morgan'

const app = express()

app.use(morgan('common'))
app.get('/', (req ,res) => {
  return res.status(200).json({ message:'sample api'})
})

const port = 3000 || process.env.APP_PORT

app.listen(port, () => {
  console.log(`App listening on http://localhost:${port}`)
})

  1. In workspace.json set project.targets.build.options.generatePackageJson = true .
  2. Change build location to apps/app_name/build .
  3. Run nx r sample-api:serve or nx r sample-api:build .
  4. check apps/app_name/build/package.json . package.json does not include morgan as dependency.

Project entry in workspace.json

 "sample-api": {
      "root": "apps/sample-api",
      "sourceRoot": "apps/sample-api/src",
      "projectType": "application",
      "targets": {
        "build": {
          "executor": "@nrwl/node:build",
          "outputs": ["{options.outputPath}"],
          "options": {
            "progress": true,
            "generatePackageJson": true,
            "outputPath": "apps/sample-api/dist",
            "main": "apps/sample-api/src/main.ts",
            "tsConfig": "apps/sample-api/tsconfig.app.json",
            "assets": ["apps/sample-api/src/assets"]
          },
          "configurations": {
            "production": {
              "optimization": true,
              "extractLicenses": true,
              "inspect": false,
              "fileReplacements": [
                {
                  "replace": "apps/sample-api/src/environments/environment.ts",
                  "with": "apps/sample-api/src/environments/environment.prod.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "executor": "@nrwl/node:execute",
          "options": {
            "buildTarget": "sample-api:build"
          }
        },
        "lint": {
          "executor": "@nrwl/linter:eslint",
          "options": {
            "lintFilePatterns": ["apps/sample-api/**/*.ts"]
          }
        },
        "test": {
          "executor": "@nrwl/jest:jest",
          "outputs": ["coverage/apps/sample-api"],
          "options": {
            "jestConfig": "apps/sample-api/jest.config.js",
            "passWithNoTests": true
          }
        }
      }
    },

This issue may not be prioritized if details are not provided to help us reproduce the issue.

Failure Logs

N/A

Environment

node 14
ubuntu LTS 20

@leosvelperez leosvelperez added the scope: node Issues related to Node, Express, NestJS support for Nx label Sep 3, 2021
@Mistic92
Copy link

I have the same issue with a lot more packages like nestjs config, gcp storage, luxon

@nartc
Copy link
Contributor

nartc commented Nov 11, 2021

@Mistic92 yours is probably similar to #5820.

@rishabh-husky Could you please provide a reproduce sample?

I do the following:

  • yarn create nx-workspace test-express --preset=express
  • cd test-express
  • yarn add morgan
  • Add morgan to main.ts
  • Add generatePackageJson: true to build options
  • nx build
    image

@Fuco1
Copy link

Fuco1 commented Nov 21, 2021

I have the same problem. I use 11 dependencies but only 6 are listed in the package.json. I do not understand how it works but it seems only those directly imported in main.ts are saved. If I add a transitive dependency directly to main.ts it appears in the package.json.

By transitive dependency I just mean another file in the same application.

I'm using 13.2.1 which is the latest at the moment.

@marbemac
Copy link

marbemac commented Jan 12, 2022

In case it's helpful, we use a custom webpack config + the generate-package-json-webpack-plugin plugin, which has proven more robust than nx implementation in our testing.

Our webpack.config.js looks something like this (we put our "apps" in a services folder, adjust accordingly):

const path = require('path');
const GeneratePackageJsonPlugin = require('generate-package-json-webpack-plugin');

module.exports = config => {
  /**
   * The main entry for services will always look something like `{root}/services/{serviceName}/src/main.ts`
   * We can take advantage of this convention to find the service package.json file
   */
  const entry = config.entry.main[0];
  const packageJSON = require(path.join(entry.split('/src/')[0], 'package.json'));

  packageJSON.dependencies = {
    ...(packageJSON.dependencies || {}),

    // common implicit deps across all packages. service specific implicit deps should go into their respective package.json file
    graphql: '~15.5.0',
  };

  /**
   * The first plugin is `ForkTsCheckerWebpackPlugin` - we do not need to do type checking as part of the dev server.
   * We have other checks for this. Removing this plugin significantly reduces the resource consumption of services during development.
   */
  config.plugins.shift();

  return {
    ...config,

    plugins: [...config.plugins, new GeneratePackageJsonPlugin(packageJSON)],

    // skip minimizing but still tree shake
    optimization: {
      ...config.optimization,

      minimize: false,

      // Non-prod uses webpack cacheUnaffected setting, which cannot be used with usedExports. So only set when building for prod
      usedExports: process.env.NODE_ENV === 'production',
    },
  };
};

@moterink
Copy link

I am having the same issue - it seems like some dependencies, especially those from libs are not included in the generated package.json. This makes containerizing a single application really difficult.

@Jacse
Copy link

Jacse commented Mar 21, 2022

This also applies to node:webpack. We have an app that uses type-graphql and also has pg installed to use postgres, but Nx isn't able to infer the usage and I haven't figured out how to include it as an implicit dependency

@andreidragu
Copy link

@marbemac your solution is nice but it adds a lot of unnecessary packages to package.json and I find it difficult to keep only the used ones in an automated manner.

My workaround is to add export * from 'pg'; in main.ts file

...
import { AppModule } from './app.module';

// workaround to have "pg" added in package.json from dist
export * from 'pg';

async function bootstrap (): Promise<void> {
...

@marbemac
Copy link

@andreidragu hmm - it should only add the the packages that are used in your code 🤔 (it does so for us at least).

@dukuo
Copy link

dukuo commented Jun 8, 2022

I'm facing the same issue now that I have migrated the workspace to the latest version. None of my apps have generated dependencies in their respective package.json

{
  "name": "demo-grpc-a",
  "version": "0.0.1",
  "dependencies": {},
  "main": "main.js"
}

@Mellywins
Copy link

I am having the same issue with pg not being included in the dist package.json. It is used in a repositories library

@ferliesypmh
Copy link

ferliesypmh commented Aug 5, 2022

I am having the same issue with pg not being included in the dist package.json. It is used in a repositories library

@Mellywins were you able to resolve this issue? i'm using 13.9.4 and i have the same issue. thanks!

@Mellywins
Copy link

@ferliesypmh I had to use a workaround to solve this...
export * from 'pg';
I added this line in the main.ts

@AlexJWayne
Copy link

Thanks @marbemac, that webpack plugin indeed does a much better job!

I'm not sure why this plugin picks up dependencies that NX does not, but definitely seems like a bug.

@bmayen
Copy link

bmayen commented Nov 2, 2022

I was running into this but after deleting root node_modules and package-lock.json and reinstalling, all dependencies were included in the generated package.json as expected.

@kornect
Copy link

kornect commented Feb 15, 2023

I kept getting this issue as well, my work around was to create an import.ts file in the app src folder, then add this file as anadditionalEntryPoints in the project.json

e.g. import.ts imports all required packages for nestjs, excluding angular related packages.

import 'handlebars';
import 'helmet';
import 'nodemailer';
import 'passport';
import 'passport-jwt';
import 'passport-local';
import 'pg';
import 'reflect-metadata';
import 'rxjs'
import 'tslib';
import 'uuid';

const forceImport = () => {
  console.log('forceImport');
};

export default forceImport;

then add this file in the additional entry points in project.json

        "generatePackageJson": true, <--- still required
        "additionalEntryPoints": [
          {
            "entryName": "import",
            "entryPath": "apps/{YOUR_APP}/src/import.ts" <-- file above
          },
        ]
      },

The package.json file gets generated with all the packages you want included with this app, might be improved but this worked for me

You can discard the imports.js that nx generates after your build runs. 😁

@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 Aug 15, 2023
@tonivj5
Copy link

tonivj5 commented Aug 15, 2023

Up

@github-actions github-actions bot removed the stale label Aug 16, 2023
@jaysoo jaysoo self-assigned this Aug 31, 2023
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 Feb 28, 2024
@DerHerrGammler
Copy link

Still an issue

@github-actions github-actions bot removed the stale label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
Development

No branches or pull requests