Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Setting Up Your Node.js and npm Environment

Introduction

In our previous lessons, we set up your code editor and integrated your AI assistant, GitHub Copilot. You now have a powerful environment for writing code and a partner to help you do it. Today, we'll install the final core component of our modern development setup: the engine that will actually run our JavaScript code outside of a web browser.

This lesson focuses on installing and verifying a Node.js and npm environment. Node.js is a JavaScript runtime that allows us to build server-side applications and use modern development tools—essential for your goal of building full web and mobile apps. Npm, the Node Package Manager, comes with Node.js and is the world's largest software registry, allowing you to easily incorporate code libraries from other developers.

By the end of this session, your machine will be fully equipped to run, test, and manage modern JavaScript projects, setting the stage for us to begin coding in earnest.

What are Node.js and npm?

Before we install anything, let's clarify what these tools are and why they're so important.

Your experience with Google Apps Script has already shown you JavaScript running in a specific environment—the Google Cloud, which is in turn connected to a browser context. Historically, JavaScript was confined to web browsers. Node.js was created to break that limitation. It's a runtime environment that allows JavaScript code to be executed on a server, on your local machine, or anywhere else.

Your background in C and C++ gives you a solid understanding of how compiled languages require a specific runtime to execute. While JavaScript is an interpreted language, the principle is similar: Node.js provides the environment and necessary APIs for JavaScript to interact with the underlying operating system—something a browser strictly forbids for security reasons.

The image below contrasts the Browser Environment with the Node.js Environment. Notice that both use the same powerful V8 JavaScript engine (the heart of Google Chrome), but they expose different APIs. The browser provides Web APIs to manipulate web pages (like the DOM), while Node.js provides APIs for file system access (fs), networking (http), and cryptography (crypto).

This diagram illustrates the key differences between running JavaScript in a browser and running it with Node.js. Node.js provides direct access to the operating system, which is essential for backend development and build tooling.

npm stands for Node Package Manager. It's a command-line tool and an online registry of pre-written, reusable JavaScript code packages. In the C/C++ world, you might have had to manually find, download, and link external libraries. npm automates this entire process. It's like a massive warehouse of ready-made parts for your software projects.

For a great analogy that explains the value of a package manager, take a couple of minutes to watch this segment from a Coder Coder video.

What is NPM, and why do we need it? | Tutorial for beginners

This video uses a simple analogy to explain why package managers like npm are so fundamental to modern software development.

Watch from the beginning until the end of the analogy. The comparison of building a car from raw materials versus assembling pre-built parts perfectly captures the efficiency npm provides.

Installing Node.js and npm

There are several ways to install Node.js, but the most robust method for professional development is to use a version manager. A version manager allows you to install multiple versions of Node.js on your machine and switch between them easily. This is crucial because different projects may require different Node.js versions. We will use nvm (Node Version Manager), the industry standard.

The installation process differs slightly for macOS and Windows. The Mozilla Developer Network (MDN) provides an excellent, concise guide.

Setting up a Node development environment - MDN Web Docs

This guide from MDN, a highly respected resource, walks through installing Node.js using a version manager.

Please read the sections on installing Node.js for your operating system. Find the heading Installing Node. Then follow the instructions under either Windows (for nvm-windows) or Ubuntu and macOS (for nvm). You can stop reading before the "Testing" section for now.

To summarize the key steps from the article:

For macOS / Linux:
You will open your terminal and run a curl or wget command provided on the official nvm repository to install nvm. After restarting your terminal, you will install the latest Long-Term Support (LTS) version of Node.js with:
nvm install --lts

The commands for installing and verifying Node.js and npm on macOS via nvm, as shown on the official Node.js website.

For Windows:
You will download and run the installer for nvm-windows from its releases page. After it's installed, you will open Command Prompt or PowerShell as an administrator and run:
nvm install lts
nvm use <version_number> (where <version_number> is the version that was just installed).

Verifying the Installation

Once you have completed the installation steps, it's essential to verify that Node.js and npm are correctly installed and accessible from your command line.

You can do this from the integrated terminal in your code editor (VS Code or Cursor). You can typically open it via the View > Terminal menu or with the shortcut Ctrl+ (backtick).

The same MDN article we used for installation also covers the verification steps.

Setting up a Node development environment - MDN Web Docs

This section shows you the commands to confirm that Node.js and npm are ready to use.

Read the section Testing your...installation. Focus on the first two commands shown for checking the versions.

In your terminal, run the following two commands, one after the other. Each should print a version number.

  1. To check the Node.js version:

    node -v
    

    You should see output similar to v20.11.1 (the exact version number will vary).

  2. To check the npm version:

    npm -v
    

    You should see output similar to 10.2.4.

If both commands return a version number, your installation was successful!

Running Your First Node.js Script

As a final confirmation, let's write and execute a simple JavaScript file.

  1. In your code editor, create a new file named hello.js.

  2. Inside this file, type the following line of code:

    console.log("Hello from Node.js!");
    

    console.log is a function you may recognize from your Google Apps Script work. Here, it will print its output directly to your terminal instead of a browser's developer console.

  3. Now, go back to your integrated terminal. Make sure you are in the same directory where you saved hello.js.

  4. Run the script by typing the node command followed by the filename:

    node hello.js
    

The terminal should immediately print the message: Hello from Node.js!.

You have just successfully executed a JavaScript program using the Node.js runtime.

Conclusion

Excellent work. You have now completed the setup of your foundational development environment.

Here are the key takeaways from this lesson:

  • Node.js is a JavaScript runtime that executes code outside of a web browser, giving it access to system resources like the file system and networking.
  • npm is the Node Package Manager, used to install, share, and manage reusable code libraries called packages.
  • Using a version manager like nvm is the recommended best practice for installing and managing Node.js versions.
  • You can verify your installation using the node -v and npm -v commands in your terminal.

Your toolkit is now complete: you have a code editor, an AI assistant, and a runtime environment.

In the next lesson, we will focus on mastering the command line. You've already used it a bit today, and we'll now dive deeper into using the integrated terminal within your editor to perform basic operations—a skill that is absolutely essential for every modern software engineer.

Can't find a good explanation? Sign up and we'll make it for you

Sign up