Resolving the "Must use import to load ES Module" error in TypeScript

Kenny DuMez
Kenny DuMez
Graphite software engineer

The error "Must use import to load ES Module" typically occurs in a Node.js environment when attempting to require an ES module (using CommonJS syntax) or when there are mismatches in module type expectations between your TypeScript configuration and your Node.js runtime. This guide will provide detailed steps to resolve this issue in a TypeScript project, ensuring that your environment is correctly set up to handle ES modules.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Before diving into the solutions, it's important to understand why this error occurs:

  1. ES modules vs. CommonJS: Node.js historically used CommonJS module syntax (require() and module.exports). ES modules (import and export) are the standard in the ECMAScript specification and are now supported in Node.js but require specific configuration to work correctly.

  2. TypeScript compilation: TypeScript compiles your code into JavaScript. The module system (CommonJS or ES modules) TypeScript outputs can lead to compatibility issues with your runtime environment if not configured correctly.

If you intend to use ES modules, first ensure that your tsconfig.json file is configured correctly. This setup aligns TypeScript's output with Node.js's module handling capabilities.

Terminal
{
"compilerOptions": {
"target": "esnext", // Compile to a modern ECMAScript version that supports ES modules
"module": "esnext", // Use ES module syntax for module code generation
"moduleResolution": "node", // Use Node.js style module resolution
"esModuleInterop": true, // Enables default imports from modules with no default export
"outDir": "./dist" // Output directory for compiled files
}
}

As of Node.js version 12.x and later, ES modules are supported but may require specific flags or configurations depending on the version:

  • Node.js 12.x to 13.x: You may need to run Node.js with the --experimental-modules flag.
  • Node.js 14.x and later: ES modules are supported without experimental flags, but you need to include the type field in your package.json.
Terminal
{
"type": "module"
}

Adding "type": "module" tells Node.js to treat .js files as ES modules. Alternatively, you can use the .mjs extension for your files if you prefer not to set the "type" in package.json.

If you're using TypeScript and intend to use ES modules, make sure your import statements are compatible:

Terminal
// Correct
import express from 'express'
// Incorrect
const express = require('express')

For conditional imports, TypeScript and modern JavaScript support dynamic imports that return promises. This feature is particularly useful for code splitting or loading modules conditionally:

Terminal
if (needModule) {
import('my-module').then((module) => {
module.doSomething()
})
}

When importing CommonJS modules in an ES module context, you might encounter compatibility issues. Node.js provides a way to import CommonJS modules dynamically:

Terminal
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const someCommonJsModule = require('some-commonjs-module')
Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
  • Check Node.js version: Ensure your Node.js version supports ES modules without additional configuration.
  • Review build and run scripts: Ensure your scripts in package.json do not inadvertently transpile code to CommonJS or use incompatible Node.js flags.
  • Dependencies: Some npm packages might not be fully compatible with ES modules. Check the documentation for any package-specific instructions.

Resolving the "Must use import to load ES Module" error involves aligning your TypeScript configuration with your runtime environment's capabilities and ensuring correct usage of module syntax. By configuring both TypeScript and Node.js properly, adopting ES module syntax in your source files, and understanding how to interact with CommonJS modules, you can mitigate module compatibility issues in your TypeScript projects.

For further reading on ES modules in Typescript see the official documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2