-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Converting ESM to CJS: module wrapped twice #2023
Comments
Did you name your file to end with .mjs/.mts/.cts? These extensions will actually make that param being set to 1. |
Oh that's a good catch, I realize I did that in the first place so that I can export both for ESM and CommonJS: {
"exports": {
".": {
"import": "./src/index.js",
"require": "./dist/index.js"
},
"./foo": {
"import": "./src/foo.js",
"require": "./dist/foo.js"
}
}
} I can fix the issue by duplicating Do you think there's a better way to achieve that with esbuild? |
The simplest way to prevent unexpected // esm/foo.js
export const foo = 'foo'
export default { foo } // never use other things as default Another approach is you can manually write a cjs wrapper to "fix" the exports: // package.json
"exports": {
"import": "./esm/foo.js",
"require": "./shim/foo-wrapper.cjs"
}
// dist/foo.js
(generated by esbuild --format=cjs, like having:)
exports.default = 'foo'
// shim/foo-wrapper.cjs (written by yourself)
module.exports = require('../dist/foo.js').default |
That makes a lot of sense, thank you @hyrious! 🙏 |
@valeriangalliat i recently encountered the same issue. if you're still using |
Due to how esbuild works, setting type: module will cause default imports to be wrapped incorrectly. See evanw/esbuild#2023 for more information. The easiest fix seems to be remove "type: module" from the package.json, which makes esbuild behave properly again
Due to how esbuild works, setting type: module will cause default imports to be wrapped incorrectly. See evanw/esbuild#2023 for more information. The easiest fix seems to be remove "type: module" from the package.json, which makes esbuild behave properly again
Hey! I'm trying to convert ESM files to CJS using esbuild (0.14.22) and I'm running into an issue when they reference each other:
node_modules/.bin/esbuild src/*.js --outdir=dist --format=cjs
When running
node dist/index.js
this logs{ default: [Getter] }
where I would expect it to befoo
(so it is consistent to the output of runningsrc/index.js
).FWIW the built
dist/index.js
is:If I set
isNodeMode
to 0 in the__toESM
function call, then the code checks the__esModule
that's present in the imported module and gets thedefault
property instead of exposing the whole{ default: ... }
object, but I couldn't figure how to get esbuild to not setisNodeMode
to 1. Is that possible?Thanks so much for a great tool and for your help! ❤️
The text was updated successfully, but these errors were encountered: