# require-esmodule

> require a compiled es6 module and handle exports.default

Latest version **1.0.1** (published 2019-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install require-esmodule
pnpm add require-esmodule
yarn add require-esmodule
bun add require-esmodule
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-09-07 |
| First published | 2019-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6 |
| Dependencies | 0 |
| Unpacked size | 5.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | require-esmodule, reuqire, esm, esmodule, export default, default |

## Links

- npm: https://www.npmjs.com/package/require-esmodule
- Repository: https://github.com/kaelzhang/require-esmodule
- Homepage: https://github.com/kaelzhang/require-esmodule#readme
- Issues: https://github.com/kaelzhang/require-esmodule/issues
- npm.io page: https://npm.io/package/require-esmodule

## Recent versions

- 1.0.1 (latest) — 2019-09-07
- 1.0.0 — 2019-09-07

## 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)
<!-- optional appveyor tst
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/kaelzhang/require-esmodule?branch=master&svg=true)](https://ci.appveyor.com/project/kaelzhang/require-esmodule)
-->
<!-- optional npm version
[![NPM version](https://badge.fury.io/js/require-esmodule.svg)](http://badge.fury.io/js/require-esmodule)
-->
<!-- optional npm downloads
[![npm module downloads per month](http://img.shields.io/npm/dm/require-esmodule.svg)](https://www.npmjs.org/package/require-esmodule)
-->
<!-- optional dependency status
[![Dependency Status](https://david-dm.org/kaelzhang/require-esmodule.svg)](https://david-dm.org/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)

---
_Source: https://npm.io/package/require-esmodule · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
