question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New project: generatePackageJson doesn't include tslib as a dependency

See original GitHub issue

Current 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=express
  • nx g @nrwl/express:app api
  • nx build
  • observe that the generated package.json in the dist/api folder is missing tslib.

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:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jksaunderscommented, Aug 2, 2022

@nartc I should clarify, it’s not actually the fault of those packages - it’s from the importHelpers: true value in the top level tsconfig.base.json, which is the default value in a newly generated Nx Express workspace. The package.json in the base generated Nx workspace comes with tslib listed as a dependency to account for this (I’m assuming) but is not included in the generated package.json generated by generatePackageJson, which I believe is not intended behaviour.

Setting importHelpers: false builds .js files that don’t require tslib, but I’m thinking there must be a way to include tslib as a dependency in the generated package.json if the loaded tsconfig at build time has includeHelpers: true, which is the current default for Nx Express workspaces. Does that make sense?

Also yes, I’m currently installing tslib separately in app’s Docker container, that is definitely a usable workaround!

0reactions
nartccommented, Aug 2, 2022

@jksaunders Gotcha. I’m afraid in this case, transitive deps of deps aren’t inferrable by Nx (especially when both fastify and mecurius do not specify tslib anywhere in their package.json).

What you can do to workaround this is to install tslib separately in the container. Eg in a Dockerfile

FROM node:14.17-slim

WORKDIR /app

COPY ./dist/apps/api .

RUN npm install --production
# install transitive deps
RUN npm install tslib

EXPOSE 3333

CMD ["node", "./main.js"]

Would this be sufficient enough?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generate package.json on nx build / deployment
This will generate you package.json with the necessary dependencies for your app. Here example: "node-api": { "root": "apps/node ...
Read more >
Angular package format
As of APF v10, it is recommended to add tslib as a direct dependency of your primary entry-point. This is because the tslib...
Read more >
tslib direct dependency migration - Angular Hispano
If you have any libraries within your workspace, this migration will convert tslib peer dependencies to direct dependencies for the libraries.
Read more >
cannot read properties of undefined (reading 'map') angular
I'm trying to use the eslint command on my angular project in the terminal to ... at /Users/xyz/project-x/node_modules/tslib/tslib.js:118:75 at new Promise ...
Read more >
@Nrwl/Community
Is it possible to have dependencies scoped to a library? or is that an an. ... show that particular project as red… but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found