deslop/commonjs-in-esm
Flag CommonJS constructs (require/module.exports/exports.x) inside an ESM module.
- Category: Correctness
- Severity: warn
- Source:
deslop-js - Framework: global
- Enabled when: react-doctor deadCode analysis enabled (default true); whole-project scan only — skipped in --diff/--staged modes
- Documentation: https://github.com/millionco/deslop-js
Validation prompt
Use this to decide whether a fired diagnostic is real or a false positive.
Fires only on files deslop classified as ESM (.mjs/.mts, or nearest package.json type is "module"; .cjs/.cts are skipped) that contain a CommonJS construct: kind "require" on a require("literal") call ("synchronous require() is unavailable in native ESM"), kind "module-exports" on a module.exports = ... assignment ("module.exports = ... is CommonJS"), or kind "exports-assignment" on an exports.x = ... assignment ("exports.x = ... is CommonJS"); all reported at high confidence. False positive: the file is actually CommonJS but was misclassified — e.g. a nested package.json sets type "commonjs" that deslop's type resolution missed, or it is a deliberate dual-package interop shim that intentionally bridges require and import — suppress those rather than convert. Separately, a genuinely ESM-classified generated build artifact fires correctly and is not a false positive, but leave it untouched and fix the upstream source instead of editing generated output.
Fix prompt
Use this once validation confirms the diagnostic is real.
Convert the construct to ESM: turn module.exports = X into export default X (or named exports if X is an object literal of named bindings), turn exports.foo = bar into export const foo = bar, and turn const x = require("pkg") into import x from "pkg" (or top-level await import("pkg") if the require is genuinely conditional or dynamic). If the file truly needs CommonJS, rename it to .cjs or set the package's type accordingly instead of mixing systems. Mixing CommonJS into a native ESM module breaks at runtime because require, module, and exports are not defined in ESM scope. See https://nodejs.org/api/esm.html#interoperability-with-commonjs