es-main is a utility package designed to replicate the CommonJS `require.main === module` check for ES modules in Node.js environments. It allows developers to determine if an ES module is being executed directly as the main entry point, enabling conditional logic for scripts that can be both imported and run independently. The package is currently at version `1.4.0` and has seen updates primarily to incorporate newer Node.js features, like utilizing `import.meta.main` when available, and for development dependency maintenance. While es-main offers a crucial workaround for older Node.js versions lacking a native equivalent, recent Node.js releases (>= 22.18.0 or 24.2.0) now provide `import.meta.main` directly, rendering es-main largely redundant for modern applications using those versions. Its primary differentiation lies in providing compatibility for environments where `import.meta.main` is not yet implemented.
npm install es-mainVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `es-main` to detect if an ES module is the main entry point, logging details about its execution context.
Replace `if (esMain(import.meta))` with `if (import.meta.main)`.
Ensure the file using `es-main` is treated as an ES Module (e.g., `.mjs` extension or `"type": "module"` in `package.json`) and run on a compatible Node.js version (Node.js 12.0.0+ for `import.meta`).
Use the ES Module `import` syntax: `import esMain from 'es-main';`
Ensure the file has a `.mjs` extension or that the nearest `package.json` specifies `"type": "module"` to enable ES module parsing.
No dependency data recorded yet.