nx
nx copied to clipboard
@nrwl/node:build package.json not including all external dependencies.
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
- create nx workspace.
- create new application using @nrwl/node.
- 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}`)
})
- In workspace.json set
project.targets.build.options.generatePackageJson = true
. - Change build location to apps/app_name/build .
- Run nx r sample-api:serve or nx r sample-api:build .
- 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
I have the same issue with a lot more packages like nestjs config, gcp storage, luxon
@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:
-
yarn create nx-workspace test-express --preset=express
-
cd test-express
-
yarn add morgan
- Add
morgan
tomain.ts
- Add
generatePackageJson: true
tobuild
options -
nx build
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.
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',
},
};
};
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.
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
@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> {
...
@andreidragu hmm - it should only add the the packages that are used in your code ๐ค (it does so for us at least).
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"
}
I am having the same issue with pg
not being included in the dist package.json. It is used in a repositories library
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!
@ferliesypmh I had to use a workaround to solve this...
export * from 'pg';
I added this line in the main.ts
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.
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.
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. ๐
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! ๐
Up
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! ๐
Still an issue