But What is Node.js?
Okay, Let me tell you.
What is Node.js?
Okay, now I understand what is node.js, but how do I use it?
So let’s continue with installing node.js and npm.
Hey, wait, what is npm?
Before installing node.js let’s see what is npm.
What is npm?
By using npm, you can easily install and manage modules.
Now what are modules?
Modules are like code libraries, written by other developers. You can use these modules to simplify development. This approach helps in organizing your codebase, making it more maintainable, and reliable.
At this instant, let’s get back to npm.
With npm, you can quickly integrate third-party functionalities into your projects. Whether you need to add advanced features to your applications or simplify your development workflow, npm is like a superstore filled with free tools for your Node.js apps. It gives you easy access to a huge bunch of free stuff made by other developers, without writing all the code from scratch.
Now let’s install node.js.
Before you can start using Node.js, you need to install it on your machine.
Following are the steps to install Node.js and npm:
1: Download Node.js
To download Node.js, visit the official Node.js website (https://nodejs.org) and download the appropriate installer for your operating system (Windows, macOS, or Linux).
I’m downloading this for Windows.
2: Install Node.js
Double-click on the downloaded .msi file and give access to run the application.
Then click the “Next” button and the installation process will start.
Accept the license agreement and click on the “Next” button.
Choose the destination path and click on the “Next” button.
Now it will install some features automatically, click on the “Next” button.
Click on “Next” and the setup is ready to install, click on the “Install” button.
Click on the “Finish” button.
3: Verify Installation
Now to verify that Node.js and npm were installed successfully open your terminal (command prompt on Windows) and enter the following commands:
The commands will return the version number of the node and npm.
Hooray! You’ve successfully installed the node and npm on your machine.
Now, let’s write your first node.js program.
First Node.js Program
In Node.js, the applications can be written using the following two methods:
- console-based method
- web-based method
In this guide, we will use the console-based method.
1: Create a New Directory
Open your terminal and navigate to the directory where you want to create your Node.js project. Create a new directory for your project using the following commands:
2: Initialize your Project
Run the following command to initialize your project. This will create a
package.json file in your directory.
You can choose the default options or edit them. All the project information will be in your
3: Write Your First Program
Create a new file named
index.js in your root directory. Open the file in a code editor and add the following code:
4: Run Your Program
In your terminal, run the following command.
You should see the output
Hello, Node.js! in your terminal.
Congratulations! You’ve just run your first Node.js program!
In this post, you learned what are Node.js and npm, how to install them as well as how to create and run a simple Node.js program.
This is just the beginning of your Node.js journey. As you delve deeper into Node.js, you’ll explore its powerful features, build web servers, interact with databases, and create APIs.
You can check out https://nodejs.dev/en/learn/ to learn more about Node.js and to continue your learning.
Thanks for reading!
For more content like this click here!