execute-esm-export

Execute an ESM export

Usage no npm install needed!

<script type="module">
  import executeEsmExport from 'https://cdn.skypack.dev/execute-esm-export';
</script>

README

execute-esm-export

Travis CI Greenkeeper badge NPM Version NPM Downloads MIT

Execute an ESM export

Node.js version support

This module officially requires node.js 13.3.0 or above. It may be possible to use with older versions of node.js but --experimental-modules would need to be set. Issues will only be fixed if present in versions of node.js where ES modules are unflagged.

Writing callable exports

// exports.mjs
export function named(...args) {
  console.log('named function called with args:', args);
}

export async function rejection(...args) {
  console.log('rejection function called with args:', args);
  throw new Error('This async (Promise) rejection will be caught and reported');
}

export default function (...args) {
  console.log('default function called with args:', args);
}

Executing

The arguments pattern is execute-esm-export [exportName [...args]]. If [...args] are being provided then the exportName must also be provided.

npx execute-esm-export exports.mjs
# -> default function called with args: []

npx execute-esm-export exports.mjs default arg1 arg2
# -> default function called with args: [ 'arg1', 'arg2' ]

npx execute-esm-export exports.mjs named arg1 arg2
# -> named function called with args: [ 'arg1', 'arg2' ]