Get Started with Node.js Development

Welcome to this hands-on guide where you’ll learn how to get started with Node.js development. Whether you’re on macOS or Windows, this guide will walk you through the installation process, running your first Node.js code, importing an NPM package, and using it in your JavaScript code. We’ll be using Visual Studio Code as our code editor, which provides a seamless development experience for Node.js projects.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  • A computer running either macOS or Windows.
  • Internet access to download necessary software.
  • Basic familiarity with your operating system’s command line interface.

Step 1: Install Node.js

Node.js is a JavaScript runtime that allows you to run JavaScript code on the server-side. You can download an install Node.js from the official Node.js website at https://nodeje.org

Step 2: Install Visual Studio Code

While you can use any code editor, one of the most popular code editors is Visual Studio Code (VSCode). You can download and install VSCode from https://code.visualstudio.com

Step 3: Create a Node.js Project

Now that you have Node.js and Visual Studio Code installed, let’s create a simple Node.js project:

  1. Open Visual Studio Code.
  2. Click on “File” > “Open Folder” and create a new folder for your project.
  3. Inside your project folder, create a new file named app.js.
  4. In app.js, add code below.
  5. Save the file.

     // app.js
     console.log("Hello, Node.js!");
    

This code will simply write a “Hello Node.js!” message to the console when run.

Step 4: Run Your Node.js Code

Let’s run the Node.js code you just created:

  1. Open a terminal within Visual Studio Code by pressing Ctrl + or clicking on “Terminal” > “New Terminal”.
  2. Navigate to your project folder using the terminal.
  3. To execute your app.js code, run the following command in the terminal:

     node app.js
    
  4. You should see the output Hello, Node.js! printed in the terminal, indicating that your Node.js code executed successfully.

Step 5: Importing a NPM Package

Now, let’s import the ‘chalk’ NPM package and use it in your Node.js code:

  1. Open a terminal within Visual Studio Code if it’s not already open.
  2. Navigate to your project folder using the terminal.
  3. Run the following command to initialize a new package.json file:

     npm init -y
    

This command initializes a new package.json file with default values.

  1. Now, let’s install the chalk NPM package, which is a library for styling console.log messages with colors.

    Run the following command:

     npm install chalk
    
  2. Once chalk is installed, open app.js in Visual Studio Code.
  3. Update the code in app.js to import chalk and use it:

     // app.js
     const chalk = require('chalk');
        
     // Example usage of chalk to style console output
     console.log(chalk.blue('Hello, Node.js!'));
     console.log(chalk.green('This text is green!'));
     console.log(chalk.red.bold('Error: Something went wrong!'));
    
  4. Save the file.

Step 6: Run Your Updated Node.js Code

Let’s run the updated Node.js code with the imported chalk package:

  1. Make sure you’re still in the project folder in the terminal.
  2. Run the following command to execute your updated app.js file:

     node app.js
    
  3. You should see the output displaying styled text using chalk, indicating that chalk was successfully imported and used in your Node.js code.

Congratulations! Now wasn’t that easy? You’ve successfully created, executed, imported an NPM package (chalk), and used it in your first Node.js project using Visual Studio Code. You’re now equipped to explore more advanced Node.js development techniques and build powerful applications.