Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

is-main

ratson871MIT0.4.0TypeScript support: included

Check if current module is main module.

require.main, main-module, import, process, main, esm, es-main, es-module

readme

is-main

Check if current module is main module.

Installation

npm install is-main --save

Usage

import { isMain } from 'is-main'

if (isMain(import.meta)) {
  console.log('is main')
}
const { isMain } = require('is-main')

if (isMain(module)) {
  console.log('is main')
}

Note

While it is useful to put main script with the module code in the same file, move main script into a separate file is often better, given there is no standard way for detecting main script.

See this Node issue for more information.

  • esmeta - Handy function to handle import.meta