npm Basics: Installing and Managing JavaScript Packages

Frontend ~ 8 minutes

# Welcome to npm

Hello and welcome! In this tutorial, we will learn npm. The name npm means Node Package Manager. It is a tool that helps you download, update and remove code that other developers have shared.

This shared code is called a package. For example, there are packages for web servers, date formatting, and CSS tools. Instead of writing everything from zero, you can install a package and save a lot of time.

What you will learn

  • How to check that npm is installed
  • How to create a new project with package.json
  • How to install, update and remove packages
  • How to run scripts from your project

Do not worry if you are new. We will go slowly, one small step at a time. Let us begin!

Check the Node.js version
Code Copied to Clipboard

# Step 1: Check your tools

npm comes together with Node.js. So, if Node.js is installed, npm is installed too. If you do not have it yet, please download the LTS version from nodejs.org. LTS means long term support, and it is the safest choice for beginners.

In the command above, we ask Node.js to show its version. You should see a number like v20.11.0. Any recent number is fine. Now, please run the next command to check npm itself.

Check the npm version
Code Copied to Clipboard

If the command is not found

If your terminal says command not found, close the terminal and open it again. The installer needs a fresh window to find the new tools. If it still does not work, please install Node.js again and restart your computer.

You can also read the official guide at docs.npmjs.com for help on every system.

# Step 2: Create a project

Every npm project has a small file called package.json. Think of it as an ID card for your project. It stores the name, the version, the scripts, and the list of packages you use.

Let us make a new folder first. Create a folder named my-first-app and open it in your terminal. After that, run the command below. The -y flag means "yes to all questions", so npm will use safe default answers.

Create package.json
Code Copied to Clipboard

# Understanding package.json

Great job! npm has created the package.json file for you. Let us look at what is inside. The most important parts are the name, the version, and the scripts. Later, a dependencies part will appear when you install packages.

Below is a small example. Your file may look a little different, and that is completely fine.

Example package.json
Code Copied to Clipboard

# Step 3: Install a package

Now, please let us install our first package. We will use Express, a very popular tool for building web servers. The command is npm install followed by the package name.

You can search for any package at npmjs.com. Each package page shows the install command, the documentation, and how many people download it every week. This is a good way to check if a package is trusted.

Install Express
Code Copied to Clipboard

# What changed in your folder

After the install finishes, you will see three things in your project.

New items in your folder

  • A node_modules folder, which holds the downloaded code
  • A package-lock.json file, which saves the exact versions
  • A new dependencies section inside package.json

The node_modules folder can be very big. Do not be afraid of it. npm also downloads the packages that Express needs, and this is normal.

Never edit or share node_modules

Please do not edit files inside node_modules. npm can replace them at any time, and your changes will be lost.

Also, do not upload this folder to Git. Add node_modules to your .gitignore file instead. Your teammates can rebuild the folder anytime by running npm install. Please do keep package.json and package-lock.json in Git, because they describe exactly what your project needs.

Install a development package
Code Copied to Clipboard

# Dependencies and dev dependencies

Some packages are needed when your app runs. Others are only helpers while you write code. npm keeps them in two different places.

The two kinds of packages

  • dependencies: needed by your app in production, like Express
  • devDependencies: needed only while developing, like nodemon

We used the --save-dev flag above. The package nodemon restarts your server by itself every time you save a file. It is a helper, so it belongs in devDependencies.

List installed packages
Code Copied to Clipboard

# See what you installed

The command above shows the packages you installed, without the long list of their helpers. The --depth=0 part tells npm to show only the top level. It is a quick way to check your project.

Now, let us make our work easier with scripts. Scripts are short names for long commands. You add them to the scripts part of package.json.

Adding scripts
Code Copied to Clipboard

# Step 4: Run a script

We now have two scripts: start and dev. To run a script, you use npm run and then the script name. For start, you can even skip the word run.

Let us run the dev script. Please make sure you have an index.js file first. Even a file with one line, like console.log('Hello'), is enough for this practice.

Run the dev script
Code Copied to Clipboard

Why scripts are useful

Scripts hide long and hard commands behind one easy word. Your whole team can type npm run dev and get the same result, even if each person uses a different computer. Many frameworks, such as React, Vue and Laravel Mix, use scripts for building and testing too.

Update packages
Code Copied to Clipboard

# Step 5: Update and remove packages

Packages get new versions often. The command above updates your packages to the newest version that your package.json allows. This keeps your project safe and fast.

To see which packages are old, you can run npm outdated. It shows a small table with the current, wanted and latest versions. Now, if you do not need a package anymore, you can remove it. Let us remove Express as an example.

Remove a package
Code Copied to Clipboard

# Understanding version numbers

You may have noticed the ^ sign before a version, like ^4.19.2. It is part of semantic versioning. A version has three numbers: major, minor and patch.

How to read 4.19.2

  • 4 is the major number. It changes when old code may break.
  • 19 is the minor number. It adds new features safely.
  • 2 is the patch number. It fixes small bugs.

The ^ sign tells npm: "You may update minor and patch numbers, but please keep the same major number." This is a safe rule for most projects.

Check for security problems
Code Copied to Clipboard
Install only packages you trust

Anyone can publish a package on npm, so please be careful. Before you install a new package, check these points on its page at npmjs.com.

A quick safety check

  • Does it have many weekly downloads?
  • Was it updated in the last year?
  • Does it have clear documentation and a link to its source code?

The npm audit command above scans your project for known security problems. Run it from time to time, and read the report calmly. Many problems can be fixed with npm audit fix.

# Summary and next steps

Well done! You have finished this tutorial. Let us quickly remember what we learned.

What we covered

  • npm is the package manager that comes with Node.js
  • npm init -y creates a package.json file
  • npm install adds packages, and --save-dev adds helper packages
  • npm run starts the scripts you define
  • npm update, npm uninstall and npm audit keep your project clean and safe

Where to go next

  • Build a small web server with Express
  • Learn about npx for running tools without installing them
  • Read the full guide at docs.npmjs.com

Please practice these commands in a test folder. Practice is the best teacher. See you in the next tutorial!