DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Introduction to Node.js: Installation and Basics

Introduction to Node.js: Installation and Basics

Node.js is a powerful JavaScript runtime built on Chrome's V8 engine. It allows developers to build scalable, high-performance applications using JavaScript on both the client and server sides. In this guide, we will walk through the process of installing Node.js and cover some of its basic features.

Installation

To get started with Node.js, you first need to install it on your machine. Here's how:

Step 1: Download

Head over to the official Node.js website at nodejs.org and click on the "Downloads" tab.

Download page

Choose the appropriate installer for your operating system (Windows, macOS, or Linux). For most users, it is recommended to select the LTS version as it provides stability and long-term support.

Step 2: Run Installer

Once downloaded, run the installation file and follow the instructions provided by the installer. The installation process may vary slightly depending on your operating system.

Step 3: Verify Installation

After successfully installing Node.js, open a command prompt or terminal window and enter node -v. This command will display the installed version of Node.js. Similarly, you can check if npm (Node Package Manager) is also installed by entering npm -v.

If both commands return valid versions without any errors, congratulations! You have successfully installed Node.js.

Getting Started with Basic Features

Now that we have Node.js up and running let's explore some of its basic features:

Running JavaScript Files

Using Node.js in a command prompt or terminal window allows you to execute JavaScript files directly. To do this:

  1. Create a new file called "example.js".
  2. Open "example.js" in a text editor.
  3. Add some basic JavaScript code like console.log("Hello from Node!");.
  4. Save your changes and close the file.
  5. In a command prompt or terminal window, navigate to the directory where "example.js" is located.
  6. Enter the command node example.js.
  7. You should see the message "Hello from Node!" printed in the console.

Congratulations! You have successfully executed your first JavaScript file using Node.js.

Creating a Simple Server

One of the main benefits of using Node.js is its ability to easily create web servers. Let's create a simple server that responds with "Hello, World!" when accessed:

  1. Create a new file called "server.js".
  2. Open "server.js" in a text editor.
  3. Add the following code:

    const http = require('http');
    
    const server = http.createServer((req, res) => {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('Hello, World!');
    });
    
    const port = 3000;
    
    server.listen(port, () => {
        console.log(`Server running on port ${port}`);
    });
    
  4. Save your changes and close the file.

  5. In a command prompt or terminal window, navigate to the directory where "server.js" is located.

  6. Enter node test-server.js into your command prompt or terminal window.

  7. Visit http://localhost:3000 in your browser.

  8. You should see "Hello, World!" displayed.

That's it! You've created and run a basic web server using Node.js.

Conclusion

In this guide, we covered how to install Node.js on your computer and explored some of its basic features such as running JavaScript files and creating simple web servers.

Now that you have a solid foundation with Node.js installation and basics, you're ready to dive deeper into this powerful runtime environment and build amazing applications!

Happy coding!

Top comments (0)