Understanding NPM

Sayeem Md Abdullah
4 min readAug 13, 2022

--

What is npm?

NPM which stands for Node Package Manager is the biggest software registry in the world. It contains more than 1.3 million packages, both public and private, and it is free to download all npm public packages without registering.

Why do we need a package?

An iPhone is made using parts from all over the world, made by different companies. iPhones do not make all their parts from scratch; they order them from different companies and assemble them. Similarly when developing a project, one does not need to create everything from scratch, such as a framework, a library or a component, etc. For example, if we are developing a React application and want to add a drag-and-drop function for users to upload files. It is not necessary to build that from scratch; we can simply use the react-dropzone package. Similarly, the npm registry has millions of packages, making developers’ lives easier.

How to install npm?

NPM is installed with Node, so there is no need to install it separately.

How to install an npm package?

The following command will download and install an npm package.

npm install <package>

In order to install a specific version, we have to specify it as follows

npm install <package>@<version>

We can also just write i instead of install.

How to install an npm package globally?

npm install --global <package>

We can also write -g instead of --global.

How to install an npm package and save it as a dependency?

npm install --save <package>

We can also write the above comment in shorthand as follows:

npm i -S <package>

Whenever we save a package as a dependency, it is automatically listed in the package.json file under dependencies and will be needed when we run our code. There is no need to specify —-save or -S anymore as npm install <package> is enough.

How to install an npm package and save it as a devDependency?

npm i --save-dev <package>

We can also write the above comment in shorthand as follows:

npm i -D <package>

The devDependencies are intended to be used only for development purposes, and they may be required at some point during the development process, but not during the execution of the code.

During production, the development dependencies are installed and saved, since npm assumes it is a development deployment, so we use the following command to stop this.

npm install --production

How to install project dependencies?

We may need to install dozens of packages in a project to get it working properly, which is hard to do manually, so we can run this command to install the dependencies based on the package.json file. When specifications change and we run the command again, it will install packages according to the updated specifications.

npm install

How to execute a script?

We use the following command to run the scripts mentioned in package.json.

npm run <script>

About the files and the folder

node_modules: An automated folder that is generated when we run the npm i command. This folder contains the modules and libraries we are importing into our project. We never push this folder to git since it is so large, so anyone who wants to clone the project can just run npm i, and the node_module will be generated according to the package.json.

package.json: A JSON manifest file stored in the root of a Node project contains all the information about the application, such as its dependencies, scripts, versions etc.

package-lock.json: An automated generated file which generates for any operation modifying either the node_modules tree or package.json file, which describes how the exact tree was generated, enabling subsequent installations to have the same tree.

How to initialize a new package?

Run the following command and answer a few questions to initialize a package.

npm init

After we are finished, a package.json file will be generated and we can install the dependencies accordingly.

How to publish a package?

Our first step is to create an npm account. Then, we’ll login from our terminal using the following command.

npm login

The following command checks if we are logged in.

npm whoami

Now we can navigate to our project folder and simply use the following command.

npm publish

The complete process of publishing NPM packages will be covered in another blog, but now you have a general understanding.

Thanks so much for reading! I will soon be posting more topics that you might find interesting. Please feel free to comment below or contact me if you think I should add, correct, or remove anything from this article.

--

--

Sayeem Md Abdullah

A Software Engineer with a passion for Storytelling and Filmmaking