require a compiled es6 module and handle exports.default
npm install require-esmodule

require a compiled es6 module and handle exports.default.
``sh`
$ npm i require-esmodule
`js
// foo.js
Object.defineProperty(exports, '__esModule', {value: true})
exports.default = {
foo: 'default-foo'
}
exports.foo = 'foo'
`
`js`
// bar.js
module.exports = {
default: {
bar: 'default-bar'
},
bar: 'bar'
}
`js`
// baz.js
module.exports = null
`js
// qux.js
const {
requireModule,
getExports
} = require('require-esmodule')
console.log(requireModule('/path/to/foo').foo) // 'default-foo'
console.log(requireModule('/path/to/foo', false).foo) // 'foo'
console.log(requireModule('/path/to/bar').bar) // 'bar'
// bar.js is not a es6 module
console.log(requireModule('/path/to/baz')) // null
`
- id string ABSOLUTE path of the moduleboolean=true
- requireDefault? whether should require export default. Defaults to true.
Returns any the module exports
`js`
const foo = requireModule('./foo', false)
is equivalent to:
`js`
import * as foo from './foo'
while
`js`
const foo = requireModule('./foo')
is equivalent to:
`js`
import foo from './foo'
The purpose of require-esmodule is to detect and make it easier to get the default exports of es modules, so the default value of requireDefault is set to true
Detect and get the real exports from the return value of require(id)`