nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.29k stars 2.32k forks source link

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

Closed rs-r2d2 closed 1 month ago

rs-r2d2 commented 3 years ago

Current Behavior

Code contains two dependencies

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

4. In workspace.json set `project.targets.build.options.generatePackageJson = true` . 
5. Change build location to apps/app_name/build .
6.  Run nx r sample-api:serve or nx r sample-api:build .
7. check  apps/app_name/build/package.json . package.json does not include morgan as dependency.
<!-- Can you reproduce this on https://github.com/nrwl/nx-examples? -->
<!-- If so, open a PR with your changes and link it below. -->
<!-- If not, please provide a minimal Github repo -->
<!-- At the very least, provide as much detail as possible to help us reproduce the issue -->

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 } } } },



<!-- Remove this line -->
This issue may not be prioritized if details are not provided to help us reproduce the issue.

### Failure Logs
<!-- Please include any relevant log snippets or files here. -->
N/A
### Environment
<!-- It's important for us to know the context in which you experience this behavior! -->
<!-- Please paste the result of `nx report` below! -->
node 14
ubuntu LTS 20
Mistic92 commented 2 years ago

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

nartc commented 2 years ago

@Mistic92 yours is probably similar to https://github.com/nrwl/nx/issues/5820.

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

I do the following:

Fuco1 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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

dukuo commented 2 years ago

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 commented 2 years ago

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

ferliesypmh commented 2 years ago

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 commented 2 years ago

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

AlexJWayne commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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[bot] commented 1 year ago

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

tonivj5 commented 1 year ago

Up

github-actions[bot] commented 6 months ago

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

DerHerrGammler commented 6 months ago

Still an issue

jaysoo commented 1 month ago

I'm closing this issue as the way we pick up dependencies and update package.json has changed.

Here's a repo to show that it is working in the latest Nx: https://github.com/jaysoo/issue-6901

If you have further problems, please open a new issue with a repro.

github-actions[bot] commented 2 days ago

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.