Node Package Manager (NPM)
Tags: npm, node
Published: 2022-10-08 07:59:49

---
title: Node Package Manager (NPM)
published: true
date: 2022-04-20 15:02:43 UTC
tags: npm, nodejs
canonical_url: https://sainath.tech/node-package-manager
---

### Need for NPM

Node.js has got built-in modules for accessing the file system, networking, and other I/O operations. The problem is it offers only essential packages as core modules. We often want to use some external packages to re-use code and avoid complexity. Node has a way to cater to these packages through the node package manager ([npm](https://www.npmjs.com/)). NPM offers a fleet of packages that are highly used in production-grade apps. [Express](https://www.npmjs.com/package/express) is one such example that has seen a wider adoption. We will learn more about express.js in an upcoming post, stay tuned to this Node.js series. Also, there are many notable packages like _react, react-dom, underscore, vue, axios, body-parser_, etc.

### Initiate NPM

To start with npm one can straightaway begin to use it. It is by default installed during the node.js installation. However, if you want to check the version, you can do it in the terminal.

```
npm --version

```

The first thing one has to do to import npm packages is to create a folder and initiate npm in it. To initiate npm, navigate to your project folder in the terminal (in the case of windows, use the command prompt) and run `npm init`. You will get a list of questionnaires to which you can input your preferences. In the interactive terminal, if nothing is filled it will populate a default value for certain fields like package name, version, and leaves it blank for description, author, etc. Press enter to move on to the next item. In the end, a JSON is created with a list of key-value items that you gave as input.

![npm init](https://cdn.hashnode.com/res/hashnode/image/upload/v1650451881085/GhjWxTCk8.png)

A short way to quickly do the initiation is by running `npm init --yes`. The `yes` flag will populate default values and skips the interactive questions to the user.

![npm init yes](https://cdn.hashnode.com/res/hashnode/image/upload/v1650451945822/BbTz9wrzM.png)

### Installing a package

The _package.json_ created by npm can be found inside the project folder. This file is a snapshot of the list of dependencies for the project. **It is necessary and be cautious to not delete this file**.

![package.json](https://cdn.hashnode.com/res/hashnode/image/upload/v1650452093828/guBsWg4La.png)

The packages can be installed using npm commands.

```
npm install <package-name> --save

```

This command will add the package to your local project and the scope remains local i.e. you can't access the package outside of this folder.

Let's go ahead and install a package for our project. I am going to install [chalk](https://www.npmjs.com/package/chalk) which helps in rendering colorful output in the console and this will be useful when you design CLI-based applications.

```
npm install chalk --save

```

If everything goes well, the package gets added to the project. If you are getting errors while downloading, check your installation and head to the official project page to see if there are any issues reported.

![chalk installation](https://cdn.hashnode.com/res/hashnode/image/upload/v1650458346078/CVs_YAgW3.png)

You can find the package name and version in the package.json under dependencies.

![package-dependencies](https://cdn.hashnode.com/res/hashnode/image/upload/v1650459915112/bl0LQfSCZ.png)

### node\_modules

You can see an additional folder named node\_modules got added to our project. It consists of files related to the dependencies that were installed. The size of node\_modules is huge and it is downloaded to your machine. You can often find the node\_modules folder ignored while pushing your code to the repository. It can also be deleted and a fresh installation of packages can be triggered. Once you have package.json in place, it is no longer necessary to mention the package name to reinstall. All the details of the package and its version are available in the package.json and the installation will pick it up from there.

```
npm install

```

### Dependencies

There is an option to segregate dependencies based on usage. Not all packages are necessary to be installed, some are meant for development purposes like the [nodemon](https://www.npmjs.com/package/nodemon) package. Nodemon keeps a watch on your file changes and automatically restarts the node application. This need not be added to your production environment as a dependency as you won't be making any file changes in production. To save a dependency for development purposes, add a flag `--save-dev` instead of `--save`. In contrast to local, there is an option to download a package globally to be used by any project by appending the flag `-g` or `--global` to the installation package. It is available globally but limited to your machine where it is installed. It is not necessary to install a global package in a project as is it a one-time installation and it will be available to use when imported.

```
npm install nodemon --save-dev
npm install nodemon -g

```

### Using the installed package

To use a package it should be imported. Different module formats have their unique way of importing. The most known formats are CommonJS(CJS) and ES2015 Modules(ESM). There are also other formats like Asynchronous Module Definition (AMD) and Universal Module Definition (UMD) which you can explore in detail. Packages that use the ESM format insist to import the format using `import` and the CJS format needs to use `require`.

```
#ESM 
import chalk from 'chalk'

```

To use chalk as CJS format, the [documentation](https://github.com/chalk/chalk) specifies to downgrade the version as the latest version supports EJS format.

> Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now.

```
#CJS
const chalk = require('chalk')

```

In our example, I have used the ESM format to import the chalk module. An additional entry of `type: "module"` has to be provided in package.json for using ESM format. ![Screenshot 2022-04-20 at 19.52.14.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650464615948/eInCWCIC-.png)

Create an _index.js_ file and import the 'chalk' module. Chalk can now be used to log your output colorfully. Check more about chalk and try it yourself.

![Screenshot 2022-04-20 at 19.51.41.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1650464631580/QQBAaEO0K.png)

Hope this post helped you in understanding the usage of npm. This post only caters to the essentials, you can go deep and learn about the detailed usage of npm on their official page.

## References

- [NPM Official](https://www.npmjs.com/)
- [NPM Documentation](https://docs.npmjs.com/)
- [Javascript Module Formats](https://code-trotter.com/web/understand-the-different-javascript-modules-formats/)
- [Chalk GitHub Page](https://github.com/chalk/chalk)