Registry / es-main

es-main

JSON →
library1.4.0jsnpmunverified

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-main
INSTALL
IMPORT
SIG · ES-MAIN
E
es-main
javascriptv1.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

esMain
import esMain from 'es-main';
const esMain = require('es-main');
This library is an ES Module and should be imported using `import` syntax. CommonJS `require()` is not supported.

Demonstrates how to use `es-main` to detect if an ES module is the main entry point, logging details about its execution context.

import esMain from 'es-main'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; // In an ES Module, __filename and __dirname are not globally available. // This is a common pattern to derive them from import.meta.url. const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function main() { if (esMain(import.meta)) { console.log(`[es-main] This module is being run directly.`); console.log(`Main file path: ${__filename}`); console.log(`Derived directory: ${__dirname}`); console.log(`Command line arguments: ${process.argv.slice(2).join(' ')}`); // Example: Run a main script task await new Promise(resolve => setTimeout(resolve, 1000)); console.log('Main script execution complete.'); } else { console.log(`[es-main] This module has been imported by another script.`); // You can export functions here for other modules to use, e.g., // export function someUtility() { /* ... */ } } } main();
Debug
Known issues
deprecatedFor Node.js versions 22.18.0 or 24.2.0 and newer, the native `import.meta.main` should be used directly instead of `es-main`.
fix
Replace `if (esMain(import.meta))` with `if (import.meta.main)`.
affects: >=1.0.0
gotchaThis package is explicitly designed for ES Modules and relies on the `import.meta` object. Attempting to use it in a CommonJS context or environments without `import.meta` support will result in errors.
fix
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`).
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to import `es-main` using `require()` in an ES module context.
fix
Use the ES Module `import` syntax: `import esMain from 'es-main';`
SyntaxError: Cannot use 'import.meta' outside a module
The JavaScript file containing `esMain(import.meta)` is not being interpreted by Node.js as an ES module.
fix
Ensure the file has a `.mjs` extension or that the nearest `package.json` specifies `"type": "module"` to enable ES module parsing.
Upgrade
Version history
1.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
es-main — npm install es-main · libregistry