Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Don't use .mjs in an ESM project
When package.json declares "type": "module", plain .js files are ESM; avoid the redundant .mjs extension.
Bad example
| 1 | // package.json: { "type": "module" } |
| 2 |
|
| 3 | // scripts/bump.mjs |
| 4 | import { run } from './shared.mjs'; |
| 5 | run(); |
Explanation (EN)
The project is already ESM, so .mjs is redundant and introduces an inconsistent extension across the codebase.
Objašnjenje (HR)
Projekt je vec ESM, pa je .mjs suvisan i unosi nedosljednu ekstenziju kroz codebase.
Good example
| 1 | // package.json: { "type": "module" } |
| 2 |
|
| 3 | // scripts/bump.js |
| 4 | import { run } from './shared.js'; |
| 5 | run(); |
Explanation (EN)
Plain .js is interpreted as ESM thanks to "type": "module", so use it consistently. Reserve .mjs/.cjs only for files that must override the package default.
Objašnjenje (HR)
Obican .js se zbog "type": "module" tumaci kao ESM, pa ga koristi dosljedno. .mjs/.cjs cuvaj samo za datoteke koje moraju nadjacati zadanu postavku paketa.
Notes (EN)
Use .cjs/.mjs only when a single file needs to deviate from the package's declared module type.
Bilješke (HR)
Koristi .cjs/.mjs samo kad pojedina datoteka mora odstupiti od deklariranog tipa modula paketa.
Exceptions / Tradeoffs (EN)
A file that must be CommonJS inside an ESM package (or vice versa) legitimately needs the explicit .cjs/.mjs extension.
Iznimke / Tradeoffi (HR)
Datoteka koja mora biti CommonJS unutar ESM paketa (ili obrnuto) opravdano treba eksplicitnu .cjs/.mjs ekstenziju.