wrong tsconfig causing 'ERR_REQUIRE_ESM` because in index.js tsc compiles imports to use `require()` despite tsconfig "module" set to "node16"

Viewed 32

What's the right tsconfig to get this to play nice?

I have a nodejs package published to NPM with not much in it (mostly proof of concept)

npmjs link: https://www.npmjs.com/package/package-for-dave

github repo: https://github.com/sebastiangug/package-for-dave

When attempting to import it into a simple application and compile, it throws:

[ERR_REQUIRE_ESM]: require() of ES Module

for good reason because TSC does indeed appear to compile the imports to "require" if you look into the lib/index.js in that github repo.

my tsconfig of the package I'm building:

{
  "compilerOptions": {
    "target": "es6",
    "sourceMap": true,
    "module": "node16",
    "esModuleInterop": false,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "outDir": "lib",
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "declaration": true,
    "moduleResolution": "Node",
    "allowSyntheticDefaultImports": false
  }
}

I am indeed setting "type": "module" in package.json as well.

What the correct tsconfig that needs to be configured to build out the project in a way that works with ESM?

** PROGRESS EDIT.

changing the package module and target to "esnext" does indeed build out an index.js file that contains import {} statements instead of require.

But the exact same error persists when it comes to the consuming nestjs application.

repository link:

https://github.com/sebastiangug/dave-test-app

Unfortunately if I change the target/module there to esnext, it will throw errors about the core nestjs modules not being ESM modules, so that's a no-go.

0 Answers
Related