require-esmodule

1.0.1 • Public • Published

Build Status Coverage

require-esmodule

require a compiled es6 module and handle exports.default.

Install

$ npm i require-esmodule

Usage

// foo.js
Object.defineProperty(exports, '__esModule', {value: true})
 
exports.default = {
  foo: 'default-foo'
}
 
exports.foo = 'foo'
// bar.js
module.exports = {
  default: {
    bar: 'default-bar'
  },
  bar: 'bar'
}
// baz.js
module.exports = null
// 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

const foo = requireModule('./foo', false)

is equivalent to:

import * as foo from './foo'

while

const foo = requireModule('./foo')

is equivalent to:

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

getExports(exports: any, requireDefault?)

Detect and get the real exports from the return value of require(id)

License

MIT

Package Sidebar

Install

npm i require-esmodule

Weekly Downloads

6

Version

1.0.1

License

MIT

Unpacked Size

5.41 kB

Total Files

5

Last publish

Collaborators

  • kael