Package Management using NPM

Hey folks! Here I am again with my short tips if you are a developer, in today's tutorial I'll show you how easily you can find node packages using the NPM package manager, but before let's take a look at the definition of NPM according to the official website:

npm is the world's largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well.

The first step is to install Node.js if you do not have it already installed. In order to do so, go to the official page by clicking here and download the one for your OS.

Once installed, open your terminal to verify the npm and Node.js versions:

node -v -> v16.14.2

npm --version -> 8.5.0

Now, let's say that we want to create a new project and install some dependencies:

Initialize a project


npm init will initialize the project, and will ask you for a few additional information of the project you are about to create.

NPM Project Initialization

After you confirm that you are done with your configuration, npm will create a new file in the root of the folder where you are running the command, that file is package.json and looks like this:

NPM package.json

As you may notice the package.json is a file that contains all the information about your configuration, it is very important because here is where you can add commands, install dependencies, add versions of your project and so on.

Install Dependencies


NPM has tons of plugins, libraries, frameworks, etc. where you can simply download and use in your next small, medium or even big project. You can take a look here: https://www.npmjs.com/

but, how to install a package? 😫

That is pretty easy, just follow this command:

npm i <libraryName>@version

As many commands, you could use some flags:

  • -g: Install a global package, this means that you are going to have that package on your computer, not as a dependency of your project.
  • --save, -S: This means that we want to install the package as a production dependency of our project.
  • --save-dev, -D: With this command, we are telling npm that we need the packages only available on development mode.
  • --dry-run: This simulates the installation of a package.
  • --force, -f: force an NPM package installation.
  • install, i: You can install all the packages within a package.json file by running this command.

Let's see an example, suppose that you are going to create a new web project where you need to add an accordion, you can go to the npm website and look and type accordion https://www.npmjs.com/search?q=accordion, then you are going to get many suggestions from developers around the globe, pick the one that you prefer.

I am going to use one that I published 3 months ago, it is called accordion.hs (I know the name it's not that good) 😄

Installing Accordion.hs

What happened?

Ok, if you now go to your editor you will notice a new folder node_modules guess what? yes,. NPM stores the files that you just downloaded in that folder, in fact NPM will download all the libraries of your project in that folder.

Node Modules

Also, the package.json has a new section:

dependencies

...interesting, huh?

List packages added globally

npm list -g --depth 0

Packages Added Globally

Uninstall a package

There are two ways:

npm uninstall <packageName> this will remove the package from node_modules and package.json.

npm uninstall <libraryName> --no-save this will remove the package only from node_modules.

Package.lock

Info related to the versions of your packages.

What are NPM Scripts

We can run n quantity of commands in our configuration, there is a section in the package.json file to do that. For example, these are the commands I use to publish my website:

Héctor Sevilla Scripts

The information above it's only basic information so you can start and understand the first steps of using npm, if you want to discuss more about this topic do not hesitate on sending me an email, DM, or keep following my posts, I'll be sharing more interesting information about the amazing programming world. See you next time! 👋