Mastering Node.js: How to Run NPM Outside of the Node REPL in Your Normal Shell

Node.js is a powerful JavaScript runtime that allows you to run JavaScript code outside of the browser. It comes with a built-in package manager called NPM (Node Package Manager), which is used to install, manage, and share reusable code modules. In this guide, we'll show you how to run NPM outside of the Node REPL in your normal shell, enabling you to use NPM more efficiently and effectively in your development environment.

Table of Contents

  1. Prerequisites
  2. Running NPM Outside the Node REPL
  3. Using NPM Scripts
  4. FAQ
  5. Related Links

Prerequisites

Before we begin, make sure you have the following software installed on your system:

  1. Node.js: Download and install the latest version of Node.js for your platform.
  2. NPM: NPM is bundled with Node.js, so you don't need to install it separately.

To verify that Node.js and NPM are installed correctly, open a terminal or command prompt and run the following commands:

node -v
npm -v

These commands should display the current versions of Node.js and NPM, respectively.

Running NPM Outside the Node REPL

Step 1: Open a Terminal or Command Prompt

Open a terminal (on Linux or macOS) or command prompt (on Windows) to access your system's shell.

Step 2: Run NPM Commands

You can run NPM commands directly in your normal shell. Let's start by initializing a new Node.js project:

mkdir my-project
cd my-project
npm init -y

This will create a new directory called my-project, navigate into it, and initialize a new Node.js project with the default settings.

Now, let's install a package from the NPM registry. For example, let's install the popular lodash library:

npm install lodash

You can now use the installed package in your Node.js application by requiring it:

// index.js
const _ = require('lodash');

const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = _.map(numbers, n => n * 2);
console.log(doubledNumbers);

Using NPM Scripts

NPM scripts are a powerful way to define and run custom tasks in your Node.js projects. You can define them in the scripts section of your package.json file.

For example, let's create a script to run our index.js file with Node.js:

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

Now, you can run the start script using the following command in your normal shell:

npm run start

This will execute node index.js, running your application.

FAQ

1. How do I update an NPM package to the latest version?

To update a specific package to the latest version, run the following command:

npm install package-name@latest

Replace package-name with the name of the package you want to update.

2. How can I list all globally installed NPM packages?

To list all globally installed NPM packages, run the following command:

npm list -g --depth=0

3. How do I uninstall an NPM package?

To uninstall a package, run the following command:

npm uninstall package-name

Replace package-name with the name of the package you want to uninstall.

4. How do I find outdated NPM packages in my project?

To find outdated packages in your project, run the following command:

npm outdated

5. How can I cache NPM packages for offline use?

NPM caches downloaded packages by default. To view the contents of your cache, run:

npm cache ls

To add a package to your cache without installing it, run:

npm cache add package-name

Replace package-name with the name of the package you want to cache.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.