Let's assume that you want to use Commander.js without any options.
Configure index.js
:
javascript#!/usr/bin/env node import { program } from 'commander'; program .command('hello') .arguments('<firstName> <secondName>') .description('Prints hello world') .action((firstName, secondName) => { console.log(`Hello ${firstName} ${secondName} 👋👋👋`); }) .parse();
Configure package.json
:
json{ "name": "helloworld", "version": "0.0.1", "description": "🦄 A simple package that prints a useless hello world", "main": "index.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node index.js" }, "bin": { "hello": "index.js" }, "author": "Erik Martín Jordán", "license": "MIT" }
Link the package to try it:
terminalnpm link
Test it:
terminal> hello Erik Martín Hello Erik Martín 👋👋👋
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.