An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.org/kaelzhang/require-esmodule.svg?branch=master)](https://travis-ci.org/kaelzhang/require-esmodule)
[![Coverage](https://codecov.io/gh/kaelzhang/require-esmodule/branch/master/graph/badge.svg)](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)