importing mjs fails in node
laat opened this issue · comments
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"
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
I'm pretty happy with tsup
tsup src --format cjs,esm --clean --sourcemap
instead of or in conjunction with tsc
That looks cool. vite is also another option since openapi3-ts depends on vitest. I don't have a special opinion.