Type: Module and Other Config Hints
You'll learn: How to configure package.json to control module system defaults and give bundlers important optimization hints.
The type Field
By default, Node.js treats .js files as CommonJS modules. To use ES modules (import/export) without renaming files to .mjs, add "type": "module" to your package.json:
{
"name": "my-app",
"version": "1.0.0",
"type": "module"
}
Now all .js files are treated as ES modules. If you still need CommonJS files, rename them to .cjs.
Think of it like this: The type field sets the "default language" for your project. Without it, Node speaks CommonJS; with "type": "module", it speaks modern ES modules.
The sideEffects Field Revisited
You've seen sideEffects in the tree shaking phase—it tells bundlers which files can be safely removed if their exports aren't used:
{
"sideEffects": false
}
This means "my entire package is pure; drop any unused imports." Or list specific files with side effects:
{
"sideEffects": ["./src/polyfills.js", "*.css"]
}
Other Configuration Hints
browser: Specifies an alternative entry point for browser environmentsworkspaces: Defines monorepo packages (used by Yarn/PNPM workspaces)private: true: Prevents accidental publishing to NPM
These fields guide tools like bundlers, Node.js, and package managers in understanding how your code should be interpreted and optimized.
Key Takeaway: The type field controls whether Node treats .js files as CommonJS or ES modules, while fields like sideEffects give bundlers critical optimization hints—all improving modern JavaScript development workflows.