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 when importing openApiTS function #885

Closed
RomainGuarinoni opened this issue Mar 11, 2022 · 3 comments
Closed

ERR_REQUIRE_ESM when importing openApiTS function #885

RomainGuarinoni opened this issue Mar 11, 2022 · 3 comments

Comments

@RomainGuarinoni
Copy link

RomainGuarinoni commented Mar 11, 2022

When I use the version 5.2.0 of openapi-typescript, there is this error when i try to import the openApiTS function :

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/rguarinoni/Documents/findAMusician/findAMusicianBackend/node_modules/openapi-typescript/dist/index.js
require() of ES modules is not supported.
require() of /home/rguarinoni/Documents/findAMusician/findAMusicianBackend/node_modules/openapi-typescript/dist/index.js from /home/rguarinoni/Documents/findAMusician/findAMusicianBackend/api/command/generateType.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/rguarinoni/Documents/findAMusician/findAMusicianBackend/node_modules/openapi-typescript/package.json.

    at new NodeError (node:internal/errors:363:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (/home/rguarinoni/Documents/findAMusician/findAMusicianBackend/api/command/generateType.ts:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Module.m._compile (/home/rguarinoni/Documents/findAMusician/findAMusicianBackend/node_modules/ts-node/src/index.ts:1371:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1138:10) {
  code: 'ERR_REQUIRE_ESM'
}

I found a way to fix the issue by downgrading to 4.4.0. Additionnaly, i noticed that a big difference was the presence of the dependecy kleur in the package.json which i suspect can be the cause of the issue.

@drwpow
Copy link
Contributor

drwpow commented Mar 11, 2022

Version 5.x is a breaking change from 4.x where ESM is now required. Are you trying to require() this library in Node?

@RomainGuarinoni
Copy link
Author

RomainGuarinoni commented Mar 13, 2022

No I was trying to import the openApiTS function as an ESM like below :

import openApiTS from 'openapi-typescript';

And this error appeared. It's strange ...

@drwpow
Copy link
Contributor

drwpow commented Mar 28, 2022

I’m going to close in favor of #888. If you could document your build tooling, and how you’re compiling TypeScript (e.g. what is your target in tsconfig.json? are you using ts-node? etc.), I’d love additions to the README on using the new ESM version with specific build tooling.

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