1.0.3 • Published 3 years ago

is-launch-file v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

isLaunchFile: Determine if a file was called directly with NodeJS Modules

Determine if a file was called directly with NodeJS Modules.

In NodeJS packages with "type": "module", the module and require keywords are undefined, so you can't use the old method of if (!module.parent). isLaunchFile uses import.meta.url and process.argv[0] to determine if a file was called directly.

Installation

npm install --save is-launch-file

Usage

import isLaunchFile from "is-launch-file"

if (isLaunchFile(import.meta.url)) {
	// This file was called directly.
}

Example

// ------------------------
// call-me-directly.js
// ------------------------
import isLaunchFile from "../index.js"
import "./only-imported.js"

console.log("call-me-directly.js:", isLaunchFile(import.meta.url))

// ------------------------
// only-imported.js
// ------------------------
import isLaunchFile from "../index.js"

console.log("only-imported.js:", isLaunchFile(import.meta.url))

If you execute node call-me-directly.js, you'll get the following output:

call-me-directly.js: true
only-imported.js: false
1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago