Using codegen.ts in an ESM project causes cosmiconfig-typescript-loader to throw an "ES modules is not supported" error
See original GitHub issueDescribe the bug
I don’t know if it’s a bug or not, but I’m reporting it just in case. See steps to reproduce below for a more detailed bug/issue explanation.
In short, if you set "type": "module"
in package.json
and use codegen.ts
as a config file, graphql-codegen
fails because of ESM error by cosmiconfig-typescript-loader
.
If you use codegen.yml
or delete "type": "module"
from package.json
, the error will not occur.
Your Example Website or App
Not yet.
Steps to Reproduce the Bug or Issue
- Add
"type": "module"
inpackage.json
- Run
npx graphql-code-generator init
and leave everything as default to createcodegen.ts
- Run generated codegen npm script
graphql-codegen --config codegen.ts
. It throws the following error:
TypeScriptLoader failed to compile TypeScript:
Must use import to load ES Module: /path/to/project/codegen.ts
require() of ES modules is not supported.
require() of /path/to/project/codegen.ts from /path/to/project/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /path/to/project/package.json.
It maybe a bug in cosmiconfig-typescript-loader
.
Expected behavior
Code generation completes without throwing an error.
Screenshots or Videos
No response
Platform
- OS: Linux x86_64
- NodeJS: 18.8.0
- “typescript”: “4.8.4”
- “graphql”: “16.6.0”
- “@graphql-codegen/cli”: “2.13.7”
- “@graphql-codegen/typescript”: “2.7.4”
- “@graphql-codegen/typescript-resolvers”: “2.7.4”
Codegen Config File
No response
Additional context
a sample tsconfig.json
{
"compilerOptions": {
"outDir": "./dist",
"target": "es2021",
"lib": ["es2021"],
"module": "Node16",
"moduleResolution": "Node16",
"esModuleInterop": true
},
"include": ["src"]
}
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:16
Top Results From Across the Web
ES modules not supported · Issue #1291 · vercel/pkg - GitHub
I'm getting the following error as soon as the compiled app boots: node:internal/modules/cjs/loader:930 throw err; ^ Error: Cannot find module ...
Read more >ESM TypeScript Usage - GraphQL Code Generator
If you choose to use ESM in your application (especially Node.js), you might hit a lot of pitfalls and incompatibility issues within libraries....
Read more >Error [ERR_REQUIRE_ESM]: require() of ES Module ... not ...
The current version of node-fetch is ONLY compatible with an ESM import (using import ), not from CommonJS modules using require() .
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
This code works in CommonJS modules, but will fail in ES modules because relative import paths need to use extensions. As a result,...
Read more >CommonJS vs. ES modules in Node.js - LogRocket Blog
CommonJS modules load using require() , and variables and functions export from a CommonJS module with module.exports . The ES module format ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As a workaround and in the case you absolutely want to use a
codegen.ts
config you can generate ayml
config from the typescript config:Then add the following script in your package.json :
This worked for me.
having the same issue I just switched my codegen to a
yml
format instead ofts
so it doesn’t use theTypeScriptLoader
which usesrequire
to load the codegen file. just a workaround for now, coming from the apollo docs it’d be great if they “just worked”, but yml will do for now 😃