contentful-typescript-codegen icon indicating copy to clipboard operation
contentful-typescript-codegen copied to clipboard

ERR_REQUIRE_ESM in Node 16.6

Open tnzk opened this issue 3 years ago • 5 comments
trafficstars

I've got "ERR_REQUIRE_ESM" in Node v16.6.

$ npm run contentful-typescript-codegen

> [email protected] contentful-typescript-codegen
> contentful-typescript-codegen --output @types/generated/contentful.d.ts

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/getContentfulEnvironment.js from /Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js not supported.
Instead change the require of getContentfulEnvironment.js in /Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_
modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js to a dynamic import() which is available in all CommonJS modules.
    at /Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:425:38
    at step (/Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:68:23)
    at Object.next (/Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:49:53)
    at /Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:42:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:38:12)
    at runCodegen (/Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:419:12)
    at Object.<anonymous> (/Users/tnzk/dev/repo/ORGNAME/PROD_NAME/sveltekit/node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js:455:1) {
  code: 'ERR_REQUIRE_ESM'
}

One cannot require an EJS module, which Node v16 determines it by the extension being .js.

This filename with .js is hard-coded in node_modules/contentful-typescript-codegen/dist/contentful-typescript-codegen.js. I can monkey-patch this, but it would also be helpful if we can specify the path for this in command line.

tnzk avatar Mar 30 '22 15:03 tnzk

I have workarounded this problem by using .cjs as file extension and adding support for it via pnpm patch:

diff --git a/dist/contentful-typescript-codegen.js b/dist/contentful-typescript-codegen.js
index aeb08f170f0783aa9f44abc0a53999e808d5727d..0d1e439fbc070669405d1d18aac3c017bc8c69bc 100755
--- a/dist/contentful-typescript-codegen.js
+++ b/dist/contentful-typescript-codegen.js
@@ -443,6 +443,8 @@ function determineEnvironmentPath() {
     var pathWithoutExtension = path.resolve(process.cwd(), "./getContentfulEnvironment");
     if (fs.existsSync(pathWithoutExtension + ".ts")) {
         return pathWithoutExtension + ".ts";
+    } else if (fs.existsSync(pathWithoutExtension + ".cjs")){
+        return pathWithoutExtension + ".cjs";
     }
     return pathWithoutExtension + ".js";
 }

pmb0 avatar Jul 08 '23 08:07 pmb0

@pmb0 it seems like that doesn't work for me, did you change anything else?

Jdruwe avatar Jan 03 '24 09:01 Jdruwe

@pmb0 it seems like that doesn't work for me, did you change anything else?

What worked for me was:

  1. Save the file with LF new-lines
  2. Load the environment variables with the dotenv package

Hope this helps!

yangavin avatar Jan 15 '24 04:01 yangavin

I opened a PR to apply the above patch on the package, in case you want to follow it: https://github.com/intercom/contentful-typescript-codegen/pull/159

GabeDuarteM avatar Feb 20 '24 19:02 GabeDuarteM

I have a same issue. When will be deploy a new version?

PatrykMaternicki avatar Apr 18 '24 09:04 PatrykMaternicki