-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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: require() doesn't support default
exports in ES Modules
#4742
Comments
Packages should not export different things from The main/module fields in the package.json describe the entrypoint of the package in ESM and CJS. It doesn't depend on the way you require/import it. If if would depend on it, it would cause duplication i. e. if you require a module in one file and import it in another file. So in my opinion this is working correctly. But like to hear more opinions... I know this is bad for package author because this could mean they need to break the API when adding a ESM export. i. e. when previously exporting a function with |
That makes sense, but in reality neither of those options is ideal. You wouldn't expect anyone to use something like this: var webpack = require('webpack').default; //<--
webpack(...);
//or
var webpack = require('webpack');
webpack.webpack(...); //<-- almost ok, but awkward It defeats the purpose of having a separate echo 'var ofi = require("object-fit-images"); ofi()' > src.js; webpack src.js dist.js
echo 'import ofi from "object-fit-images"; ofi()' > src.js; webpack src.js dist.js |
You're perfectly right here, always read one file regardless of which import/require method is used. So how about flattening the
var ofi = require("object-fit-images");
ofi(); into: var ofi = require("object-fit-images");
ofi = 'default' in ofi ? ofi['default'] : ofi;
ofi(); |
Fixes #71 Context: webpack/webpack#4742
module
instead of main
default
exports in ES Modules
interopDefault happens when The purpose of the
|
I already agreed with that in my previous comment. What I'm suggesting is the equivalent of const webpack = require('webpack').default;
Now we could even agree that the current situation makes perfect technical sense, but this kills interoperability: it either results in poor APIs ( |
I just ran into this with I'm considering reverting the change to use ES2015 exports because of this, it's now a bad experience for end-users. 😓 |
More context: http://2ality.com/2017/01/babel-esm-spec-mode.html#an-es-module-can-only-default-import-commonjs-modules For library authors: As a workaround, you could:
The first two choices will be explicit and won't depend on tooling (but they'll need to be documented) |
After releasing 7.3.0, we've seen folks run into issues (#221) with the new `module` field added to package.json. I've tried to read up on how webpack uses this field, and it looks like it will pick it up by default. This is tricky because most people will expect a CommonJS export (i.e. `module.exports = ...`), but now they suddenly have to import react-waypoints using const Waypoint = require('react-waypoint').default; There's a lengthy issue over at webpack discussing this [1], with a few proposed solutions at the bottom: - offer a separate package like lodash does (lodash-es), or - skip the module field and use a es-specific entry point, like import a from 'yourpkg/es' - skip the module field, avoid es modules (my preferred solution for most of my modules) I'm opting for the second point here. We can use the commonjs bundle as the default, and then if people are adventurous they can point at the es module directly: import Waypoint from 'react-waypoint/build/index.mjs'; We're not really going to the bottom of this issue with this commit. But I believe it will solve headaches for a lot of applications. For instance, I just found out that one developer in my team spent time chasing down a bug where parts of the application wouldn't work. Waypoints that were imported with `import Waypoint from 'react-waypoint'` were working, but not the ones being imported via `const Waypoint = require('react-waypoint')` (we have some legacy). Fixes #221 [1] webpack/webpack#4742
Big.js just released an update that added esmodule support by adding a "module" field to their package.json. By default, webpack will use this field when requiring a module. Since esmodules aren't fully supported in the browser and we don't do any transpiling this PR configures webpack to not consider the esmodule field. [Webpack `mainFields` docs](https://webpack.js.org/configuration/resolve/#resolve-mainfields) [Why `require('big.js')` is broken for us](webpack/webpack#4742) Fixes #1363 License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
Big.js just released an update that added esmodule support by adding a "module" field to their package.json. By default, webpack will use this field when requiring a module. Since esmodules aren't fully supported in the browser and we don't do any transpiling this PR configures webpack to not consider the esmodule field. [Webpack `mainFields` docs](https://webpack.js.org/configuration/resolve/#resolve-mainfields) [Why `require('big.js')` is broken for us](webpack/webpack#4742) Refs ipfs/js-ipfs#1363 Note that this solves the issue for `big.js`, but also for any other modules in the future that decide to define a `module` field in their `package.json` or any that already do and we just haven't realised it yet! License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
…typeorm#4788) TypeORM uses require() within PlatformTools to load driver packages. The typeorm-aurora-data-api-driver is exported with export default and packaged with rollup, which, as per webpack/webpack#4742, causes the require'd module to be available via require('typeorm-aurora-data-api-driver').default property.
…#4788) (#5302) TypeORM uses require() within PlatformTools to load driver packages. The typeorm-aurora-data-api-driver is exported with export default and packaged with rollup, which, as per webpack/webpack#4742, causes the require'd module to be available via require('typeorm-aurora-data-api-driver').default property.
anyway to fixes this in webpack??? |
There is objection, https://www.bignerdranch.com/blog/default-exports-or-named-exports-why-not-both/ maybe webpack better to provide a optionMap to config each node module are default export or named export. |
✅ ➡️ Edit 2020 for Webpack users:
import
nowadays. Only userequire
if the package is not an ES Module.Do you want to request a feature or report a bug?
Bug
What is the current behavior?
In webpack 1.x, this picks up the CommonJS file defined in
main
.In webpack 2.x, this loads the
module
file and setsofi = {default: realOfi}
, which means that users have to use the unnaturalconst ofi = require('object-fit-images').default
. ExampleIf the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
import
is for ES Modules and it should try to read themodule
file if it exists. This works correctly.require
is for CJS files and it should only read themain
entry point, not themodule
one. If it does, it should at least flattendefault
wherever possible.Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
node 7.8.0
webpack 2.4.1
OSX 10.11.6
The text was updated successfully, but these errors were encountered: