
How to Install Node.js Step by Step for Beginners
In the following guide, we will see how to start with Node.js and how to install all the tools, which are required for node application.
Installing Node.js
To work with Node.js, you first need to install it from the official link mentioned below: https://nodejs.org/en/download
Always go with the LTS (Long-Term Support), which is well tested and stable without any glitches, or choose based on project requirements.
How to verify Node.js installation and its version
To verify the Node.js installation and its version, you have two options: either open the Node.js command prompt or open the Windows command prompt or terminal for Mac users, and run the below command:node -vOnce you run the above command, you may get a similar output:
C:\Users\xyd>node -v
v16.20.2Here, the version may be different based on the version you selected while downloading from the official website.
How to create the first Node.js application:
To create your first Node.js application, follow these steps:- Create one folder (e.g., NodeApplication) at your preferred location such as Desktop, Documents, Drive, etc.
- Open your favorite IDE (e.g., VS Code) and go to File → Open Folder → select your created folder and open it.
- Open the terminal either in VS Code IDE or Node.js command prompt.
- Write the below command to create the first Node application:
npm init
5. Once you run the above command, it will ask a few questions; go with the default steps
6. If you want to skip the questions, run:
npm init -y
Here, -y represents “yes” for all questions by default, so you don’t need to manually accept them Create one file, mostly named index.js, which will be the entry point of your application.
First Node.js Example:
console.log('Hello World!');To run the above program in the terminal, write:
node index.jsOnce executed, you will see ‘Hello World!’ in the terminal. This was a very basic example of a Node.js application. Now, let’s create a Node.js application with a server that runs on localhost:3000.
How to create the first Server in Node.js Application
Follow these steps to create your first server:- Create a file within the application folder and run the command.
- npm init -y
- Once you run the command, the following structure will be created:
PS C:\Users\xyd\Documents\Angular Practice
Basics\NodeApplication> npm init -y
Wrote to C:\Users\xyd\Documents\Angular Practice
Basics\NodeApplication\package.json:
{
"name": "nodeapplication",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" &&
exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
} A package.json file is generated automatically. It is the configuration file for the Node application, which contains metadata about the project such as name of node application, and the file will look like this:
{ "name": "nodeapplication", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
As you can see the main is the index.js file which is the entry point of your application you can change if you want consider different file:
{ "name": "nodeapplication", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
Create one file name index.js (if not changed) or else server.js (if changed). Write the below code:
const http = require("http"); const server = http.createServer((req, res) => { res.write("Hi from server...!"); res.end(); }); server.listen(3000, () => { console.log("Server is running on port 3000"); });
Here,
- const: is a JavaScript keyword used to declare a constant variable.
- require: is a function which is used to import different modules and dependencies.
- http: is a built-in or core module used to create server and make communication between client and server.
- createServer(): is a function which is used to create a server.
- req: is a request object sent by client
- res: is a response object which will be sent by server
- write(): is a function used to send the response to the client
- end(): is a function used to end the response
- listen(): is a function used to run application on provided port
Note: This example uses Node.js built-in http module (not Express.js).
Now it’s time to run the application and see the output. Run the following command in terminal:
node index.js you will get the following response in the terminal:
Server is running on port 3000Now finally open your favorite browser (e.g., Chrome) and try to access the node application using localhost:3000
Congratulations you created your first Node.js server application.
I would like to share my real-time experience as a full-stack developer. The most challenging part when a frontend developer moves to a full-stack journey is getting stuck on how to start, where to start, how to do all the setup, how to create first application and first server. Trust me this is my real-time experience which I am sharing through ApnaInsights.