Own global NPM module in 5 minutes

Oleh Baranovskyi
4 min readMar 31, 2018

--

There are two ways to install npm modules: locally or globally.
In current article we are going to learn how to create and use own global npm module. Global module can be very helpful and guess you already saw that by using gulp, grunt, bower, angular-cli, jshint or some others. In my opinion it can be very useful to create some clients that will initialize your application as you wish for example will configure editor config or mocha with first test files.

For those who do not know basic usage of global modules I will try to explain it in short way.

  1. Global module can be accessed from any directory of your pc using command line
  2. If module was installed as global then your command in the beginning will have module name, for example if you installed bower as global module you can run from any directory the following command:
bower init 

3. In order to install module globally we use -g before module name:

npm install -g <package>

Now we have some basic understanding of NPM global modules, so we can move on.

  1. First let’s create working directory and go there:
mkdir best-guitar-player
cd best-guitar-player

2. Create ./lib and ./bin directory inside of root folder:

mkdir bin
mkdir lib

3. Now we need to initialize root folder as node project and we can use default package.json. In order to skip all npm questions we can add — yes at the end of our npm init command:

npm init --yes

our root folder looks like this:

4. Create in ./lib folder index.js file. It will be responsive for module functionality:

cd ./lib
touch index.js

Content of index.js file:

let bestGuitarPlayer = 'Jimi_Hendrix'
, log = console.log;
let printBestGuitarPlayer = function(guitarPlayer) {
if(guitarPlayer === bestGuitarPlayer) {
log(`Best guitar player is ${guitarPlayer}`)
} else {
log(`Really? I guess ${bestGuitarPlayer} is much more better than ${guitarPlayer}`)
}
}
exports.printBestGuitarPlayer = printBestGuitarPlayer;

5. Create global module file:

cd ../bin
touch run.js

Content of run.js:

#!/usr/bin/env nodelet lib = require('../lib')
, args = process.argv.splice(process.execArgv.length + 2)
, guitarPlayerName = args[0];
lib.printBestGuitarPlayer(guitarPlayerName);

#!/usr/bin/env node is an instance of a shebang line: the very first line in an executable plain-text file on Unix-like platforms that tells the system what interpreter to pass that file to for execution, via the command line following the magic #! prefix (called shebang).

Note: Windows does not support shebang lines, so they’re effectively ignored there; on Windows it is solely a given file’s filename extension that determines what executable will interpret it.

5. Now let’s expose module for global installation. In order to do that we need to update our package.json file with the following changes:

{
"name": "oleh-global-module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"oleh-global-module": "./bin/run.js"
}
,
"preferGlobal": true,
"directories": {
"lib": "lib"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

Okay, now we are ready to install our module globally.

Installation

There are two ways to install it:

  1. Go to folder up from root directory and run:
npm install -g best-guitar-player

2. Use full path (in my case it will be: /Users/obaranovsky/Desktop/best-guitar-player):

npm install -g /Users/obaranovsky/Desktop/best-guitar-player

Now are ready to test it!

Testing

Go to any directory and run the following command:

best-guitar-player Ritchie_Blackmore

Here is the result:

or you can run this command :)

best-guitar-player Jimi_Hendrix

and result will be:

Deinstallation

You can uninstall global module in two ways:

  1. Using full path (in my case it will be /Users/obaranovsky/Desktop/best-guitar-player)
npm uninstall -g /Users/obaranovsky/Desktop/best-guitar-player

2. From directory that contains ./best-guitar-player folder

npm uninstall -g best-guitar-player

Conclusion

Thank you guys for reading. I hope you enjoyed it and learned some new stuff related to JavaScript. Please subscribe and press ‘Clap’ button if you like this article.

--

--