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:
Prerequisites:
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.
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.
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.
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:
Create a Node.js program that logs your name and the current date to the console.
Install a package from NPM, import it into your Node.js program, and use the package in some way.
Solutions:
```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);
```
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.