Softnami
Author: Hussain Mir Ali

I am interested in web and mobile technologies.

If you any questions or feedback then message me at devtips@softnami.com.

Announcements
Ads

Call predictor: AI based call prediction

Download

2020

Feb
Jan

2019

Dec
Nov
Jul
May
Mar
Jan

2018

Nov
Sep
Jul
Jun
Apr
Feb
Jan

2017

Dec
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Jan

2016

Dec
Oct
Sep
Aug
Jul

Launch and use NPM modules



JavaScript's built in methods don't always satisfy application use cases. Instead of creating a custom method just for a single application developers can find solutions built by others on npm. The node package manger(npm) has thousands of modules built by other developers that are built for both general and specific use cases.

In this blog I have discussed how developers can use npm modules and launch their own npm modules.

Phase 1: Use an existing npm module
I will use an existing npm module known as Fibonacci which is a Fibonacci number generator module built by another developer.

Phase 2: Build and launch your own npm module
I will be building a mock 'localStorage' module which other developers can use to test browser code using NodeJS.

Phase 1

Step 1: Create the package.json file in your project folder.


hxce@ubuntu:~/Desktop/npmtest$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (npmtest)
version: (1.0.0)
description: Using npm module
entry point: (index.js) main.js
test command:
git repository:
keywords:
author: Hussain Mir Ali
license: (ISC)
About to write to /home/hxce/Desktop/npmtest/package.json:

{
"name": "npmtest",
"version": "1.0.0",
"description": "Using npm module",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Hussain Mir Ali",
"license": "ISC"
}


Is this ok? (yes)
hxce@ubuntu:~/Desktop/npmtest$



Step 2: Install 'fibonnaci' module from npm.


hxce@ubuntu:~/Desktop/npmtest$ npm install fibonacci --save
npm WARN package.json npmtest@1.0.0 No repository field.
npm WARN package.json npmtest@1.0.0 No README data
fibonacci@1.6.1 node_modules/fibonacci
└── bn.js@4.11.6
hxce@ubuntu:~/Desktop/npmtest$

Step 3: Create the 'main.js' file where the code will utilize Fibonacci number generator.


hxce@ubuntu:~/Desktop/npmtest$ touch main.js

Step 4: Implement the 'main.js' file.


var fibonacci = require ('fibonacci');
var bigNumber = fibonacci.iterate (3000);
console.log (bigNumber);


Step 5: Run the 'main.js' file.


hxce@ubuntu:~/Desktop/npmtest$ node main.js
{ number: '410615886307971260333568378719267105220125108637369252408885430926905584274113403731330491660850044560830036835706942274588569362145476502674373045446852160486606292497360503469773453733196887405847255290082049086907512622059054542195889758031109222670849274793859539133318371244795543147611073276240066737934085191731810993201706776838934766764778739502174470268627820918553842225858306408301661862900358266857238210235802504351951472997919676524004784236376453347268364152648346245840573214241419937917242918602639810097866942392015404620153818671425739835074851396421139982713640679581178458198658692285968043243656709796000',
length: 627,
iterations: '3000',
ms: 565 }



Phase 2

Step 1: Create 'main.js' file. This is the file which you will use to implement an npm module from scratch.

hxce@ubuntu:~/Desktop/npmtest$ touch main.js


Step 2: Implementing the localStorage method in 'main.js' file.


"use strict";

var localStorage = (function() {
var storage = {};

return {
setItem: function(key, value) {
storage[key] = value || '';
},
getItem: function(key) {
return storage[key] || null;
},
removeItem: function(key) {
delete storage[key];
},
getlength() {
return Object.keys(storage).length;
},
key: function(i) {
var keys = Object.keys(storage);
return keys[i] || null;
}
};
})();

module.exports = localStorage;

Step 3: Create the 'package.json' file. This file is useful while publishing the npm module so you can set the license type, set your name as the author, add key words and set other useful information.


hxce@ubuntu:~/Desktop/npmtest$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (npmtest) local-storage-mock
version: (1.0.0)
description: localStorage mock for NodeJS.
entry point: (main.js)
test command:
git repository:
keywords: localStorage local_storage storage local
author: Hussain Mir Ali
license: (ISC) MIT
About to write to /home/hxce/Desktop/npmtest/package.json:

{
"name": "localstorage",
"version": "1.0.0",
"description": "localStorage mock for NodeJS.",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"localStorage",
"local_storage",
"storage",
"local"
],
"author": "Hussain Mir Ali",
"license": "MIT"
}


Is this ok? (yes)
hxce@ubuntu:~/Desktop/npmtest$

Step 4: Create a 'README.md' file and add the following text, but in your own module you can change the name of the author.


# Local Storage
#[Author: Hussain Mir Ali]
Local Storage mock for testing browser code in NodeJS.

#To use the project:

run: npm install -g local-storage-mock

#Sample usage:

```javascript
var lstorage = require('local-storage-mock');

lstorage.setItem('Hello', 'World');
lstorage.setItem('Node', 'JS');
lstorage.setItem('Weight', 100);
lstorage.setItem('Car', {'Engine': 'V8', 'Make': 'BMW', 'Year': 2013});
lstorage.setItem('Words', ['Pen', 'Pineapple', 'Apple','Pen']);

console.log(lstorage.getItem('Hello')==='World');//true
console.log(lstorage.getItem('Weight')===100);//true

console.log(lstorage.getlength() === 5);//true

lstorage.removeItem('Hello');

console.log(lstorage.getlength() === 4);//true

console.log(lstorage.key(0) === 'Node');//true


Step 5: Create an account on npm  https://www.npmjs.com/signup  .

Step 6: Login to npm account.

hxce@ubuntu:~/Desktop/npmtest$ npm login
Username: husenxce
Password:
Email: (this IS public) miralihussainkhan@gmail.com

Step 7: Publish the newly created npm module.

hxce@ubuntu:~/Desktop/npmtest$ npm publish
+ local-storage-mock@1.0.0



Demo Link: https://www.npmjs.com/package/local-storage-mock