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

Cannot run codegen CLI command on .cts or .cjs files (CommonJS) #2437

Closed
Macludde opened this issue Jun 22, 2022 · 4 comments
Closed

Cannot run codegen CLI command on .cts or .cjs files (CommonJS) #2437

Macludde opened this issue Jun 22, 2022 · 4 comments

Comments

@Macludde
Copy link
Contributor

Macludde commented Jun 22, 2022

I have a project which uses Vite and thus also uses type:"module" in its package.json.

Error description

When running npx @rtk-query/codegen-openapi I get the following error:

const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:/.../src/api/generationConfig.ts
require() of ES modules is not supported.
require() of /.../src/api/generationConfig.ts from /.../node_modules/@rtk-query/codegen-openapi/lib/bin/cli.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 /.../package.json.

Reason for error

The above error is caused by the config file being inside the package.json project whilst also having type: "module" specified. One fix to get around this is declaring generationConfig.ts as generationConfig.cts instead to tell it to use CommonJS imports instead of ESM.

However, this doesn't work with @rtk-query/codegen-openapi as in the cli file, on line 46, the regex for checking if the path given is to a correct file is as follows
/\.(jsx?|tsx?|jsonc?)?$/
This doesn't match to files ending in .cjs or .cts

Possible solution

An easy fix would be to change the regex to
/\.(c?(jsx?|tsx?)|jsonc?)?$/

@philjones88
Copy link

Hit this issue too. Trying to make private NPM packages of our generated files to save having multiple copies all over the place.

@Macludde
Copy link
Contributor Author

@philjones88
Copy link

@Macludde I've updated to that package but still hit the issue:

/Users/philjones/Documents/<redacted>/node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/philjones/Documents/<redacted>/openapi-config.ts
require() of ES modules is not supported.
require() of /Users/philjones/Documents/<redacted>/openapi-config.ts from /Users/philjones/Documents/<redacted>/node_modules/@rtk-query/codegen-openapi/lib/bin/cli.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 /Users/philjones/Documents/<redacted>/package.json.

    at createErrRequireEsm (/Users/philjones/Documents/<redacted>/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users/philjones/Documents/<redacted>/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/philjones/Documents/<redacted>/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at run (/Users/philjones/Documents/<redacted>/node_modules/@rtk-query/codegen-openapi/src/bin/cli.ts:59:26)
    at Object.<anonymous> (/Users/philjones/Documents/<redacted>/node_modules/@rtk-query/codegen-openapi/src/bin/cli.ts:53:3)
    at Module._compile (node:internal/modules/cjs/loader:1126:14) {
  code: 'ERR_REQUIRE_ESM'
}

@Macludde
Copy link
Contributor Author

Your config file needs to be a .cts files and not .ts (and .cjs instead of .js). The change introduced by the PR above simply expanded the regex check to allow for .cts and .cjs files for the config.

In a regular project all .ts and .js files are automatically marked as CommonJS but when "type": "module" is used in the
package.json, it is instead marked as using ES modules.

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

No branches or pull requests

2 participants