Sometimes you’re left wondering, “What is NPM”? This article is a gentle introduction into what NPM is and what it does for you.

You may have seen tutorials online where they tell you that you need to install something using NPM. Generally, you’re given the command to copy and paste, and without really thinking, you do so.

However, what’s really going on whenever you’re using NPM? When you copy and paste commands from a tutorial, do you truly understand what they mean?

What is NPM?

NPM stands for Node Package Manager and is one of the multiple package managers (others include Yarn, Bower, etc.) available for the Javascript programming language. Furthermore, it’s the default package manager for Node.js.

NPM has a command-line client, which is generally how you install packages. What seems magical is how NPM knows what to download. All you do is give the name of the package you’d like, and NPM knows where to get it from. This is because NPM has an online database of public and paid-for private packages called the NPM registry.

Image for post

The NPM registry can be accessed online.

So whenever you type in something like:

$ npm install styled-components

It will look for styled-components as the key and find where to download the package from the online database. NPM takes care of downloading and installing Styled Components for you.

What is a Package Manager?

Essentially, a package manager is a collection of software tools that a developer can use to automate managing packages in a standardized manner.

Image for post

Imagine having to manually download libraries such as React, Redux, Styled Components to get your project running. You’d have to check each package’s version number and get the correct dependencies as well. That doesn’t really sound like time well-spent for a developer.

That’s where NPM comes in, as a software tool, and helps us, developers, out. We no longer have to manage third-party packages for our project manually. It’s all been made easy with NPM.

Thanks, NPM.

What Purpose Does NPM Serve?

Like mentioned before, NPM helps with managing packages for our project. Since NPM automates managing packages, developers can focus more on developing and less on packages’ maintenance.

So it acts as an assistant to the developer. How a doctor diagnoses a problem and tells his or her assistant to get a specific tool, you will be able to diagnose a bug, determine the best possible solution for it and have NPM retrieve the packages, otherwise known as the tools.

Image for post

Developers picking out third-party packages

High-Level Overview of NPM

As a general rule, any project that is using Node.js will need a package.json file. The package.json file is basically the control panel for NPM. This is where you tell NPM which libraries you want to import, information about source control, project metadata, and more.

Whenever you run npm install, NPM will look to your package.json file and look at what libraries you want to import.

Before we go any further, though, we should understand the difference between a local and global installation. There is a slight distinction that plays a part in the package.json file.

Local Installation

A local installation downloads the package files into your project’s folder. The package files are installed “locally” to your project.

You may sometimes notice a node_modules folder in your projects. This is the folder that holds those downloaded files. As you add more packages to your project, this folder will get heavier.

Whenever you are working with Git, it is recommended not to push the node_modules folder because of how large it can get. Rather the recommended action is to use the --save flag whenever installing a package, so it gets tracked in your package.json file. Instead of pushing the entire node_modules folder, you will push the package.json file that contains all the packages to download. Teammates can download the package.json file and locally install the node_modules folder themselves.

Global Installation

Think of a global installation as if you’re downloading the package to your computer so you can use it outside of your current project. You can globally install a package by using the -g flag when installing. So, for example, if you wanted to install the Webpack CLI globally, you would use the following command:

$ npm install -g webpack-cli

This will allow you to use the Webpack CLI anywhere on your computer. NPM takes care of hooking things up with your operating system to be used outside of your project.

When to Use Global or Local

There are a few rules of thumbs that you can follow to know when to install globally versus installing locally.

  1. If the packages include command-line tools, download them globally.
  2. If the packages are to be used for your project, download them locally.
  3. If you need to do both, install both globally and locally.

What is the NPM Registry?

The NPM registry is a database collection of open-source packages for Node.js, front-end frameworks, and Javascript in general. When you install NPM onto your machine, the default registry URL is set to https://registry.npmjs.org/

If you put that link into your browser, you’ll get back a JSON object that gives some information about the NPM registry.

Whenever you run npm install <package-name> you are telling NPM to look for the package name through the registry URL. This is how NPM can find the packages that you are looking for.

#javascript #npm #node #programming #developer

A Beginner’s Guide to NPM, the Node Package Manager
1.85 GEEK