New project: generatePackageJson doesn't include tslib as a dependency
See original GitHub issueCurrent Behavior
When a new workspace is created with a node app (eg. express) and the generatePackageJson option is set to true, package.json is missing tslib which is required since the tsconfig.base.json that comes with a new nx workspace uses importHelpers: true. This means that when pulled into a Docker container for deployment or similar, and npm install is run, tslib is excluded, and the build will fail.
Expected Behavior
tslib should be included in the package.json file produced by the generatePackageJson option if importHelpers: true.
Steps to Reproduce
Repo that demonstrates this behaviour: https://github.com/jksaunders/nx-generate-package-json-tslib
To use the example, run npm install then nx build, and observe that the generated package.json in the dist/api folder is missing tslib.
To reproduce from scratch:
npx create-nx-workspace --preset=expressnx g @nrwl/express:app apinx build- observe that the generated
package.jsonin thedist/apifolder is missingtslib.
Failure Logs
When the built file is run in the container (eg. node ./dist/api/main.js) or locally if the top level node_modules are deleted and npm install is run within the dist/api folder then running the built file (eg. node ./dist/api/main.js):
2022-06-29 11:16:12.392 MDT
Error: Cannot find module 'tslib'
2022-06-29 11:16:12.392 MDT
Require stack:
2022-06-29 11:16:12.392 MDT
- /usr/src/app/main.js
2022-06-29 11:16:12.392 MDT
at Module._resolveFilename (node:internal/modules/cjs/loader:946:15)
2022-06-29 11:16:12.392 MDT
at Module._load (node:internal/modules/cjs/loader:787:27)
2022-06-29 11:16:12.392 MDT
at Module.require (node:internal/modules/cjs/loader:1012:19)
2022-06-29 11:16:12.392 MDT
at require (node:internal/modules/cjs/helpers:102:18)
2022-06-29 11:16:12.392 MDT
at Object.tslib (/usr/src/app/main.js:22:18)
2022-06-29 11:16:12.392 MDT
at __webpack_require__ (/usr/src/app/main.js:46:41)
2022-06-29 11:16:12.392 MDT
at /usr/src/app/main.js:59:17
2022-06-29 11:16:12.392 MDT
at /usr/src/app/main.js:98:3
2022-06-29 11:16:12.392 MDT
at Object.<anonymous> (/usr/src/app/main.js:103:12)
2022-06-29 11:16:12.392 MDT
at Module._compile (node:internal/modules/cjs/loader:1112:14) {
2022-06-29 11:16:12.392 MDT
code: 'MODULE_NOT_FOUND',
2022-06-29 11:16:12.392 MDT
requireStack: [ '/usr/src/app/main.js' ]
2022-06-29 11:16:12.392 MDT
}
Environment
> NX Report complete - copy this into the issue template
Node : 18.3.0
OS : darwin x64
npm : 8.11.0
nx : 14.3.6
@nrwl/angular : Not Found
@nrwl/cypress : Not Found
@nrwl/detox : Not Found
@nrwl/devkit : 14.3.6
@nrwl/eslint-plugin-nx : 14.3.6
@nrwl/express : 14.3.6
@nrwl/jest : 14.3.6
@nrwl/js : 14.3.6
@nrwl/linter : 14.3.6
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : 14.3.6
@nrwl/nx-cloud : Not Found
@nrwl/nx-plugin : Not Found
@nrwl/react : Not Found
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : Not Found
@nrwl/web : Not Found
@nrwl/workspace : 14.3.6
typescript : 4.7.4
---------------------------------------
Community plugins:
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)

Top Related StackOverflow Question
@nartc I should clarify, it’s not actually the fault of those packages - it’s from the
importHelpers: truevalue in the top leveltsconfig.base.json, which is the default value in a newly generated Nx Express workspace. Thepackage.jsonin the base generated Nx workspace comes withtsliblisted as a dependency to account for this (I’m assuming) but is not included in the generatedpackage.jsongenerated bygeneratePackageJson, which I believe is not intended behaviour.Setting
importHelpers: falsebuilds.jsfiles that don’t requiretslib, but I’m thinking there must be a way to includetslibas a dependency in the generatedpackage.jsonif the loadedtsconfigat build time hasincludeHelpers: true, which is the current default for Nx Express workspaces. Does that make sense?Also yes, I’m currently installing
tslibseparately in app’s Docker container, that is definitely a usable workaround!@jksaunders Gotcha. I’m afraid in this case, transitive deps of deps aren’t inferrable by Nx (especially when both
fastifyandmecuriusdo not specifytslibanywhere in theirpackage.json).What you can do to workaround this is to install
tslibseparately in the container. Eg in a DockerfileWould this be sufficient enough?