Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

jiti

unjs65.5mMIT2.4.2TypeScript support: included

Runtime typescript and ESM support for Node.js

readme

jiti

npm version npm downloads bundle size

This is the active development branch. Check out jiti/v1 for legacy v1 docs and code.

🌟 Used in

Docusaurus, ESLint, FormKit, Histoire, Knip, Nitro, Nuxt, PostCSS loader, Rsbuild, Size Limit, Slidev, Tailwindcss, Tokenami, UnoCSS, WXT, Winglang, Graphql code generator, Lingui, Scaffdog, Storybook, ...UnJS ecosystem, ...60M+ npm monthly downloads, ...6M+ public repositories.

✅ Features

  • Seamless TypeScript and ESM syntax support for Node.js
  • Seamless interoperability between ESM and CommonJS
  • Asynchronous API to replace import()
  • Synchronous API to replace require() (deprecated)
  • Super slim and zero dependency
  • Custom resolve aliases
  • Smart syntax detection to avoid extra transforms
  • Node.js native require.cache integration
  • Filesystem transpile with hard disk caches
  • ESM Loader support
  • JSX support (opt-in)

[!IMPORTANT] To enhance compatibility, jiti >=2.1 enabled interopdefault using a new Proxy method. If you migrated to 2.0.0 earlier, this might have caused behavior changes. In case of any issues during the upgrade, please report so we can investigate to solve them. 🙏🏼

💡 Usage

CLI

You can use jiti CLI to quickly run any script with TypeScript and native ESM support!

npx jiti ./index.ts

Programmatic

Initialize a jiti instance:

// ESM
import { createJiti } from "jiti";
const jiti = createJiti(import.meta.url);

// CommonJS (deprecated)
const { createJiti } = require("jiti");
const jiti = createJiti(__filename);

Import (async) and resolve with ESM compatibility:

// jiti.import(id) is similar to import(id)
const mod = await jiti.import("./path/to/file.ts");

// jiti.esmResolve(id) is similar to import.meta.resolve(id)
const resolvedPath = jiti.esmResolve("./src");

If you need the default export of module, you can use jiti.import(id, { default: true }) as shortcut to mod?.default ?? mod.

// shortcut to mod?.default ?? mod
const modDefault = await jiti.import("./path/to/file.ts", { default: true });

CommonJS (sync & deprecated):

// jiti() is similar to require(id)
const mod = jiti("./path/to/file.ts");

// jiti.resolve() is similar to require.resolve(id)
const resolvedPath = jiti.resolve("./src");

You can also pass options as the second argument:

const jiti = createJiti(import.meta.url, { debug: true });

Register global ESM loader

You can globally register jiti using global hooks. (Important: Requires Node.js > 20)

import "jiti/register";

Or:

node --import jiti/register index.ts

🎈 jiti/native

You can alias jiti to jiti/native to directly depend on runtime's import.meta.resolve and dynamic import() support. This allows easing up the ecosystem transition to runtime native support by giving the same API of jiti.

⚙️ Options

debug

  • Type: Boolean
  • Default: false
  • Environment variable: JITI_DEBUG

Enable verbose logging. You can use JITI_DEBUG=1 <your command> to enable it.

fsCache

  • Type: Boolean | String
  • Default: true
  • Environment variable: JITI_FS_CACHE

Filesystem source cache (enabled by default)

By default (when is true), jiti uses node_modules/.cache/jiti (if exists) or {TMP_DIR}/jiti.

Note: It is recommended that this option be enabled for better performance.

moduleCache

  • Type: String
  • Default: true
  • Environment variable: JITI_MODULE_CACHE

Runtime module cache (enabled by default).

Disabling allows editing code and importing the same module multiple times.

When enabled, jiti integrates with Node.js native CommonJS cache-store.

transform

  • Type: Function
  • Default: Babel (lazy loaded)

Transform function. See src/babel for more details

sourceMaps

  • Type: Boolean
  • Default false
  • Environment variable: JITI_SOURCE_MAPS

Add inline source map to transformed source for better debugging.

interopDefault

  • Type: Boolean
  • Default: true
  • Environment variable: JITI_INTEROP_DEFAULT

Jiti combines module exports with the default export using an internal Proxy to improve compatibility with mixed CJS/ESM usage. You can check the current implementation here.

alias

  • Type: Object
  • Default: -
  • Environment variable: JITI_ALIAS

You can also pass an object to the environment variable for inline config. Example: JITI_ALIAS='{"~/*": "./src/*"}' jiti ....

Custom alias map used to resolve IDs.

nativeModules

  • Type: Array
  • Default: ['typescript']
  • Environment variable: JITI_NATIVE_MODULES

List of modules (within node_modules) to always use native require() for them.

transformModules

  • Type: Array
  • Default: []
  • Environment variable: JITI_TRANSFORM_MODULES

List of modules (within node_modules) to transform them regardless of syntax.

importMeta

Parent module's import.meta context to use for ESM resolution. (only used for jiti/native import).

tryNative

  • Type: Boolean
  • Default: Enabled if bun is detected
  • Environment variable: JITI_TRY_NATIVE

Try to use native require and import without jiti transformations first.

jsx

  • Type: Boolean | {options}
  • Default: false
  • Environment Variable: JITI_JSX

Enable JSX support using @babel/plugin-transform-react-jsx.

See test/fixtures/jsx for framework integration examples.

Development

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run pnpm dev
  • Run pnpm jiti ./test/path/to/file.ts

License

Published under the MIT license. Made by @pi0 and community 💛

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v2.4.2

compare changes

🩹 Fixes

  • cache: Add +map suffix to fs entries when sourceMaps enabled (#352)
  • Use native require cache of loaded entries only (#348)

🏡 Chore

✅ Tests

  • Simplify snapshot tests (#351)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.4.1

compare changes

🩹 Fixes

  • Interop modules with primitive default export (#343)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.4.0

compare changes

🚀 Enhancements

  • Support generic type for jiti.import<T> (#331)

🩹 Fixes

  • Try to resolve .ts files with .js extension from js files (#337)

🏡 Chore

🤖 CI

❤️ Contributors

v2.3.3

compare changes

🩹 Fixes

  • eval: Return fallback value (#326)

💅 Refactors

  • Remove some unused exports (#327)

❤️ Contributors

v2.3.2

compare changes

🩹 Fixes

  • eval: Fallback in async mode (#325)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.3.1

compare changes

🩹 Fixes

  • Conditional access to mod.default (8c30a94)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.3.0

compare changes

🚀 Enhancements

  • Support jiti.import(id, {default: true}) (#323)

🩹 Fixes

  • interopDefault: Avoid in operator for primitive inputs (#321)

💅 Refactors

  • interopDefault: Simplify logic for default export checks (#322)

📖 Documentation

  • Fix format (#320)
  • Update interopDefault description and reference (61891a0)
  • Add note about interop default (537fa39)

🏡 Chore

❤️ Contributors

v2.2.1

compare changes

🩹 Fixes

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.2.0

compare changes

🚀 Enhancements

  • Use smarter proxy for interopDefault (#318)

💅 Refactors

  • Inline interopDefault from mlly (8826047)

🤖 CI

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.1.2

compare changes

🌊 Types

  • Use local NodeModule type (718bea2)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.1.1

compare changes

🩹 Fixes

  • types: Add standalone types for node require (#316)

🏡 Chore

✅ Tests

  • Add dependency tests (1d86ca3)
  • Ignore deps for node native register (e7ffe04)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.1.0

compare changes

🚀 Enhancements

  • Enable interopDefault by default (#310)
  • Support import.meta.dirname and import.meta.filename (#308)

🔥 Performance

  • cli: Enable node 22 compile cache (#312)

🩹 Fixes

  • Make TransformOptions type strict to allow auto-complete (#305)
  • Properly handle tsx (#311)

💅 Refactors

  • Deprecate commonjs api (#313)

📦 Build

  • Fix /register and /native subpath types for Node10 module resolution (#304)

🏡 Chore

❤️ Contributors

v2.0.0

compare changes

🚀 Enhancements

  • Handle data: imports (non-native only) (#299)
  • Support jsx (#200)
  • Eval esm modules with fallback loader (#300)
  • Support import.meta.resolve (#301)

🩹 Fixes

  • Handle global url instance mismatch (#298)
  • Optional access to Reflect.metadata (#165)
  • Add only paths option to native require.resolve (50e4280)

💅 Refactors

  • Make jiti.esmResolve consistent with import.meta.resolve (#303)

📖 Documentation

  • Add example for inline JITI_ALIAS (a53715a)

🏡 Chore

❤️ Contributors

v2.0.0-rc.1

compare changes

🚀 Enhancements

  • jiti/native export (#289)
  • Improve jiti/native compatibility with node and deno (#294)

💅 Refactors

  • Improve internal babel types (#271)
  • Always use native impl for jiti/native (#293)
  • Rename experimentalBun to tryNative (#295)

📦 Build

  • Fix type resolution issue (#269)

🏡 Chore

❤️ Contributors

v2.0.0-beta.3

compare changes

🚀 Enhancements

  • Allow try and other resolve options for import/esmResolve (#268)
  • Allow set interopDefault using JITI_INTEROP_DEFAULT env (1c080a1)

🔥 Performance

  • Use native createRequire (69da3c5)

🩹 Fixes

  • cache: Prefer node_modules/.cache if exists (832f206)
  • Use native esm import for built-ins (54d6b4a)
  • Respect interopDefault in babel transform (485b4e9)
  • Split cache based on interopDefault (f820a15)
  • Remove ext from cache path (50b1b3a)
  • Proprly resolve .mts/.cts with .mjs/.cjs imports (a5aefad)
  • resolve: Make sure parentURL is a dir (d224e84)

💅 Refactors

  • Improve debug logging (463a8a3)
  • Rename importResolve toesmResolve` (aac88e6)
  • Improve env handling (ee4489d)
  • Use imporr/require in debug logs (934a5bb)

🏡 Chore

✅ Tests

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.0-beta.2

compare changes

🚀 Enhancements

  • Add experimental esm loader support (#266)

🔥 Performance

  • Reduce overhead of sub jiti instances (#265)

🩹 Fixes

  • Resolve with esm conditions in async context (#264)

💅 Refactors

  • Use more clear fsCache and moduleCache options (#263)
  • Use esm imports for babel plugins (22e259f)

📦 Build

🏡 Chore

🤖 CI

  • Enable loader test (#267)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.0-beta.1

compare changes

🚀 Enhancements

  • Basic top-level await support (#239)
  • Native esm support (#259)

🩹 Fixes

  • Use distinct cache paths for async mode (6e8ec7a)

💅 Refactors

  • Split option normalization (#172)
  • Split logic (#240)
  • Remove legacy node syntax polyfills (#260)
  • 3rd arg to createJiti is optional (60a23e3)
  • Upgrade cache version to 8 (99224ae)

📖 Documentation

  • Update bundlephobia link (#179)

🏡 Chore

🤖 CI

❤️ Contributors

v1.21.3

compare changes

🩹 Fixes

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.21.2

compare changes

🩹 Fixes

  • Pin mlly to 1.4.2 (#237)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.21.1

compare changes

🏡 Chore

🤖 CI

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.21.0

compare changes

🚀 Enhancements

  • Add jiti.import function for async import (#170)
  • Add forward compatible (stub) types for jiti.import (#175)

🏡 Chore

❤️ Contributors

v1.20.0

compare changes

🚀 Enhancements

  • Experimental native bun support (#156)

💅 Refactors

  • Replace proposal babel plugins with ecmascript transforms (1fb4d8c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.19.3

compare changes

🩹 Fixes

  • Only replace import.meta.env (c6895d6)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.19.2

compare changes

🩹 Fixes

  • Pass custom extensions to esm resolver (#152)
  • Support import.meta.env and import.meta.env?.prop (#159)

🏡 Chore

✅ Tests

  • typescript: Remove type-only namespace access (de73bd9)

🎨 Styles

  • Format with prettier v3 (fe61c6e)
  • Format report with prettier (6098284)

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Anhao

v1.19.1

compare changes

🩹 Fixes

  • Read cache from evalOptions (2c1765e)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.19.0

compare changes

🚀 Enhancements

  • Expose jiti.evalModule (#146)

🩹 Fixes

  • Handle nested circular dependencies (#142)
  • types: Export JITIOptions and TreansformOptions (#132)

📖 Documentation

🏡 Chore

✅ Tests

  • Mask node version in snapshots (64cf136)

❤️ Contributors

v1.18.2

compare changes

🚀 Enhancements

  • Allow resolving .ts files with .js extension (#128)
  • Support import.meta.env (#129)

🔥 Performance

  • Use extension resolutions only for parent typescript files (27a9888)

🩹 Fixes

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Db3f994 <Pooya Parsa>
  • Guillaume Chau (@Akryum)

v1.17.2

compare changes

🩹 Fixes

  • Add support to emit decorator metadata (#119)
  • Use inline require cache to avoid circular dependencies (#125)
  • Workaround for pnpm and TMPDIR (#123)

🏡 Chore

✅ Tests

  • Add typescript satisfies fixture (#107)

🎨 Styles

❤️ Contributors

v1.17.1

compare changes

🩹 Fixes

  • Prefer require condition first in esm resolve mode (#117)

💅 Refactors

  • Use mkdirSync from node:fs instead of mkdirp (413d3c3)

🏡 Chore

❤️ Contributors

1.17.0 (2023-02-08)

Features

  • add support for .mts (#112) (94ab3f2)
  • support import assertions and .json imports with .default property (#114) (08a9a47)

1.16.2 (2023-01-10)

1.16.1 (2023-01-03)

1.16.0 (2022-09-19)

Features

  • support export namespace from syntax (resolves #84) (ebb59cf)

Bug Fixes

1.15.0 (2022-09-06)

Features

  • basic alias support (resolves #37) (cab50cc)
  • options for nativeModules and transformModules (64151af)

Bug Fixes

  • manually exclude typescript from transpilation (dbd3f22)
  • use pathe for path resolution (000c6ad), closes #74

1.14.0 (2022-06-20)

Features

  • detect native esm using type: module (#67) (986f146)

Bug Fixes

  • use inlined import meta plugin to inject url (#68) (b52bb17)

1.13.0 (2022-02-18)

Features

  • support inline sourceMaps for easier debugging (#46) (888db00)

Bug Fixes

  • use backslash to make import maps working in windows (e8696c7)

1.12.15 (2022-01-28)

Bug Fixes

  • force transpile config package (2ddcb8a), closes #56

1.12.14 (2022-01-26)

Bug Fixes

  • move esm resolve behind a flag (60e094c)

1.12.13 (2022-01-25)

1.12.12 (2022-01-25)

Bug Fixes

  • ensure resolve esm id exists (2d44274)

1.12.11 (2022-01-25)

Bug Fixes

  • default _filename if null or falsy value passed (1a24f2a)
  • pkg: do not mangle dist build (3b456e1)

1.12.10 (2022-01-25)

Bug Fixes

  • support resolving with import condition (#52) (1e1bb0c)

1.12.9 (2021-10-18)

Bug Fixes

  • inline mlly.interopDefault (resolves #48) (32e606f)

1.12.8 (2021-10-18)

Bug Fixes

1.12.7 (2021-10-12)

1.12.6 (2021-10-02)

Bug Fixes

  • avoid detecting dynamic import as esm syntax (0b904a9)

1.12.5 (2021-09-29)

Bug Fixes

  • remove dynamicImport from options (ad42dd1)

1.12.4 (2021-09-29)

Bug Fixes

  • remove v8-compile-cache integration (a9fe3a0)

1.12.3 (2021-09-21)

Bug Fixes

  • interopDefault: allow recursive default (55e0f62)

1.12.2 (2021-09-21)

Bug Fixes

  • interopDefault: handle cjs cache (1f3e4c3)

1.12.1 (2021-09-21)

Bug Fixes

  • interopDefault: support mixed CJS + default (4392c6a)

1.12.0 (2021-09-13)

Features

1.11.0 (2021-07-26)

Features

  • support node: and file: protocols (bbb1cb3), closes #30
  • support mjs and cjs extensions (369c3ff)

1.10.1 (2021-05-28)

Bug Fixes

  • babel: properly pass plugin-transform-typescript options (7a1ae3b)

1.10.0 (2021-05-28)

Features

  • enable allowDeclareFields for babel transform (#33) (914499c)

1.9.2 (2021-05-11)

Bug Fixes

  • spread when pushing babel plugins from transformOptions (#31) (f25960a)

1.9.1 (2021-04-09)

1.9.0 (2021-04-09)

Features

  • interopDefault option (opt-in) (5203145)

1.8.0 (2021-04-09)

Features

1.7.0 (2021-04-09)

Features

  • support legacy decorators for typescript (#28) (801b798), closes #27
  • support legacy parameter decorators for typescript (#29) (6586a1c)

1.6.4 (2021-03-11)

Bug Fixes

1.6.3 (2021-03-06)

Bug Fixes

1.6.2 (2021-03-05)

Bug Fixes

  • support class property syntax for esm (64b1636)

1.6.1 (2021-03-05)

Bug Fixes

1.6.0 (2021-03-03)

Features

  • transpile import.meta.url (resolves #21) (d071704)

Bug Fixes

1.5.0 (2021-03-03)

Features

  • extensions option (fixes mjs handling) (3804b3c)

Bug Fixes

1.4.0 (2021-03-01)

Features

  • transpile nullish-coalescing-operator and optional-chaining for node<14 (6011ef5)

1.3.0 (2021-01-21)

Features

1.2.1 (2021-01-20)

Bug Fixes

  • improve extension handling for unknown formats (a2a797d)
  • use dynamic import => cjs only if native dynamic import is not possible (5323175)

1.2.0 (2021-01-14)

Features

1.1.0 (2021-01-13)

Features

  • support mixed sytax via esm fallback (1e642e4)

Bug Fixes

  • consistantly use tmpdir as cache dir (e49a791)

1.0.0 (2021-01-12)

0.1.20 (2021-01-12)

Bug Fixes

  • resolve with index.{ts,mjs} (2fe1846)

0.1.19 (2020-12-30)

Features

  • improved parse and runtime error stack trace (9d94ca1)

Bug Fixes

  • remove duplicate v8-compile-cache (#13) (3f81fc9)

0.1.18 (2020-12-22)

Features

  • use native require for resolving (f7e1a56)

Bug Fixes

  • support circular dependency (closes #12) (06c687f)

0.1.17 (2020-11-27)

Features

Bug Fixes

  • cli: resolve relative to cwd (52a4c12)

0.1.16 (2020-11-23)

Features

  • fallback to tmpdir and then disable if cache dir is not writable (398fe08)
  • support JITI_DEBUG ands JITI_CACHE environment variables (eed965d)

0.1.15 (2020-11-22)

Features

  • basic support for dynamic imports (9494452)

0.1.14 (2020-11-21)

Bug Fixes

  • cli: import main wrapper (25bceb1)

0.1.13 (2020-11-21)

Features

0.1.12 (2020-11-01)

Bug Fixes

  • directly call wrapper to fix require.resolve issue (63c0a5c)

0.1.11 (2020-06-19)

Features

0.1.10 (2020-06-19)

Bug Fixes

  • always ensure isDir for filename (6b343a4)

0.1.9 (2020-06-12)

Bug Fixes

0.1.8 (2020-06-12)

Bug Fixes

  • use fake file for default _filename (05d721f)
  • types: allow passing undefined filename (#2) (9136f15)

0.1.7 (2020-06-11)

Bug Fixes

  • pkg: add repository field (639c02f)

0.1.6 (2020-06-11)

Features

0.1.5 (2020-06-11)

Features

  • allow passign debug as jiti options (8da2310)
  • build and stack-trace improvements (71780ab)
  • improve babel transpile and debug (f3042dc)

0.1.4 (2020-06-11)

Features

Bug Fixes

  • multiline syntax detection (46f830b)

0.1.3 (2020-06-07)

0.1.2 (2020-06-07)

Features

  • improve build system and stability (5c3ee63)