In this article, we will start from the basic introduction of NodeJs, its installation to running our first NodeJs application.
Node.js is an open-source, cross-platform, runtime environment that allows developers to create all kinds of server-side tools.
Head over to Node’s official website and download the latest stable version of node.js for your Operating System.
For Windows Users-
- Run the installer.
- Follow the installer steps, agree to the license agreement and click the next button.
- Restart your system/machine.
- Now, test NodeJS by printing its version using the following command in Command Prompt:
For Linux Users-
- In order to get this version, we just have to use the apt package manager.
- We should refresh our local package index first, and then install from the repositories:
$sudo apt get update
$sudo apt-get install nodejs
- Now we need to install Node Package Manager(npm):
$ sudo apt-get install npm
- This will allow you to easily install modules and packages to use with Node.js.
- To check which version of Node.js you have installed after these steps, type:
$ nodejs -v
Hello World with NodeJs
To get started with Node.js, let’s try it in the terminal!
Start Node.js by simply typing node:
Okay, let’s try printing something:
$ node> console.log(‘hello from Node.js’)
Once you hit Enter, you will get something like this:
>console.log(‘hello from Node.js’)
hello from Node.
Feel free to play with Node.js by using this interface .
Now we are all set to create our Hello World Application through IDE
- Let’s start by creating a file called “hello.js”.
- Open up your IDE (Atom, Sublime or VS Code ) and create a new file and save it with the name “hello.js”.
- If you’re done with that, copy the following snippet into this file i.e. “hello.js”-
> console.log(‘hello from Node.js’);
- To run this file, you should open up your terminal and navigate to the directory in which you have saved “hello.js” or you can use integrated terminal of your text editor if present(We are using VS Code here).
- Once you successfully navigated yourself to the right spot, run your file using the node hello.js command.
- You can see that it will produce the same output as before – printing the string directly into the terminal.
$ node hello.js
hello from Node.js
If you see the same, then Congrats! This completes your first application with NodeJs.
That’s all I have and thanks a lot for reading. Please let me know if any corrections/suggestions. Please do share and comments if you like the post. Thanks in advance… 😉
Thanks Yash Kesharvani for helping us to grow day by day. He is expert in the backend services and loves to play around node.js.