NX - how can I build and serve node application that depends on ESM libs

94 Views Asked by At

Background

I've got an NX workspace with a react front-end and an express back-end. I want to add some dependencies to the back-end application but am having trouble adding dependencies that are ESM Libs.

When running nx run express-server:serve which uses the @nx/js:node executor I get the following error

Error [ERR_REQUIRE_ESM]: require() of ES Module Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.

Research

I have been reading any related post/thread (see list below) that I can find but I haven't been able to find a solution to this. My understanding is, that this isn't a bug with NX but with the rise of ESM only libs, there must be a workaround for this.

I did make an attempt to update my workspace to ESM but it seems the support for this in NX is lacking and the end result just produced other errors. I would like to continue using NX and be able to support the importing of ESM only libs.

Related Threads

Sample code

nx-express-esm

  • npm i
  • nx run express-server:build (works)
  • nx run express-server:serve (fails with above error)

This is a simple contrived example to demonstrate the problem. I know I could just downgrade nanoid to version 3 but I have other ESM libs that I can't downgrade.

0

There are 0 best solutions below