---
title: "Should I use \"type\": \"module\" in package.json?"
description: "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."
url: https://www.danny.engineering/answer/nodejs/should-i-use-type-module-in-packagejson
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Node.js"]
---

# Should I use "type": "module" in package.json?

## 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.


```json
{
  "name": "my-app",
  "type": "module"
}
```


## 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:


| File                                         | Treated as       |
| -------------------------------------------- | ---------------- |
| `.js` with `"type": "module"`                | ES module        |
| `.js` with `"type": "commonjs"` or no `type` | CommonJS         |
| `.mjs`                                       | Always ES module |
| `.cjs`                                       | Always 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.


```javascript
// src/index.js
import { readFile } from 'node:fs/promises';
import { format } from './format.js';

const text = await readFile(new URL('./data.txt', import.meta.url), 'utf8');
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.


```javascript
// legacy.cjs
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:


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


## 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.

## Related reading

- [Node.js: Packages, the type field](https://nodejs.org/api/packages.html#type)
- [Node.js: ECMAScript modules](https://nodejs.org/api/esm.html)
