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

Package detail

if-main

stefanpenner3MIT1.0.2

null

readme

if-main CI

yarn add if-main
// my-file.mjs
import ifMain from 'if-main';

ifMain(import.meta.url, async ([name]) => {
  console.log(`Hello, ${name}!`);
  // do stuff if the current file, is also the main file for the program
  // node <current-file>
  // if you want to exit with a non zero status code, throw an exception
  //   throw new Error('something went wrong');
  // if you want to exit with a specific status code
  //   process.exitCode = 99;
});
node ./my-file.mjs world
> Hello, world!

why ?

because