# tsimportlib

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

Latest version **0.0.5** (published 2023-05-16) · 0 weekly downloads

## Install

```sh
npm install tsimportlib
pnpm add tsimportlib
yarn add tsimportlib
bun add tsimportlib
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2023-05-16 |
| First published | 2021-06-05 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Andrew Bradley |
| Maintainers | cspotcode |
| Keywords | dynamic import, import, typescript, commonjs, esm, async, modules |

## Links

- npm: https://www.npmjs.com/package/tsimportlib
- Homepage: https://github.com/cspotcode/tsimportlib
- Issues: https://github.com/cspotcode/tsimportlib/issues
- npm.io page: https://npm.io/package/tsimportlib

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2023-05-16
- 0.0.4 — 2023-04-21
- 0.0.3 — 2021-06-05
- 0.0.2 — 2021-06-05
- 0.0.1 — 2021-06-05

## README

## 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)`.

```typescript
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.

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