$ mkdir sample
$ cd sample
$ npm init
$ touch index.js
index.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:
santosh
will be our command to execute our index.js file
"bin": {
"santosh": "index.js"
},
$ cd sample
$ npm link
Note: This command will link our project as global executable. It is like installing a npm package globally
npm i -g santosh
$ santosh
Hi there
Note: Your sample folder will be sync with the command. So any changes inside that project folder will reflect on that command execution.
$ npm adduser
or,
$ npm login
$ npm publish
Note: If you are publishing next feature (major/minor/patch). You have to change
version
value insidepackage.json
file. Butnpm
provides 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