How to correctly use ES6 "export default" with CommonJS "require"? How to correctly use ES6 "export default" with CommonJS "require"? javascript javascript

How to correctly use ES6 "export default" with CommonJS "require"?


To use export default with Babel, you can do 1 of the following:

  1. require("myStuff").default
  2. npm install babel-plugin-add-module-exports --save-dev

Or 3:

//myStuff.jsvar thingToExport = {};Object.defineProperty(exports, "__esModule", {  value: true});exports["default"] = thingToExport;


If someone using the gulp + browserify + babelify to bundle js using in client-end.

Try following code [gulpfile.js]:

browserify({  entries: "./ui/qiyun-ui/javascripts/qiyun-ui.src.js",  standalone: "qyUI" // To UMD}).transform(babelify, {  presets: ["env"],  plugins: ["add-module-exports"] // export default {} => module.exports = exports['default'];}).bundle()

Don't forget to install this package:https://www.npmjs.com/package/babel-plugin-add-module-exports