Shams Nahid
Shams Nahid's Blog

Shams Nahid's Blog

Build and Publish your first NPM package in less than 5 minutes!!!

Build and Publish your first NPM package in less than 5 minutes!!!

Shams Nahid's photo
Shams Nahid

Published on Sep 19, 2018

3 min read

Source Code ScreenCast

Intro

Here we are going to create a minimal node package. It will contain only two methods.

If you think this is a rocket science, then let's build one and have fun. The most interesting matter is, it would take less than 5 minutes to build and publish one. We will then test out own module using npm package manager.

I also added the source code and video screencast links.

Procedures:

  1. First, we create the package.

  2. Then publish to the Node Package Manager(npm)

  3. Finally, we install the package from npm and test it.

Prerequisites:

  1. Make sure you have installed latest Node.js and NPM

  2. Create an account in npm.

  3. Don’t forget your Username, Password, and Email (LOL !!!).

Build the package (2 steps)

Step 01 : (Log in to local machine)

First, log in to npm in your local machine.

npm login

Put the username, password, and email, you used to create the npm account.

Step 02: (Build the package)

Create a new node project in a new directory.

  1. Create a directory, named magic-directory.
mkdir magic-directory
  1. Enter to the directory
cd magic-directory
  1. Make a node project
npm init

Follow the below convention to create the project. For random package name, version or entry point can create issues for publish the package.

  • As package name, *username-magic-module (it has to be unique, don’t use any special character)*

  • As for first release version should be 0.0.1

  • As for the description, This is my magic module

  • As for the entry point, index.js

  • Skip the test command for now

  • Skip the git repo for now

  • Skip keywords for now

  • As for the author, put your full name

  • Skip licensing for now

    Make sure your package.json is very much similar to the following package.json, except the name and author property. The name property should have ‘your-name’ prefix.

{
  "name": "your-user-name-magic-module",
  "version": "0.0.1",
  "description": "This is my magic module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "B M Shams Nahid",
  "license": "ISC"
}
  1. Create a file named ‘index.js’ in your root directory
touch index.js

So your project directory should be similar to this

├── ...
├── index.js                    # Project entry point
├── package.json                # Project dependency
├── ...
  1. Open index.js in your favourite text editor and create two methods
const magicString = () => {
    return 'Hello World';
};

const displayMyName = (name) => {
    return `Your name is ${name}`;
};

module.exports = {
    magicString,
    displayMyName
};

Congratulation!!! You just created ypur first node module. Time to publish it.

Publish the module

npm publish

Your node module is published !!!

To find your published package, first, go to npmjs and then from menu, go to the package section.

Test Module

To test your module,

Create a directory, ‘test-my-magic-module’

mkdir test-my-magic-module

Create node project

npm init --yes

Install your module

npm i your-newly-published-module-name

Create a file named ‘index.js’

touch index.js

Now your index.js file should look like

const myMagicModule = require('myNewlyCreatedModuleName');
const magicStringOutput = myMagicModule.magicString();
const displayNameOutput = myMagicModule.displayMyName('Shams Nahid');

console.log (magicStringOutput);
console.log (displayNameOutput);

Run the project

node index.js

You should see the magic string and the name we provided.

Conclusion

Stay tuned and if there is a confusing term or something, response below. I will replay ASAP.

 
Share this