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

pkg.main, pkg.module are incompatible, so require("es6-error") behaves differently under Rollup/Webpack #42

Open
andersk opened this issue Aug 29, 2020 · 0 comments

Comments

@andersk
Copy link

andersk commented Aug 29, 2020

In Node, require("es6-error") returns the function ExtendableError, but in Rollup or Webpack, require("es6-error") returns a module { default: ExtendableError }. This means an es6-error dependent can’t be compatible with both environments without contortions.

$ echo 'console.log(require("es6-error"))' > src.js
$ npm i quickselect webpack webpack-cli
$ node src.js
[Function: ExtendableError]
$ npx rollup src.js -p node-resolve -p commonjs -f cjs -o out.js
$ node out.js
[Object: null prototype] { default: [Function: ExtendableError] }
$ npx webpack -d
$ node dist/main.js 
Object [Module] { default: [Function: ExtendableError] }

This is because Node is using pkg.main (./lib/index), while Rollup and Webpack are using pkg.module (./es6/index.js), and the two files do not provide compatible interfaces.

Three potential solutions are:

  • disable babel-plugin-add-module-exports, so require("es6-error").default would work everywhere; or
  • add ExtendableError.default = ExtendableError for compatibility; or
  • remove pkg.module.
@andersk andersk changed the title pkg.main, pkg.module are incompatible, so require("es6-error") behaves differently under Webpack pkg.main, pkg.module are incompatible, so require("es6-error") behaves differently under Rollup/Webpack Aug 29, 2020
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

No branches or pull requests

1 participant