AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 37

What does "type": "module" mean in package.json?

Direct answer

"type": "module" treats .js files in that package as ES modules — import/export allowed, require absent unless .cjs; omitting type or "commonjs" keeps .js as CommonJS; use .mjs/.cjs to override per file.

The type field sets the default module system for .js files in a package boundary — critical in monorepos where apps/api and apps/web may differ from legacy CJS tools.

Setting.js behaviorExtension override
"type": "module"ESM — import/export.cjs → CommonJS
"type": "commonjs" or omittedCJS — require.mjs → ESM
No type in root package.jsonNode default: CommonJS for .jsPer-package in workspaces
  • Node version — ESM stable without flags in modern Node LTS; check engines in package.json.
  • Tooling — tsx/ts-node respect type; Jest/Vitest need ESM config when type is module.
  • Dual packages — libraries may ship "exports" map with import and require conditions.
package.json
{
  "name": "@acedevhub/api",
  "type": "module",
  "engines": { "node": ">=20" },
  "scripts": {
    "content:import:interviews": "tsx --env-file=.env src/scripts/import-content-interviews.ts"
  }
}