Date:

Jiti

Jiti: A Package for Seamless Interoperability between ESM and CommonJS

For this week, I have been reading the unbuild source code and found a few packages that I have never seen before or used. I wanted to share some interesting packages that are used in these OSS projects so we can learn a thing or two.

What is Jiti?

Jiti is a package built by authors at Unjs. Unjs provides JS tools, libraries, and has about 63 npm packages and 421m downloads per month. Sheesh, that’s a lot.

The Jiti repository has this description “Runtime TypeScript and ESM support for Node.js”. Okay, what does this mean? To understand more about this tool, I looked at the features it offers:

  • 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)

Jiti’s usage in Docusaurus

I found the below code related to Jiti in the Docusaurus — moduleUtils.js:

import jiti from 'jiti';
//...

export async function loadFreshModule(modulePath: string): Promise<unknown> {
  try {
    if (typeof modulePath!== 'string') {
      throw new Error(logger.interpolate`Invalid module path of type ${modulePath}`);
    }
    const load = jiti(__filename, {
      // Transpilation cache, can be safely enabled
      cache: true,
      // Bypass Node.js runtime require cache
      // Same as "import-fresh" package we used previously
      requireCache: false,
      // Only take into consideration the default export
      // For now we don't need named exports
      // This also helps normalize return value for both CJS/ESM/TS modules
      interopDefault: true,
      // debug: true,
    });
    return load(modulePath);
  } catch (error) {
    console.error(error);
  }
}

Note in Docusaurus Migration Guide

New Config Loader

Docusaurus v3 changes its internal config loading library from import-fresh to jiti. It is responsible for loading files such as docusaurus.config.js or sidebars.js, and Docusaurus plugins.

Conclusion

In this article, we discussed Jiti, a package built by authors at Unjs, and its usage in Docusaurus. Jiti provides seamless interoperability between ESM and CommonJS, which is useful in dynamic imports and other scenarios.

FAQs

Q: What is Jiti?
A: Jiti is a package built by authors at Unjs that provides runtime TypeScript and ESM support for Node.js.

Q: What are the features of Jiti?
A: Jiti provides seamless TypeScript and ESM syntax support for Node.js, seamless interoperability between ESM and CommonJS, and other features such as asynchronous API, synchronous API, and more.

Q: How is Jiti used in Docusaurus?
A: Jiti is used in Docusaurus as a config loader to load files such as docusaurus.config.js or sidebars.js, and Docusaurus plugins.

Latest stories

Read More

mimic Robotics unveils full-stack platform for dexterous robot manipulation

mimic Robotics has introduced a new robotic hand,...

Aetina expands Nvidia Jetson Thor portfolio with T3000 and T2000 support

Please switch off your ad blocker. Our website relies...

How to benchmark your system before running robotics simulations

Please switch off your ad blocker. Our website relies...

Has AI Agent Autonomy Redefined Robotics Safety and Control?

Robotics systems are learning to perceive, choose, and...

Opinion: Exotec managing director highlights key warehouse automation trends for 2026

Thomas Genestar, managing director of western Europe at...

MassRobotics opens RoboBoston 2026 sponsorships and announces AI career fair

MassRobotics has announced that it will host RoboBoston...

Agility Robotics opens new Fremont facility to accelerate physical AI development

Agility Robotics, a humanoid robotics and physical AI...

LEAVE A REPLY

Please enter your comment!
Please enter your name here