{"id":13128,"library":"es-main","title":"ES Module Main Check","description":"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.","status":"maintenance","version":"1.4.0","language":"javascript","source_language":"en","source_url":"git://github.com/tschaub/es-main","tags":["javascript","require.main","process.mainModule","modules","import.meta.main","typescript"],"install":[{"cmd":"npm install es-main","lang":"bash","label":"npm"},{"cmd":"yarn add es-main","lang":"bash","label":"yarn"},{"cmd":"pnpm add es-main","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is an ES Module and should be imported using `import` syntax. CommonJS `require()` is not supported.","wrong":"const esMain = require('es-main');","symbol":"esMain","correct":"import esMain from 'es-main';"}],"quickstart":{"code":"import esMain from 'es-main';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\n// In an ES Module, __filename and __dirname are not globally available.\n// This is a common pattern to derive them from import.meta.url.\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nasync function main() {\n  if (esMain(import.meta)) {\n    console.log(`[es-main] This module is being run directly.`);\n    console.log(`Main file path: ${__filename}`);\n    console.log(`Derived directory: ${__dirname}`);\n    console.log(`Command line arguments: ${process.argv.slice(2).join(' ')}`);\n    // Example: Run a main script task\n    await new Promise(resolve => setTimeout(resolve, 1000));\n    console.log('Main script execution complete.');\n  } else {\n    console.log(`[es-main] This module has been imported by another script.`);\n    // You can export functions here for other modules to use, e.g.,\n    // export function someUtility() { /* ... */ }\n  }\n}\n\nmain();\n","lang":"javascript","description":"Demonstrates how to use `es-main` to detect if an ES module is the main entry point, logging details about its execution context."},"warnings":[{"fix":"Replace `if (esMain(import.meta))` with `if (import.meta.main)`.","message":"For 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`.","severity":"deprecated","affected_versions":">=1.0.0"},{"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`).","message":"This 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use the ES Module `import` syntax: `import esMain from 'es-main';`","cause":"Attempting to import `es-main` using `require()` in an ES module context.","error":"ReferenceError: require is not defined"},{"fix":"Ensure the file has a `.mjs` extension or that the nearest `package.json` specifies `\"type\": \"module\"` to enable ES module parsing.","cause":"The JavaScript file containing `esMain(import.meta)` is not being interpreted by Node.js as an ES module.","error":"SyntaxError: Cannot use 'import.meta' outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}