metadevpro / openapi3-ts

TS Model & utils for creating and exposing OpenAPI 3.x contracts.
MIT License
485 stars 64 forks source link

Cannot import `oas31` using ESM in `openapi3-ts@4.1.1` #114

Closed invakid404 closed 1 year ago

invakid404 commented 1 year ago

While trying to upgrade to openapi3-ts@4.1.1 in my project, I suddenly started getting this error on all my imports:

import { oas31 } from 'openapi3-ts';
         ^^^^^
SyntaxError: Named export 'oas31' not found. The requested module 'openapi3-ts' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'openapi3-ts';
const { oas31 } = pkg;

I'm guessing this is happening because my project is ESM. It used to work fine with openapi3-ts@4.0.4.

I'm using TypeScript 5.0.4, and my tsconfig.json looks like this:

{
  "compilerOptions": {
    "lib": ["ESNext", "dom"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "outDir": "dist"
  }
}
koooge commented 1 year ago

Hi, "moduleResolution": "node16", would work. We need to update the README.

refs: https://github.com/metadevpro/openapi3-ts/pull/111

invakid404 commented 1 year ago

What if I want to use NodeNext though? Switching to Node16 is not an option for my project.

koooge commented 1 year ago

You can use it since it is the exact same. https://www.typescriptlang.org/tsconfig#moduleResolution

invakid404 commented 1 year ago

But if it's the exact same, why wouldn't it work in the first place? I'm confused :D

koooge commented 1 year ago

Yours is not nodenext -> "moduleResolution": "Node",.