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


Source Code ScreenCast

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1621505113001/V9SvkapNe.png)

## 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](https://github.com/bmshamsnahid/Build-Publish-And-Test-A-Node-Package) and [video screencast](https://www.youtube.com/watch?v=xt-qfDGZd0o) links.

## Procedures:

1. First, we create the package.

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

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

## Prerequisites:

1. Make sure you have installed latest [Node.js](https://nodejs.org) and [NPM](https://nodejs.org)

1. **Create an account in [npm](https://www.npmjs.com)**.

1. 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
```


2. Enter to the directory

```
cd magic-directory
```


3. 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.

```json
{
  "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"
}

```

4. Create a file named ‘index.js’ in your root directory

```
touch index.js
```


So your project directory should be similar to this

```javascript
├── ...
├── index.js                    # Project entry point
├── package.json                # Project dependency
├── ...
```

5. Open index.js in your favourite text editor and create two methods

```javascript
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](https://www.npmjs.com/) 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

```javascript
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.
