This is my add-cli.js
#!/usr/bin/env node
// Extracting the command line arguments
const args = process.argv.slice(2);
// Function to add numbers
function addNumbers(numbers) {
return numbers.reduce((acc, curr) => acc + parseInt(curr), 0);
}
// Checking if arguments are provided
if (args.length === 0) {
console.log("Please provide numbers to add. Example: add-cli 2 3");
} else {
const sum = addNumbers(args);
console.log("Sum:", sum);
}
`
This is my package.josn
{ "name": "add-cli", "version": "1.0.0", "description": "A CLI tool to add numbers.", "main": "add-cli.js", "bin": "./add-cli.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Your Name", "license": "MIT" }
Now when i run ./add-cli 2 3
It doesnt log 5 but creates a new files named 2 and 3 and brings cursor to add-cli.js file
I was learning to build a cli for npm and i saw that using bin we can make a command that can be used to execute that linked file.
P.S. - It works with node ./add-cli.js 2 3