Node.js makes writing Event driven server side applications a piece of cake.
Node.js is a cross platform run-time that has been built on top of Chrome’s JavaScript Runtime which is specifically targeted for building scalable server side network applications.
Do you remember Ghost Blogging Platform? It uses Node.js. In this article let us just focus on how to install node.js and write a one line code.
Install Node.js on Windows
Both the source code and the installers are available for Windows and Macintosh Operating systems in the official Node.js download section.
The Installer is available for both 32-bit and 64-bit operating systems. I picked the x64 version which is around 5.9 MB
Once you have downloaded the installer, double click to start installing Node.js on your machine.
- You should see the below setup screen, click Next
- Check the “I accept the terms in the License Agreement” check box and click Next.
- Provide the path where you would like the Node.js to be installed or just leave the default path as it is.
- In the Custom Setup Screen you can choose which components can be installed, as of now just leave this as it is and click Next.
- Click the Install button to start your Node.js installation
- Once the installation is completed you should see the below message displayed, click on the Finish button.
Your First Node.js Program
Hurray, you have completed the installation successfully, now go to the start menu and under Node.js folder click on Node.js executable which will bring the command prompt as shown below. Now let us see whether the Node.js installed on your system is working fine or not by trying out a simple line of code.
Now type the below code in the console window:
console.log(‘Hello Artic Post Readers’)
You can now press CTRL + D or CTRL + C to exit the Node.js console.
Quick Tip: Do you know that you could execute the same line of code or for that matter any other JavaScript in your browser’s console tab?
Executing External JavaScript in Node.js
You can also save the above code that we just executed from an external JavaScript file and refer the file name for execution in the Node.js command prompt which will give the same result.
Save the above line of code in a file called “NodeJavaScript.js”
Now from the start menu, under Node.js folder click the Node.js command prompt
In the command prompt first navigate to the path where your JavaScript file is located and then execute the file using the below syntax,
Node <filename.js>
refer the below screenshot
Common Issues on Your First Node.js Program
If you were trying to execute some JavaScript in your node.js and you see three dots emitted and nothing else happens then there is something wrong with the code, you can just exit the current command execution by entering ‘.’ – dot and press enter
You should see the below message returned by the console:
Invalid REPL keyword
REPL means, Read-Evaluate-Print Loop. Now correct your JavaScript code and re-try again.