CLOSE

   NodeJS  JavaScript  NPM  
   Technology    Programming

Node Package Manager (NPM)

         
 JANUARY 4, 2020   by Bhaveshm23

Node Package Manager(NPM) can be divided into two parts:

  1. It is a repository for publishing open-source Node.js projects.

  2. It is a command-line tool used for package installation, version management, and dependency management.

NPM is a package manager for other open-source JavaScript frameworks such as Angular, React, Gulp, jQuery, etc.

The NPM comes along with the Node.js installation. You can verify the NPM by writing the following command on the command line which will give you the installed version of NPM.

C:\>npm -v
6.18.0

C:\>

If you are using an older version of NPM and want to update it you can do so by using the following command:

npm install npm -g



Using NPM to create a new NodeJs Project

To create a new node.js project we use the npm init command. The command is written in the folder where the user wants to create a new project.

npm init command to create node Js project

When you type the npm init command, you will be asked for the package name, version number, description, entry point of the application, test command, git repository, keywords, author, and license. These are optional and you can skip them by pressing enter or enter the information when you want to change something. For example, I have changed my entry point to app.js from index.js which is the default entry point(see in the picture above).

The npm init command creates a package.json file which contains all the information added by you for the new node.js project.

Example of npm init creating package.json file

The package.json file looks like this and it can be edited if required.

example for package.json file

It's a good practice to add a "start" value which specifies which file to run when your node.js application is run. So when you share your code with someone they will simply type npm start instead of node app.js (which is the entry point of the app). They won't have to look for which is your entry file.

adding start value to package.json

E:\Node.jsNPM> npm start

> [email protected] start E:\Node.jsNPM
> node app.js

You can add your own custom name like "start-server" instead of "start" but you will get an error "not a known command" when you type npm start-server to start your application as the set of commands that can be used with npm is pre-defined.

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start-server":"node app.js"
}

node npm commands

So in order to run a custom command, we can use the npm run command with our custom command, for example,

E:\Node.jsNPM> npm start-server

> [email protected] start-server E:\Node.jsNPM
> node app.js



Global vs Local Package Installation using NPM

NPM is used to install third-party packages that aren't included in the node's core module. These packages are available through the NPM repository. NPM global installation is for the whole computer ie. the global mode performs the operation which affects all the Node.js applications on the computer, while the local mode performs an operation for a particular local directory(a particular project) which affects the application in that directory only.


Install packages locally

To install any package locally you can use the following command,

npm install <package-name>

For example, to install express

npm install express

All the modules installed using npm are installed under the node_modules folder. This will create a folder of express under node_modules.


Adding the Dependency into package.json

To add the dependency into package.json you need to use --save at the end of the install command.

For example,

npm install express --save

The above command will add the dependency in the package.json file like following,

"dependencies": {
    "express": "^4.17.1"
}

You can install multiple packages using a single command too, like this,

For example,

npm install express body-parser mongoose --save

This will install all the packages and will add them to the package.json file too,

"dependencies": {
    "body":"^5.1.0",
    "express": "^4.17.1",
    "mongoose":"^5.7.14",
    "parser": "^0.1.4"
}

Install packages globally

To install the package globally you need to use the install command with -g flag, like below,

npm install -g <package-name>

The above command will install the package globally so that all the Node.js applications on the computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modules folder.


Update a package

Whenever you want to use the updated version of the package you can simply use the command

npm update <package-name>

Uninstalling package

To uninstall a package using below command,

npm uninstall <package-name>



Conclusion:

Since NPM is an online repository of open-source packages that can be found on npm it has many advantages. Suppose you are working on something and every time you make changes you need to restart the server that's where nodemon comes to rescue. Nodemon is a tool that automatically restarts the server whenever some changes are detected.

Another use of npm is dependency management. Your node.js project's package.json contains the dependencies so when you share your project with someone to get those dependencies they simply need to write npm install. This installs all the dependencies needed for the application to work.


SHARE YOUR THOUGHTS WITH US!