- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js Setup
Setting up Node.js on your system is the first step to leveraging its power for building scalable, efficient applications. Unlike traditional web environments where JavaScript runs only in the browser, Node.js allows you to run JavaScript on servers, enabling you to build everything from CLI tools to massive real-time chat applications. Follow the guide below to install Node.js and get started with your first application.
Prerequisites
- A computer running Windows, macOS, or a Linux distribution (like Ubuntu).
- Basic knowledge of JavaScript (variables, functions, and objects).
- A code editor installed, such as Visual Studio Code, which has excellent built-in support for Node.js debugging.
Steps to Set Up Node.js
1. Download Node.js
- Visit the Node.js official website.
- You will see two versions: LTS (Long-Term Support) and Current. For most developers and production environments, we recommend the LTS version because it is more stable and receives security updates for a longer period.
- Download the installer specifically for your operating system.
2. Install Node.js
- Run the downloaded installer (.msi for Windows or .pkg for macOS).
- Follow the on-screen instructions. Usually, the default settings are perfect for most developers. Ensure the "Add to PATH" checkbox is checked, as this allows you to run Node commands from any terminal window.
- Verify the installation by opening your terminal (Command Prompt, PowerShell, or Terminal on Mac) and running:
node -v
npm -v
Output:
v20.10.0
10.2.3
3. Set Up a Node.js Project
- Every Node.js project needs its own folder to keep its files and dependencies organized. Create a new directory and move into it:
mkdir my-node-app
cd my-node-app
- Initialize a new Node.js project using NPM (Node Package Manager). NPM is installed automatically with Node and helps you manage external libraries:
npm init -y
Output:
A package.json file will be created in your folder. This file is the "manifest" of your project—it lists your project name, version, and the packages your app needs to run.
npm init. Without this, you won't be able to easily install libraries (like Express or Mongoose) or share your project with other developers.
4. Write Your First Node.js Application
- Create a file named
app.jsin your project folder using your editor or the terminal:
touch app.js
- Open
app.jsin your code editor and add the following code to create a basic web server:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://localhost:3000/');
});
http module as shown above, most real-world developers use a framework called Express to make building servers much faster and cleaner.
5. Run the Application
- Return to your terminal and start your server by running:
node app.js
- Open your browser and navigate to
http://localhost:3000. You should see the message "Hello, Node.js!" printed on the screen. - To stop the server, go back to your terminal and press
Ctrl + C.
Summary
Node.js setup involves downloading and installing the runtime, initializing a project with NPM, and creating your first application script. Once installed, you have access to a massive ecosystem of packages through the NPM registry. Whether you're building lightweight APIs, robust web servers, or real-time streaming apps, Node.js provides the high-performance foundation required for modern web development. Now that you're set up, your next step is to explore NPM packages to add more functionality to your app!