Getting Started with Node.js: Set up your first node project

Getting started with Node.js is relatively straightforward. In this post, I will go over the steps as simply as I can. However, before we start, there are a few prerequisites needed before you can get started.

Prerequisites:

  1. Basic knowledge of JavaScript.

  2. A computer running Windows, macOS, or Linux.

  3. A code editor of your choice, such as Visual Studio Code, Sublime Text, or Atom.

In this post, we will go over the steps necessary to get you up and running with Node.js.

Step 1: Download and install Node.js

The first step is to download the latest version of Node.js from the official website. https://nodejs.org/en/download/

Step 2: Verify the installation

To verify that Node.js has been installed correctly, open a terminal or command prompt and run the following command:

node -v

The output should look something like this:

v14.15.5

Please note that when you install Node, you also install Node Package Manager, commonly referred to as simply NPM.

Run the following command as you did to verify the node install to confirm this:

npm -v

The output should look something like this:

6.14.8

Step 3: Create a new Node.js project

To create a new Node.js project, open a terminal or command prompt and navigate to the directory where you want to create your project. Run the following command to initialize a new Node.js project:

npm init

This command will prompt you to enter some information about your project, such as the name and version. You can press enter to accept the defaults for each prompt.

This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file.

Press ^C at any time to quit.

name: (test)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

About to write to /path/to/project/package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \\"Error: no test specified\\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Is this OK? (yes)

Once done this will create a package.json file in your project directory, which contains information about your project and its dependencies.

Step 4: Create your entry file

You can now create your file and name it as you like, in this example we will name it index.js (like our main in the package.json file above), open your code editor, and add the following code to it:

console.log("Hello Node!")

Step 5: Start the script

To run your script, open a terminal or command prompt, navigate to your project directory, and run the following command:

node index.js

This will start the script and print "Hello Node!" in the console

Congratulations! You have now set up Node.js, created a new Node.js project, created a file, and learned how to use the console.log function. You can now continue to build and develop your project.

In the next post, we will explore how to use the Express.js framework in a Node.js application. Express.js is a popular framework for building web applications and APIs in Node.js. We will show how to install and use it, how to serve static pages and how to create a basic server and handle routes with it. If you have any questions or run into any issues as you work through this tutorial, please feel free to reach out for help.