is-main-module
v1.0.5
Published
Can be useful with Webpack to detect whether module is required or called directly
Downloads
62
Maintainers
Readme
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
npm i --save is-main-module- Make sure you have set
externals: [nodeExternals()]in your Webpack config. - Check if your module is required by another one with
!isMain(). In addition you can get refs to thismoduleandrequire.main
Example
const thisModule = require('is-main-module');
const isThisModuleRequired = thisModule.!isMain();
const isThisModuleMain = thisModule.isMain();
const refToThisModule = thisModule.moduleRef;
const refToMainModule = thisModule.mainRef;