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

importing mjs fails in node #115

Closed
laat opened this issue Apr 18, 2023 · 3 comments · Fixed by #116
Closed

importing mjs fails in node #115

laat opened this issue Apr 18, 2023 · 3 comments · Fixed by #116

Comments

@laat
Copy link

laat commented Apr 18, 2023

package.json

{
  "type": "module",
  "devDependencies": {
    "openapi3-ts": "^4.1.1"
  }
}

app.mjs

import x from 'openapi3-ts/oas31';
const builder = new x.OpenApiBuilder();
console.log(builder.rootDoc);

it fails to import when running node app.mjs

(node:59840) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
.pnpm/openapi3-ts@4.1.1/node_modules/openapi3-ts/dist/mjs/oas31.js:1
export * from './dsl/openapi-builder31';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1195:20)
    at Module._compile (node:internal/modules/cjs/loader:1239:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Module._load (node:internal/modules/cjs/loader:972:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:165:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)

Node.js v19.9.0

It's probably because the published package.json does not have "type": "module", or that the published files does not end with ".mjs"

@koooge
Copy link
Contributor

koooge commented Apr 19, 2023

Since cjs would not work with type module, the file ext should be .mjs. Also, tsc doesn't seem to be able to change file ext. microsoft/TypeScript#49462

@laat
Copy link
Author

laat commented Apr 19, 2023

I'm pretty happy with tsup

tsup src --format cjs,esm --clean --sourcemap instead of or in conjunction with tsc

@koooge
Copy link
Contributor

koooge commented Apr 19, 2023

That looks cool. vite is also another option since openapi3-ts depends on vitest. I don't have a special opinion.

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

Successfully merging a pull request may close this issue.

2 participants