tsimportlib
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

Dynamic import within TS compiled to CommonJS

Currently an alpha-quality, best-effort implementation.
May be unnecessary if you use TypeScript's "module": "NodeNext", "moduleResolution": "NodeNext"

Node.js allows dynamic import() calls within CommonJS file. TypeScript transforms import() into require() when targetting CommonJS.

This presents a problem when a .ts file compiled to CommonJS wants to do a truly async, dynamic import of a native ESM module. How do we avoid TypeScript's transformation from import() to require()?

This library implements a workaround. Replace import('lib') with dynamicImport('lib', module).

import {dynamicImport} from 'tsimportlib';

async function main() {
    const dynamicallyImportedEsmModule = await dynamicImport('truly-esm-module', module) as typeof import('truly-esm-module');
}

async function loadPlugin(name: string) {
    // In this example, plugins may or may not be native ESM, so we use dynamic import to support both.
    const dynamicallyImportedPlugin = await dynamicImport(name, module) as MyPluginInterface;
}

Limitations

Due to Node.js limitations, we must use the CommonJS resolver to locate modules. This should work well enough for third-party modules, but may not work for dual-mode modules or ones that suppress importing of their package.json file. As a fallback you can pass an absolute path to the target module.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.5
    7,143
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.5
    7,143
  • 0.0.4
    499
  • 0.0.3
    812
  • 0.0.2
    0
  • 0.0.1
    1

Package Sidebar

Install

npm i tsimportlib

Weekly Downloads

8,455

Version

0.0.5

License

none

Unpacked Size

3.31 kB

Total Files

4

Last publish

Collaborators

  • cspotcode