import-esm
Import ECMAScript modules in the safe way
Motivation
ECMAScript modules are the official standard format to package JavaScript code for reuse.
Static import is supported since Node.js@8.5.0
, you can't use import foo from 'foo'
on Node.js@<8.5.0
.
Dynamic import is supported since Node.js@9.6.0
, which means we will get an SyntaxError like: SyntaxError: Unexpected token import
on Node.js@<9.6.0
if you use code like import(url)
. And you can't avoid this by putting import(url)
in a try/catch
block.
Install
yarn add import-esm # or npm install import-esm
Usage
const importEsm = ;async { try console // If your engine support ECMAScript Modules //=> [Module] { ... } catch error console // If your engine doesn't support ECMAScript Modules //=> Error: ECMAScript Modules are not supported. ... }
API
importEsm(url)
Import a Module
from url
.
Returns a Promise<Module>
.
url
Type: string
The module url you want import.
importEsm.check()
Check the ECMAScript Modules support.
Returns a Promise<boolean>
.
const importEsm = ;async { console //=> true}
importEsm.checkSync()
The cached result of ECMAScript Modules support check.
Returns a boolean | ''
.
Note: Since there is no sync way to detect ECMAScript Modules support, this method returns ''
(empty string), if we don't know support result yet. **
To get the real result, you have to compare importEsm.checkSync()
with boolean
const importEsm = const isEsmSupported = importEsm if isEsmSupported === true // ECMAScript Modules are supported. else if isEsmSupported === false // ECMAScript Modules are not supported. else // We don't know yet.
This method is design for easier use in case like this:
const importEsm = const importModule = async { try return await catch error // Runs on every time } const importModule = async { if importEsm // Please make sure your engine knows `import()` syntax return importurl try return await catch error // Runs only if we don't know support result yet }
Known Issue(s)
- On
Node.js@<=0.10
, you need polyfillPromise
yourself. - ESM support in Node.js is experimental, though enabled by default in Node.js 13. You will see messages like
ExperimentalWarning: The ESM module loader is experimental
in console. These are emitted by Node.js, not from this module.