https://github.com/kaelzhang/require-esmodule
require a compiled es6 module and handle exports.default
https://github.com/kaelzhang/require-esmodule
es6 esmodule require
Last synced: 9 months ago
JSON representation
require a compiled es6 module and handle exports.default
- Host: GitHub
- URL: https://github.com/kaelzhang/require-esmodule
- Owner: kaelzhang
- License: other
- Created: 2019-09-07T03:32:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-07T04:21:01.000Z (over 6 years ago)
- Last Synced: 2025-07-05T09:16:33.472Z (9 months ago)
- Topics: es6, esmodule, require
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/kaelzhang/require-esmodule)
[](https://codecov.io/gh/kaelzhang/require-esmodule)
# require-esmodule
require a compiled es6 module and handle exports.default.
## Install
```sh
$ npm i require-esmodule
```
## Usage
```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
```
## requireModule(id: string, requireDefault? : boolean = true)
- **id** `string` **ABSOLUTE** path of the module
- **requireDefault?** `boolean=true` whether should require export default. Defaults to `true`.
Returns `any` the module exports
### `requireDefault` as `false`
```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 export**s of es modules, so the default value of `requireDefault` is set to `true`
## getExports(exports: any, requireDefault?)
Detect and get the **real** exports from the return value of `require(id)`
## License
[MIT](LICENSE)