Installing Node.js and NPM

Tutorial 2 of 5

Introduction

In this tutorial, we will cover the process of installing Node.js and the Node Package Manager (NPM) on your system. Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. NPM is the default package manager for Node.js, and it is used for installing and managing package dependencies.

By the end of this tutorial, you will be able to:

  • Install Node.js and NPM on your system
  • Understand the basic concepts of Node.js and NPM
  • Execute simple Node.js programs

Prerequisites:

  • Basic understanding of how to use the terminal or command prompt
  • Familiarity with JavaScript is helpful, but not necessary

Step-by-Step Guide

  1. Installing Node.js and NPM:

Visit the official Node.js website at https://nodejs.org/en/download/ and choose the appropriate package for your operating system. The installation process includes NPM, so you don't need to install it separately.

  1. Verification:

After the installation, open your terminal or command prompt and type the following commands to confirm the successful installation of Node.js and NPM:

bash node -v npm -v

These commands should display the installed versions of Node.js and NPM respectively.

Code Examples

  1. Create a Simple Node.js Program:

Open a new file named app.js and write the following JavaScript code:

javascript console.log('Hello, World!');

To run this Node.js program, open your terminal and type:

bash node app.js

This command will execute the app.js file with Node.js and display 'Hello, World!' on the console.

Summary

In this tutorial, we have installed Node.js and NPM on your system and verified their installation. We also ran a simple Node.js program. Next, you can start learning more about Node.js and NPM, how to create server-side applications, or how to manage and install packages using NPM.

For further learning, here are some resources:

  • Node.js Documentation: https://nodejs.org/en/docs/
  • NPM Documentation: https://docs.npmjs.com/

Practice Exercises

  1. Create a Node.js program that logs your name and the current date to the console.

  2. Install a package from NPM, import it into your Node.js program, and use the package in some way.

Solutions:

  1. Here is an example of how you might complete the first exercise:

```javascript
// Use JavaScript's built-in Date object to get the current date
var currentDate = new Date();

console.log('My name is John Doe');
console.log('The current date is', currentDate);
```

  1. For the second exercise, you might use the moment package to format the date:

First, install the moment package using NPM:

bash npm install moment

Then, use it in your Node.js program:

```javascript
// Import the 'moment' package
var moment = require('moment');

// Format the current date using 'moment'
var formattedDate = moment().format('MMMM Do YYYY, h:mm:ss a');

console.log('The formatted date is', formattedDate);
```

This program will display the current date in a more human-friendly format.