is-main
Version:
Check if current module is main module.
42 lines (27 loc) • 731 B
Markdown
Check if current module is main module.
```
npm install is-main --save
```
<!-- eslint-disable strict -->
```js
import { isMain } from 'is-main'
if (isMain(import.meta)) {
console.log('is main')
}
```
```js
const { isMain } = require('is-main')
if (isMain(module)) {
console.log('is main')
}
```
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](https://github.com/nodejs/node/issues/15760) for more information.
- [esmeta](https://github.com/ratson/esmeta) - Handy function to handle import.meta