Use default exports and named exports at the same time in CommonJS.
npm install default-exports
function aFunc() {}
function bFunc() {}
function cFunc() {}
module.exports = require("default-exports")({
aFunc,
bFunc,
cFunc
});
In another module:
const mainFunc = require("./myModule");
console.log(mainFunc); // aFunc() {}
const { aFunc, bFunc, cFunc } = require("./myModule");
console.log(aFunc, bFunc, cFunc); // aFunc() {}, bFunc() {}, cFunc() {}
Export provided values as named exports and set the first item as the default export.
Type: object
An object of values to export, the first one to use as the default export.
Export provided value as default export and named exports.
Type: any
The function to use as the default export.
Type: object
Default: {}
Key-value pairs of the named exports.
Since default-exports
relys on object-based transforms, to set a value which is not based on an object such as strings or numbers, use their classes:
defaultExports(new String("A string"), otherExports);
defaultExports(new Number(12), otherExports);