Skip to content

Node.js Environment Setup

Online Playgrounds Before setting up a local environment, you can experiment with Node.js using online coding platforms. Websites like Replit, CodeSandbox, or Glitch provide ready-to-use Node.js environments directly in your browser. This allows you to run code examples and try things out without any local installation. Try running this simple example on one of those platforms:

/* Simple Hello World in Node.js */
console.log("Hello World!");

For serious development, you’ll want to set up Node.js on your own computer. This requires two main components:

  • A Code Editor: To write and edit your JavaScript code.
  • The Node.js Runtime: To execute your JavaScript code outside of a browser.

You can use any text editor to write Node.js code. However, modern code editors offer features like syntax highlighting, code completion (IntelliSense), debugging tools, and integrated terminals, which significantly improve productivity. Popular choices include:

  • Visual Studio Code (VS Code): Free, powerful, and highly extensible. The most popular choice currently.
  • WebStorm: A powerful commercial IDE from JetBrains.
  • Sublime Text: A lightweight and customizable editor.
  • Atom: Another free and extensible editor (development has slowed).
  • Vim/Neovim or Emacs: Powerful terminal-based editors preferred by some developers.

The files you create containing your Node.js program code are typically saved with the .js extension (for traditional CommonJS modules) or sometimes .mjs (for ES modules).

Ensure you have a code editor installed and are comfortable writing, saving, and managing files before proceeding.

Node.js is a JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It allows you to execute JavaScript code on the server-side or in a standalone environment. It’s not just an interpreter; it includes modules for file system access, networking, cryptography, and more, along with a package manager (npm) for installing third-party libraries.

Node.js provides official installers and binaries for various operating systems (Windows, macOS, Linux) and architectures (x64, ARM64).

There are two primary ways to install Node.js:

  1. Official Installers: Download the appropriate installer (LTS recommended for most users) from the official Node.js website: https://nodejs.org/
  2. Node Version Manager (NVM): Tools like nvm (for macOS/Linux) or nvm-windows allow you to install and manage multiple Node.js versions on the same machine. This is highly recommended for managing different project requirements. Find installation instructions here: nvm (macOS/Linux), nvm-windows

Installation using Official Installer (Example)

Section titled “Installation using Official Installer (Example)”
  • Windows: Download the .msi installer and follow the installation wizard. It will typically install Node.js to C:\Program Files\nodejs and add it to your system’s PATH environment variable.
  • macOS: Download the .pkg installer and follow the wizard. It usually installs Node.js to /usr/local/bin/node and npm to /usr/local/bin/npm.
  • Linux: You can use the official binaries (.tar.xz) and extract them, or more commonly, use a package manager specific to your distribution (like apt for Debian/Ubuntu, yum for CentOS/Fedora) often in conjunction with NodeSource repositories for up-to-date versions. Refer to Node.js documentation for specific instructions: Node.js Binary Distributions

Important: After installation (especially on Windows), you might need to close and reopen any open terminal or command prompt windows for the PATH changes to take effect.

Using NVM simplifies managing different Node.js versions. Once NVM is installed (follow instructions from the links above), you can install and switch versions easily:

# Install the latest LTS version
nvm install --lts
# Use the installed LTS version
nvm use --lts
# Install a specific version (e.g., latest current)
nvm install node # Installs the latest version
nvm use node
# List installed versions
nvm ls

Open your terminal or command prompt and type the following commands to check if Node.js and npm (Node Package Manager, installed alongside Node.js) are installed correctly:

# Check Node.js version
node -v
# Check npm version
npm -v

If these commands output version numbers (e.g., v18.17.1, 9.6.7), your installation is successful.

Create a file named hello.js with the following code:

hello.js
const message = "Hello, World!";
console.log(message);

Navigate to the directory containing hello.js in your terminal and execute it using Node.js:

$ node hello.js

If your setup is correct, this will print:

Hello, World!

You now have a working Node.js development environment set up locally.