What is Node.js?¶
In simple terms, Node.js is a JavaScript runtime environment built on Chrome’s V8 engine. This means you can write backend code (traditionally, JavaScript was primarily used for web frontends) in JavaScript and run it on the server. It extends JavaScript beyond browsers to server-side, desktop applications, and even mobile apps, making it ideal for beginners to learn full-stack development.
Step 1: Install Node.js¶
Installation steps vary slightly by operating system. Below are detailed instructions for Windows, Mac, and Linux.
Windows Installation¶
-
Download the Node.js Installer
Visit the official Node.js website (https://nodejs.org/), and click the LTS (Long-Term Support) download button (LTS versions are recommended for stability). For example, the current LTS version is 20.x. Choose the installer matching your system architecture (32-bit/64-bit; Windows typically defaults to 64-bit). -
Run the Installer
Double-click the downloaded installer and follow the prompts:
- Click Next to accept the license agreement.
- Choose the installation path (default:C:\Program Files\nodejs). IMPORTANT: Check “Add to PATH” (this automatically configures environment variables, so thenodecommand is recognized in the command line).
- Continue clicking Next and wait for installation to complete.
Mac Installation¶
Using Homebrew (Mac’s package manager) is the simplest method:
- Install Homebrew (if not already installed)
Open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(Note: This requires Xcode Command Line Tools; it will prompt you to install if missing.)
- Install Node.js via Homebrew
In Terminal:
brew install node
Homebrew automatically installs Node.js and its package manager, npm.
Linux Installation¶
Using Ubuntu/Debian as an example (other distributions like CentOS follow similar steps with minor command differences):
- Install Node.js and npm
In Ubuntu/Debian Terminal:
sudo apt update # Update package sources
sudo apt install nodejs npm # Install Node.js and npm
(Note: Ubuntu repositories may have older Node.js versions. For the latest version, see “Version Management Tools” below. For learning purposes, the default version is sufficient.)
- Verify Installation
After installation, open a command line (Windows cmd/PowerShell, Mac/Linux Terminal) and run:
node -v # Should display a version like v20.x.x
npm -v # Should display a version like 10.x.x
If versions are displayed, Node.js is installed successfully!
Step 2: Configure Your Development Environment¶
After installation, use an editor like VS Code (lightweight, free, and powerful) to write and run code.
1. Install VS Code¶
- Download the installer from https://code.visualstudio.com/ and follow setup prompts (Windows/Mac/Linux).
- Open VS Code, click the “Extensions” icon in the left activity bar, search for “Node.js,” and install the official Node.js extension (by Microsoft, for syntax highlighting and code hints).
2. Create Your First Node.js Project¶
-
Create a Project Folder
Make a new folder (e.g.,my-node-app) on your computer to serve as the project directory. -
Open the Project in VS Code
In VS Code, go to File → Open Folder and select themy-node-appfolder. -
Create a Code File
In the left Explorer panel, right-click themy-node-appfolder → New File, name itindex.js(Node.js code typically uses.jsfiles). -
Write Test Code
Inindex.js, add:
console.log('Hello, Node.js!'); // Outputs to the console
3. Run Node.js Code¶
-
Open the Terminal
In VS Code, click Terminal → New Terminal (the terminal will open in the project directory). -
Execute the Code
In the terminal, run:
node index.js
The terminal will display Hello, Node.js!, confirming successful execution.
4. npm Package Manager Basics¶
npm is Node.js’s “app store,” allowing you to install third-party libraries (e.g., for HTTP requests, database operations).
- Initialize the Project
In the project root (my-node-app) terminal:
npm init -y # -y skips prompts and creates package.json immediately
This generates a package.json file to track project metadata and dependencies.
- Install Dependencies
For example, installlodash(a utility library for arrays/objects):
npm install lodash
lodash will appear in package.json under dependencies.
- Use the Dependency
Updateindex.jsto uselodash:
const _ = require('lodash'); // Import lodash
const arr = [1, 2, 3];
console.log(_.sum(arr)); // Output: 6
Run with node index.js.
Summary¶
Congratulations! You’ve installed Node.js and set up your development environment. You can now:
- Write JavaScript code in VS Code and run it with node filename.js.
- Use npm to install third-party libraries for enhanced project functionality.
- Explore web servers, API development, and more in future steps.
Practice Tip: Experiment with small examples (e.g., file reading, user input handling) to familiarize yourself with Node.js syntax and ecosystem. For issues, refer to the official docs or community forums (e.g., Stack Overflow). Happy coding with Node.js!