package type field
The "type" setting in package.json that makes Node.js treat the package's .js files as ES modules ("module") or CommonJS ("commonjs").
In more detail
When it is missing, Node.js looks at the syntax to guess, but setting it explicitly avoids surprises. Files ending in .mjs are always ES modules and .cjs always CommonJS, whatever the field says.
Related terms
- package.json — The JSON file at the root of every JavaScript package that records its name, version, dependencies, entry points and other settings.
- ECMAScript modules — The standard JavaScript module system, written with import and export statements, used by browsers and modern tools.
- CommonJS — Node.js's original module system, which loads code with require() and exports it with module.exports.
Taught in
- Native Module Systems: ESM Loader Hooks vs CommonJS — Node.js Internals & Backend Services