More Node.js answers

by Danny
Published 2 min read

The type field decides whether Node treats .js files as ES modules or CommonJS. Here is what it changes, when to set it, and how .mjs and .cjs let you mix both formats.

The short answer

#

For a new project, yes. Setting "type": "module" makes Node treat every .js file in the package as an ES module, so you can use import and export everywhere. Keep CommonJS for older projects that rely on require() until you are ready to migrate, and use .cjs or .mjs for the odd file that needs the other format.


_10
{
_10
"name": "my-app",
_10
"type": "module"
_10
}

Why it happens

#

Node supports two module systems:

  • CommonJS, the original Node format, with require() and module.exports.
  • ES modules (ESM), the JavaScript standard, with import and export. Browsers, Deno, Bun and modern tooling all use it.

A .js file could be either, so Node needs a rule. It looks for the nearest package.json above the file and reads its type field:

FileTreated as
.js with "type": "module"ES module
.js with "type": "commonjs" or no typeCommonJS
.mjsAlways ES module
.cjsAlways CommonJS

Recent Node versions can also notice ESM syntax in a .js file when there is no type field and run it as a module, but they print a warning and do extra work to reparse the file. Set the field explicitly instead.

How to fix it

#

New projects: set type to module

Add the field, then write standard imports. Remember that ES modules need full relative paths with file extensions.


_10
// src/index.js
_10
import { readFile } from 'node:fs/promises';
_10
import { format } from './format.js';
_10
_10
const text = await readFile(new URL('./data.txt', import.meta.url), 'utf8');
_10
console.log(format(text));

Things that change when you switch:

  • require, module.exports, __dirname and __filename are not defined. Use import, export, import.meta.dirname and import.meta.filename.
  • Top-level await works.
  • Code runs in strict mode.
  • JSON imports need an attribute: import data from './data.json' with { type: 'json' };.

Existing CommonJS projects: migrate gradually

You do not have to convert everything at once. Rename new files to .mjs, or set "type": "module" and rename the files you have not converted yet to .cjs.

ES modules can import CommonJS files directly. CommonJS can load ES modules with require() from Node 22.12.0 and 20.19.0 onwards, as long as the module does not use top-level await. On older versions use await import() instead.


_10
// legacy.cjs
_10
const { format } = require('./format.mjs'); // works in Node 22.12.0+

Config files that must stay CommonJS

Some tools still load their config with require(). If one breaks after the switch, rename its config to .cjs, for example .eslintrc.cjs, or check whether the tool supports an .mjs or .ts config.

Libraries: think about consumers

If you publish a package, the type field only affects how your own files are read. Use the exports field to say which file each consumer gets. Now that require(esm) works in every supported Node release, many libraries ship ESM only. If you need to support older tools, publish both builds:


_10
{
_10
"type": "module",
_10
"exports": {
_10
".": {
_10
"import": "./dist/index.js",
_10
"require": "./dist/index.cjs"
_10
}
_10
}
_10
}

Common pitfalls

#
  • Forgetting .js on relative imports. ESM does not guess extensions, so you get ERR_MODULE_NOT_FOUND.
  • Expecting the field to affect files in another package. Each package's own package.json decides for its files.
  • Putting "type": "module" in the root of a monorepo and breaking CommonJS config files in every workspace.
  • Mixing require and import in the same file. A file is one format or the other.
  • Assuming TypeScript output follows the field automatically. Set "module": "nodenext" in tsconfig.json so the compiler follows the same rules as Node.
#

Was this resource helpful?