Setting Up Node.js Environment
Introduction
Setting up the Node.js environment is the first step towards building applications using this powerful runtime. This lesson will guide you through the installation process, configuration, and verification of your Node.js setup.
System Requirements
Before installing Node.js, ensure your system meets the following requirements:
- Operating System: Windows, macOS, or Linux.
- Hardware: A machine with at least 1GB of RAM (more is recommended for larger applications).
Step 1: Downloading Node.js
- Visit the Official Website: Go to the Node.js official website.
- Choose the Right Version: You will see two versions available for download:
- LTS (Long Term Support): Recommended for most users as it receives regular updates and stability improvements.
- Current: Contains the latest features but may not be as stable.
- Download the Installer: Click on the installer for your operating system (Windows, macOS, or Linux).
Step 2: Installing Node.js
- Windows Installation:
- Run the downloaded
.msi
installer. - Follow the setup wizard instructions, accepting the license agreement and default settings.
- Ensure the option to install
npm
(Node Package Manager) is checked.
- Run the downloaded
- macOS Installation:
- Open the downloaded
.pkg
file. - Follow the on-screen instructions to complete the installation.
- Open the downloaded
- Linux Installation:
- For most Linux distributions, you can install Node.js using a package manager. Here’s how to do it for Ubuntu:
- Alternatively, you can use Node Version Manager (nvm) to install Node.js, which allows for easier management of multiple Node.js versions.
sudo apt update sudo apt install nodejs npm
Step 3: Verifying the Installation
To ensure Node.js and npm were installed correctly, follow these steps:
- Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- Check the Node.js version by typing:
node -v
- Check the npm version by typing:
npm -v
Step 4: Setting Up a Simple Node.js Project
-
Create a Project Directory:
- Navigate to your preferred location in the terminal and create a new directory:
mkdir my-node-app cd my-node-app
-
Initialize the Project:
- Run the following command to create a
package.json
file, which will keep track of your project’s dependencies:
npm init -y
The
-y
flag automatically accepts the default configuration. - Run the following command to create a
-
Create Your First JavaScript File:
- Create a new file named
app.js
using a text editor of your choice (e.g., Visual Studio Code, Sublime Text):
touch app.js
- Create a new file named
-
Write Your First Node.js Code:
- Open
app.js
and add the following code to create a simple web server:
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, Node.js!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
- Open
-
Run Your Node.js Application:
- In the terminal, run the following command to start your server:
- You should see a message indicating that the server is running.
node app.js
-
Access Your Application:
- Open your web browser and navigate to
http://127.0.0.1:3000/
. You should see the message "Hello, Node.js!"
- Open your web browser and navigate to
Conclusion
In this lesson, you learned how to set up the Node.js environment on your machine, verify the installation, and create a simple Node.js application. This foundational knowledge will serve as the basis for building more complex applications in the following lessons.
Key Takeaways
- Node.js can be easily installed on various operating systems.
- The npm package manager is included in the Node.js installation.
- You can create and run a basic Node.js application to verify your setup.