Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_REQUIRE_ESM in Node 16.6 #116

Open
tnzk opened this issue Mar 30, 2022 · 5 comments · May be fixed by #159
Open

ERR_REQUIRE_ESM in Node 16.6 #116

tnzk opened this issue Mar 30, 2022 · 5 comments · May be fixed by #159

Comments

@tnzk
Copy link

tnzk commented Mar 30, 2022

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.

@pmb0
Copy link

pmb0 commented Jul 8, 2023

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";
 }

@Jdruwe
Copy link

Jdruwe commented Jan 3, 2024

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

@yangavin
Copy link

@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!

GabeDuarteM added a commit to GabeDuarteM/contentful-typescript-codegen that referenced this issue Feb 20, 2024
GabeDuarteM added a commit to GabeDuarteM/contentful-typescript-codegen that referenced this issue Feb 20, 2024
@GabeDuarteM GabeDuarteM linked a pull request Feb 20, 2024 that will close this issue
GabeDuarteM added a commit to GabeDuarteM/contentful-typescript-codegen that referenced this issue Feb 20, 2024
@GabeDuarteM
Copy link

I opened a PR to apply the above patch on the package, in case you want to follow it: #159

@PatrykMaternicki
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants