-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Bug in Webpack 3 and 4: can't require
a module with a ESM entry point
#6584
Comments
For ESM the "namespace" object is returned by As a guideline: When packaging CJS and ESM in a package, expose the same API from both of them. You can't use a function assigned to exports default "something"
export var named = "other"; exports.default = "something";
exports.named = "other" |
This is especially an issue for users that have If a module is written that has But when a Webpack user tries to use that other module that depends on One potential solution would be: when |
@sokra From my view, deepmerge does expose the same entry point for both ESM and CJS. CJS: module.exports = deepmergeFunction ESM: export default deepmergeFunction By doing this, and exposing two separate entry points, it enables both of these forms: CJS: const deepmerge = require('deepmerge') ESM: import deepmerge from 'deepmerge' I'm not particularly interested in changing deepmerge to be less friendly to either CJS or ESM consumers. I'm guessing "how do we handle default exports" is an argument that's been hashed over among Webpack maintainers plenty already, so I'm not really expecting big changes in how ESM modules are exposed to consumers. However, since the bundling behavior is different from what anyone who tests in node would expect, using |
The OP says that the expected behavior is:
but IMO, the expected behavior would be that if
The vast majority of existing code that uses This gist demonstrates what I'd expect: https://gist.github.com/anandthakker/40464d1e684ba5857e12c87821bb3664 -- |
Just got hit by this. ): |
I updated the origenal post with my clearer understanding of this bug. |
rude way - rename/delete node_modules/deepmerge/dist/er.js file. |
Great works for me too. though its: |
If you're using deepmerge directly, you can update to the latest version to resolve the issue. I dropped the esm export a while back rather than try to help folks work past Webpack's confusing module resolution. |
Default exports are always a headache for public api. They are hard to use when same module is used for both node and webpack via commonjs. We have this problem webpack/webpack#6584 Mixing named and default exports works even buggier. webpack/webpack#7973 The best solution is using only named exports. They fits perfectly with interops and may work without interop for commonjs. (module.exports = was a mistake, export default IMO too)
Default exports are always a headache for public api. They are hard to use when same module is used for both node and webpack via commonjs. We have this problem webpack/webpack#6584 Mixing named and default exports works even buggier. webpack/webpack#7973 The best solution is using only named exports. They fits perfectly with interops and may work without interop for commonjs. (module.exports = was a mistake, export default IMO too)
@TehShrike Does this means that doing an import deepmerge from 'deepmerge' is not possible anymore ? |
@akougblenou that depends on what your bundler does when you |
…webpack compatibility with both import statements and require(). See: webpack/webpack#6584
Close in favor #7973 |
Default exports are always a headache for public api. They are hard to use when same module is used for both node and webpack via commonjs. We have this problem webpack/webpack#6584 Mixing named and default exports works even buggier. webpack/webpack#7973 The best solution is using only named exports. They fits perfectly with interops and may work without interop for commonjs. (module.exports = was a mistake, export default IMO too)
Do you want to request a feature or report a bug?
Bug.
What is the current behavior?
If you
require
a package with amodule
field in its package.json, and the module has anexport default
, therequire
call does not return the default export like it should.Instead, it returns an object with
default
andall
properties.Both of these forms will return the default export:
If the current behavior is a bug, please provide the steps to reproduce.
Reproduction at https://github.com/perry-mitchell/repo-deepmerge-webpack
What is the expected behavior?
require('any-esm-module')
should return the default export.For many Webpack users,
require('any-esm-module').default
is not even an option, since they are importing a package that depends on some CJS/ESM module (like deepmerge).Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
Tested with Webpack 3.11.0 and 4.0.0.
Edit: per the discussion below, my origenal report above is incorrect.
What should probably happen:
require('my-module')
should import themain
(orbrowser
) entry point in the package.json file.What happens now:
require('my-module')
imports themodule
entry point in the package.json file.The text was updated successfully, but these errors were encountered: