$ mkdir sample
$ cd sample
$ npm init
$ touch index.jsindex.js
#!/usr/bin/env node
console.log(`Hi there`)package.json
{
"name": "santosh",
"version": "1.0.0",
"bin": {
"santosh": "index.js"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Santosh Subedi",
"license": "ISC"
}Note:
santoshwill be our command to execute our index.js file
"bin": {
"santosh": "index.js"
},$ cd sample
$ npm linkNote: This command will link our project as global executable. It is like installing a npm package globally
npm i -g santosh
$ santosh
Hi thereNote: Your sample folder will be sync with the command. So any changes inside that project folder will reflect on that command execution.
$ npm adduseror,
$ npm login
$ npm publishNote: If you are publishing next feature (major/minor/patch). You have to change
versionvalue insidepackage.jsonfile. Butnpmprovides us a simple way
$ cd sample
$ npm version major/minor/patch
$ npm publish
Always clear git logs (meaning make your local changes to remote git repo updated before publishing to npm).
e.g.
$ git add .
$ git commit -m "IMPROVE: Clear console added"
$ git push origin master
$ npm version minor
$ npm publish