-
Notifications
You must be signed in to change notification settings - Fork 162
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
Using codegen.ts in an ESM project causes cosmiconfig-typescript-loader to throw an "ES modules is not supported" error #225
Comments
Hey @tars0x9752, I about to move a project of mine over to ESM and noticed that the generator has a specific section in the documentation on getting ESM working, I'm sure you have already seen it, but just in case: https://the-guild.dev/graphql/codegen/docs/getting-started/esm-typescript-usage I haven't started the ESM migration myself so it's possible additional steps outside of that documentation are needed. The quick version is
A quick (empty) project seems to indicate this works on my end, but again I haven't tested with real project yet. |
Thanks for the reply. As you've guessed, I've already seen that document. The thing is, even that document's exmaple doesn't use As I wrote in my first comment, if I use something other than
Does it work with |
@tars0x9752 I didn't look at the example project, just the documentation on that page. I did notice that you're running the What happens if you run the following: npx graphql-codegen-esm --config codegen.ts I'm not sure what package needs to be installed to use |
Thanks, but I've already tried It looks like But these graphql libraries here such as |
I'm also having the same issue. |
having the same issue I just switched my codegen to a |
I'm now having an error where @graphql-tools/code-file-loader is using require
|
Thanks a lot this solved my problem on a fresh sveltekit install |
Solved with this: |
I'm having this problem as well, had to use a |
It isn't supposed to defeat anything really, just the extension of your codegen config file. |
I need a JavaScript configuration for dotenv. Is there any workaround that allows JavaScript? |
I guess that for now we have no other choice than using the You can follow the instructions here for generating ESM compliant code. If you are generating code from an ESM typescript schema and you have an error of type {
"codegen": "NODE_OPTIONS=\"--loader ts-node/esm\" graphql-codegen-esm --config codegen.yml"
} |
As a workaround and in the case you absolutely want to use a // codegen.ts
import yml from 'yaml';
import type {CodegenConfig} from '@graphql-codegen/cli';
import {writeFileSync} from 'fs';
const config: CodegenConfig = {
schema: 'src/graphql/schemas/schema.ts',
// ...
};
// save config as yml
writeFileSync('codegen.yml', yml.stringify(config));
export default config; Then add the following script in your package.json : {
// Generate codegen.yml then call graphql-codegen-esm
"codegen": "export NODE_OPTIONS=\"--loader ts-node/esm\" && node codegen.ts && graphql-codegen-esm --config codegen.yml"
} This worked for me. |
This is terrifying. Thank you |
Hi, I'm having the same issue but none of the solutions are working for me. I've tried .ts, .yml, .cts config files and
Any other ideas? |
I was having the same error you can run this command to check
|
FYI I've created an issue that covers several issues with using typescript files (codegen and local plugins) with workarounds that might help. |
I also encountered the same error. $ yarn codegen
yarn run v1.22.19
$ graphql-codegen --config codegen.ts
/Users/xxx/xxx/project_nextjs/node_modules/cliui/build/index.cjs:291
const stringWidth = require('string-width');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/xxx/xxx/project_nextjs/node_modules/string-width/index.js from /Users/xxx/xxx/project_nextjs/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /Users/xxx/xxx/project_nextjs/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules. It appears to be an unintentionally installed package due to a dependency. I solved the problem by upgrading the yarn version. $ yarn --version
1.22.19
$ yarn set version 1.3.2
$ yarn --version
1.3.2 |
Same issue in Next.js project (v14). Solved with this: yarn add [email protected] |
@dimitriBouteille I am using these packages: I tried it with your command: But i still get the same error... |
This solved the issue |
Currently afterwards this pops: So you also need a
|
Same issue in Next.js project (v14) & @graphql-codegen/cli v5.0.2. Solved with this: "resolutions": {
"cli-truncate": "^4.0.0",
"wrap-ansi": "^9.0.0",
"listr2": "^8.2.3",
"inquirer": "^10.1.0"
} |
Codegen config loaded now, but client preset it can't find any documents |
Solved with this. This feature enabled by default at the NodeJS 22.12.0 |
Describe 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"
inpackage.json
and usecodegen.ts
as a config file,graphql-codegen
fails because of ESM error bycosmiconfig-typescript-loader
.If you use
codegen.yml
or delete"type": "module"
frompackage.json
, the error will not occur.Your Example Website or App
Not yet.
Steps to Reproduce the Bug or Issue
"type": "module"
inpackage.json
npx graphql-code-generator init
and leave everything as default to createcodegen.ts
graphql-codegen --config codegen.ts
. It throws the following error:It maybe a bug in
cosmiconfig-typescript-loader
.Expected behavior
Code generation completes without throwing an error.
Screenshots or Videos
No response
Platform
Codegen Config File
No response
Additional context
a sample
tsconfig.json
The text was updated successfully, but these errors were encountered: