# ts-import

> Import (compile and cache on the fly) TypeScript files dynamically with ease.

Latest version **5.0.0-beta.1** (published 2025-02-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-import
pnpm add ts-import
yarn add ts-import
bun add ts-import
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0-beta.1 |
| Published | 2025-02-19 |
| First published | 2020-07-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 3 |
| Unpacked size | 58.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Artur Kurowski |
| Maintainers | radarsu |
| Keywords | compile, dynamic, import, require, typescript |

## Links

- npm: https://www.npmjs.com/package/ts-import
- Homepage: https://github.com/radarsu/ts-import#readme
- npm.io page: https://npm.io/package/ts-import

## Dependencies (3)

- [tslib](https://npm.io/package/tslib.md) 2.5.0
- [comment-parser](https://npm.io/package/comment-parser.md) 1.3.1
- [options-defaults](https://npm.io/package/options-defaults.md) 2.0.40

## 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

- 5.0.0-beta.1 (latest) — 2025-02-19
- 0.0.34-4 (pnpm-temp) — 2020-08-15
- 5.0.0-beta.0 — 2023-04-11
- 4.0.0-beta.10 — 2022-12-08
- 4.0.0-beta.9 — 2022-12-07
- 4.0.0-beta.8 — 2022-11-26
- 4.0.0-beta.7 — 2022-11-26
- 4.0.0-beta.6 — 2022-07-09
- 4.0.0-beta.5 — 2022-07-09
- 4.0.0-beta.4 — 2022-07-09
- 4.0.0-beta.3 — 2022-07-09
- 4.0.0-beta.2 — 2022-06-17
- 4.0.0-beta.1 — 2022-06-15
- 4.0.0-beta.0 — 2022-06-15
- 3.0.0-beta.4 — 2022-06-15
- … 74 more at https://npm.io/package/ts-import/versions

## README

<p align="center">
    <h1>ts-import</h1>
    <div>Importing TypeScript files dynamically into JavaScript requires additional compilation step, which is troublesome to write for many. Popular `typescript-require` package seems to be obsolete and doesn't allow much customization. Typed alternative to https://github.com/theblacksmith/typescript-require written in TypeScript.</div>
</p>

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/radarsu)

## Table of contents

- [Table of contents](#table-of-contents)
- [Features](#features)
- [Install](#install)
- [Usage](#usage)
  - [allowConfigurationWithComments](#allowconfigurationwithcomments)

## Features

-   **Asynchronous and synchronous version** - uses **import** for async and **require** for sync.
-   **Caches JavaScript** files into directory inside **.cache/ts-import**.
-   **Fast** - I've benchmarked ways to compare detecting file changes with **fs** module and checking mtimeMs turned out to be fastest (https://jsperf.com/fs-stat-mtime-vs-mtimems). Also, compilation in versions 3+ is approximately 10x faster than in version 2.
-   **Few dependencies** - uses only `comment-parser` and my tiny utility package `options-defaults`.
-   **Highly flexible and configurable** - all compilerOptions are available under transpileOptions parameter.
-   **No interference** - doesn't interfere with native import, require etc. changing their behavior or impacting their performance.

## Install

`npm i ts-import@4` - CJS
`npm i ts-import@5` - ESM

## Usage

```ts
import * as tsImport from 'ts-import';

const main = async () => {
    const filePath = `/home/user/file.ts`;
    const asyncResult = await tsImport.load(filePath, {
        // allowConfigurationWithComments: false,
    });

    // Only available in version 4.
    const syncResult = tsImport.loadSync(filePath);
};

void main();
```

### allowConfigurationWithComments

You can define if file should be imported in the default `transpile` mode or `compile` mode by placing a comment on top of the specific file.

Compile mode is slower, but allows the specified file to be part of a complex program - it can import other files etc.

```ts
/**
 * @tsImport
 * { "mode": "compile" }
 */

import { getOtherVariable } from './get-other-variable';

const result = getOtherVariable();

export { result };
```

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