Jiti: TypeScript and ESM Runtime for Node.js

2.6.1 · active · verified Sun Apr 19

Jiti is a robust utility for Node.js that provides seamless runtime support for both TypeScript and ECMAScript Modules (ESM), along with comprehensive interoperability between ESM and CommonJS. The package is currently at version 2.6.1 and maintains a high release cadence with frequent minor version bumps and patch fixes, indicating active and continuous development. Its key differentiators include a 'zero dependency' footprint, significant performance optimizations (e.g., improved startup times from v2.6.0 onwards), and widespread adoption within prominent projects such as Nuxt, TailwindCSS, ESLint, and Storybook. Jiti offers both an asynchronous (`jiti.import`) and a synchronously (now deprecated `jiti()`) API, alongside a global ESM loader for Node.js versions 20 and above. It simplifies the execution of complex JavaScript ecosystems by handling various module formats and language extensions without requiring explicit build steps.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize Jiti for runtime TypeScript and ESM support, dynamically import a module, access both named and default exports, and resolve paths programmatically, including creating and cleaning up a temporary module for a runnable example.

import { createJiti } from "jiti";
import path from "node:path";
import fs from "node:fs/promises";

// Initialize jiti, specifying the current module's URL for resolution context.
// In a CommonJS environment, use `__filename` instead of `import.meta.url`.
const jiti = createJiti(import.meta.url, {
  debug: process.env.NODE_ENV === 'development',
  // Cache directory to speed up subsequent loads
  cacheDir: path.join(process.cwd(), '.jiti-cache'),
});

// Example of importing a TypeScript file with ESM compatibility
// For demonstration, we'll create a dummy TS file first.
async function runJitiExample() {
  const dummyTsPath = path.join(__dirname, 'temp-module.ts');
  const dummyTsContent = 'export const message = "Hello from Jiti!"; export default { status: "OK" };';
  await fs.writeFile(dummyTsPath, dummyTsContent);

  try {
    // Import the module. Jiti handles TypeScript transpilation.
    // Use a generic type to ensure strong typing for the imported module.
    const mod = await jiti.import<{ message: string; default: { status: string } }>(dummyTsPath);
    console.log("Named export 'message':", mod.message);

    // To directly get the default export (common for default-export-only modules)
    const modDefault = await jiti.import<{ status: string }>(dummyTsPath, { default: true });
    console.log("Default export 'status':", modDefault.status);

    // Jiti can also resolve paths using ESM resolution logic.
    const resolvedPath = jiti.esmResolve('./temp-module.ts');
    console.log("Resolved path for temp-module.ts:", resolvedPath);
  } catch (error) {
    console.error("Error running Jiti example:", error);
  } finally {
    // Clean up the temporary file.
    await fs.unlink(dummyTsPath);
  }
}

runJitiExample();

view raw JSON →