Reputation: 251
I've a monorepo using nx with multiple node/nestjs apps. Some of the apps doesn't require all the packages used in the other apps. Because it's a monorepo, I need to install all packages for every apps during the deployment.
Is there a way generate a package.json on build that would contain only the packages needed for the app that I'm building?
I've tryed to use "generate-package-json-webpack-plugin" to generate the package.json, but it only detect half the dependencies.
I've also tried to build a single js file containing all the apps, but it doesn't seem to work and always require tslib.
Upvotes: 22
Views: 31101
Reputation: 41
The easiest way I found is to just add the generatePackageJson
option to the plugins
array in webpack.config.js
and it will generate a package.json
and package-lock.json
files with the dependencies you have for that specific app.
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join } = require('path');
module.exports = {
output: {
path: join(__dirname, '../../dist/apps/your-app'), // change path as needed
},
plugins: [
new NxAppWebpackPlugin({
target: 'node',
compiler: 'tsc',
main: './src/main.ts',
tsConfig: './tsconfig.app.json',
assets: ['./src/assets'],
generatePackageJson: true // <- Right here
})
],
};
Which is similar to what is written on the Nx documentation only I wanted to use the tsc
compiler rather than swc
.
For more info you can check out the documentation part that helped me on this here.
Hope this will be helpful to anyone in the future entering this thread!
Upvotes: 0
Reputation: 4603
Following up on Shlomi's answer - as workspace.json
is now deprecated, generatePackageJson
option moved to @nx/webpack:webpack package so a modern (as of 2023) way to make it work is to either:
nx.json
:{
"targetDefaults": {
"build": {
"executor": "@nx/webpack:webpack",
"options": {
"generatePackageJson": true
},
// ...
},
// ...
},
// ...
}
project.json
:{
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"options": {
"generatePackageJson": true
},
//...
},
// ...
}
// ...
}
package.json
:{
"name": "...",
"scripts": {
// ...
},
"nx": {
"targets": {
"build": {
"executor": "@nx/webpack:webpack",
"options": {
"generatePackageJson": true
},
},
// ...
},
// ...
},
// ...
}
Upvotes: 6
Reputation: 3315
After I look at the nx source code I found the answer.
Set generatePackageJson
to true
in workspace.json
where <project-name>/targets/build/options
.
This will generate you package.json with the necessary dependencies for your app.
Here example:
"node-api": {
"root": "apps/node-api",
"sourceRoot": "apps/node-api/src",
"projectType": "application",
"prefix": "node-api",
"targets": {
"build": {
"executor": "@nrwl/node:build",
"outputs": ["{options.outputPath}"],
"options": {
"showCircularDependencies": false,
"outputPath": "dist/apps/node-api",
"main": "apps/node-api/src/main.ts",
"tsConfig": "apps/node-api/tsconfig.app.json",
"assets": ["apps/node-api/src/assets"],
"generatePackageJson": true <----------------------
},
....
Upvotes: 29
Reputation: 1170
Nx encourages a single-version policy and has a single package.json.
If the issue is that you are installing all the dependencies every time in CI before building then you might need to rely on the functionality provided by your CI system to cache these between runs - a lot of existing CI systems do provide these: * Gitlab: https://docs.gitlab.com/ee/ci/caching/ * CircleCI: https://circleci.com/docs/2.0/caching/ * Travis: https://docs.travis-ci.com/user/caching/
However this comes with its own set of issues (e.g. parallel jobs where one or more is changing the dependencies).
We can try to explore having a command in Nx: sort of a "affected:dep-install" that will detect which packages to install as part of the affected command. Please create an issue for it here: https://github.com/nrwl/nx/issues
Upvotes: 0