npm in Node.js

What is npm in Node.js?

Node Package Manager (npm) as the name indicates it is used to install the Node packages. Also if you use it in development it is easier to specify and link dependencies. If you’ve been working with Javascript for a while, you might have heard of npm: npm makes it easy for Javascript developers to share the code that they’ve created to solve particular problems, and for other developers to reuse that code in their own applications.

Basically, npm is a way to reuse the code from the other developers and also to share your code with them. It has a provision to create versions of your code which helps developers to manage different versions of code.

npm Getting Help

npm has a very good documentation about all the commands. It provides the syntax of all the commands and the option available to execute. There are 2 ways you can get help.

Syntax

npm help access

This will open the browser and provides you the details about the npm access command. The detailed documentation consists of Syntax/Synopsis, Description, and details about the command usage and execution.

npm access --help

Running the above command will provide you the quick help in the command line on how to use the npm command.

Installing Modules using npm

We can install node.js modules using npm command. Node.js has very good modules available which solve most of the basic needs. You could also build a custom module using JavaScript and package it.

Syntax

mpm install <Module Name>

For example, if you would like to install the express framework in Node.js you can install by running the below command.

npm install express

Once the installation is finished you can use the express module in you JS file as below.

var express = require('express');

Global vs Local Installation

By default, npm installs all the node modules locally. The local mode refers to the package installation in node_modules directory lying in the folder where node.js is installed. Locally deployed package are accessible using the require method in the JS files. For example, when we installed the express module, it created node_modules directory in the current directory where it installed the express module.

Command to see the Local installed Modules in Node.js

If you want to verify all the modules installed locally in node.js run the below command and it will display all the modules installed using npm locally.

npm ls -l

How to Install Node Modules Globally?

Globally installed packages/dependencies are stored in the system folder. Such dependencies can be used in the CLI(Command Line interface) function of any node.js but it cannot be imported using the require() method in Node application.

Command to install Node Module Globally using npm

npm install express -g

Command to see Globally installed Modules in Node.js using npm

If you want to verify all the modules installed globally in node.js run the below command and it will display all the modules installed using npm globally.

npm ls -g

Command to update modules in Node.js using npm

Update the package.json in the directory to the version you require and run the following command to update the module.

npm update express

Command to uninstall modules in Node.js using npm

npm uninstall express

Once you run this command you could navigate to the directory node_modules or by typing the npm ls command

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Node.js

Introduction to Node.js

Table of Contents Hide What is Node.js?Who Developed Node JS?Node.js OS supportFeatures of Node.js#1. Event Driven and Asynchronous#2. Super Fast.#3. Single Threaded and Highly Scalable#4. No Buffering#5. Easy to LearnWhere…
View Post