Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

is-main-module

1.0.5 • Public • Published

Problem description

The standard way to detect whether NodeJS module was called directly (via CLI) or required by another module is to use module === require.main. When using Webpack both that objects do not refer to the original objects module and require.main provided by NodeJS, as we expect by default. See this issue


How to use

  1. npm i --save is-main-module
  2. Make sure you have set externals: [nodeExternals()] in your Webpack config.
  3. Check if your module is required by another one with !isMain(). In addition you can get refs to this module and require.main

Example

const thisModule = require('is-main-module');

const isThisModuleRequired = thisModule.!isMain();
const isThisModuleMain = thisModule.isMain();
const refToThisModule = thisModule.moduleRef;
const refToMainModule = thisModule.mainRef;

Install

npm i is-main-module

DownloadsWeekly Downloads

4

Version

1.0.5

License

ISC

Unpacked Size

1.62 kB

Total Files

3

Last publish

Collaborators

  • avatar