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

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.

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

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

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

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.

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

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.

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.

Command to uninstall modules in Node.js using npm

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

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.