contentful-typescript-codegen
contentful-typescript-codegen copied to clipboard
ERR_REQUIRE_ESM in Node 16.6
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.
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 it seems like that doesn't work for me, did you change anything else?
@pmb0 it seems like that doesn't work for me, did you change anything else?
What worked for me was:
- Save the file with LF new-lines
- Load the environment variables with the dotenv package
Hope this helps!
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
I have a same issue. When will be deploy a new version?