Popular Categories

Demystifying: Node Package Manager Aka npm

Using npm (node package manager) effectively is a cornerstone of modern web development, no matter if it’s exclusively with Node.js, as a package manager or build tool for the front-end development, or even as a piece of workflows in other languages and on other platforms.

As a beginner, understanding the core concepts behind npm could be somewhat difficult – I had to spend hours just to figure out small details.

Her is a detail for Demystifying Node Package Manager Aka npm, a guide for those who are entirely new to Node.js, npm, and the surrounding ecosystem.

Prerequisites:

Before proceeding ahead make sure you have installed Node.

Basic Concept:

npm consists of three components:
1. Website: npm official website is https://www.npmjs.com/. Using this website you can find packages, view documentation, share and publish packages.
2. Registry: npm registry is a large database consisting of more than half a million packages. Developers download packages from the npm registry and publish their packages to the registry.
3. CLI (Command Line Interface): When using npm, you are most likely going to be using the command line tool for the majority of your interactions.

Installing Node:

npm comes with the node.js. So, you don’t have to install it separately. You can install node.js from their official website https://nodejs.org/en/download/.

After installing node, You can check the version of node and npm by going into the command prompt:
node -v
npm -v

Demystifying: Node Package Manager Aka npm

Understanding package.json:

The package.json is the project manifest file. Using package.json you can manage dependencies and write scripts. It has all the meta data about the project.

Creating package.json:

First, head over to your project folder. You can create package.json from the command:

npm init

The npm init command is a step-by-step tool to scaffold out your project. It will prompt for you for several details like: The project’s name, The project’s initial version, The project’s description etc. Once you are through to these steps your package.json file will be created in the folder where you had this command run.

In-case you don’t want to fill out the brief details, then simply use:

npm init --yes 

This will Instantly Initialize a Project without prompting for several questions.

The Essential npm Commands:

1. Installing Modules/Packages

Installing a modules/packages from npm is one of the most basic things you should learn to do when getting started with npm. Here is the syntax of the command:

npm install < module >

In the above command, you will need to replace with the name of the module you wish to install. For example, if you wish to install Express, then the above command will become like:

npm install express

Once the command is over CLI, it will install the express module into /node_modules in the current directory.

The above syntax is referred to as ‘locally installed package’ where package can be accessed only on the folder you’ve downloaded it.

If you add -g to the above command, it will make the installation global which will mean that the packages works anywhere on the machine.

npm install < module > -g

2. Updating packages:

Sometimes we need to update our packages to get new features. To do that, you’ve to use the following command syntax:

npm update < package_name >

The above command is for a specific package. Incase if you wish to update the entire list of packages, simply use the following command:

npm update

For global packages, you’ve to use -g.

npm update < package_name > -g

3. Uninstalling packages:

Sometimes you don’t need a particular package and you want to remove it. It’s not a good idea to manually remove the package from the node_modules folder as it can be a dependency for the other packages. So, to safely remove a package you’ve to use the command:

npm uninstall < package_name >

For global packages,

npm uninstall < package_name > -g

4. Installing from package.json:

If you want to share your project then you may not want to share all your node modules. You can just share your package.json which contains the packages needed for your project. Then it can simply be installed by the following command:

npm install

This command will download all the packages the project needs.

5. List installed packages:

To get the list of installed packages, use the command:

npm list

6. Getting help:

npm CLI has built -n help command. You can access it by:

npm help

To get help for a particular command, use the command:

npm  -h

You can also search npm documentation for help. To do that use the command:

npm help-search < command >

Conclusion:

Now you’ve learned all the basics of npm. To know more you can refer to the official documentation at their website (https://docs.npmjs.com/).