This recipe transforms usage of the deprecated process.mainModule to use require.main in CommonJS modules.
Deprecation
Node.js deprecated process.mainModule in favor of require.main for CommonJS modules.
See DEP0138 for more details.
Usage
Run this codemod with:
Before/After
What It Does
- Replaces all instances of
process.mainModule with require.main
- Works with any comparison or assignment pattern
- Preserves the rest of your code logic
Common Use Cases
Detecting if Module is Main
Accessing Main Module Properties
This replacement only applies to CommonJS modules. For ESM modules, use import.meta.url to determine the entry point.
The pattern require.main === module is a common way to check if a script was run directly or imported as a module, enabling dual-mode usage.