| authors | brunocroh |
|---|
Replaces deprecated process.mainModule with require.main in CommonJS modules.
Run this codemod with:
npx codemod @nodejs/process-main-moduleDirect property access:
-if (process.mainModule === module) {
+if (require.main === module) {
// cli thing
} else {
// module thing
}mainModule destructured from process — the destructuring is removed and all usages are replaced with require.main:
-const { mainModule } = process;
-
-if (mainModule === module) {
- console.log(mainModule.filename);
+if (require.main === module) {
+ console.log(require.main.filename);
}mainModule destructured from a require("node:process") call alongside other bindings — only mainModule is removed from the destructure; the remaining bindings are preserved:
-const { mainModule, env, cwd } = require("node:process");
+const { env, cwd } = require("node:process");
-if (mainModule === module) {
+if (require.main === module) {
console.log(env, cwd);
}