Node.js: Run JavaScript Code Without a Browser

October 1, 2018

javascriptnode

In this article, I shall talk about how JavaScript code can be executed without a browser. After all, nowadays, there are many stand-alone applications that do not need browsers at all and are written in JavaScript programming language. I would not go more into the details of those applications, but just for a second, thing about how cool it would be if you know JavaScript and can make cool applications, other than Web Applications.

First, let me show you the tedious method of running the JavaScript application that does not necessary require any kind of Web page. Still, few of the newbies do it in the following fashion.

Tedious Method: Running JavaScript Code in Browsers

There are the following steps you must have to do in order to run your code in any browser.

Write HTML Code

Write the HTML code like:

<!DOCTYPE html>
    <head>
    </head>
    <body>
    </body> 
</html>

Add Script To HTML file

Add script to either the head of the HTML file, or the body or make another file and reference it from your HTML document.

Disadvantages and Distractions

The disadvantages of the above strategy to write a stand-alone application or for learning are unmistakable. Most of the programmers get distracted and first declare fields to show their results. Although they might have nothing to do with their applications. Few try to beautify their page first using CSS, and the rest of people gets frustrated about inspecting the page by right clicking on it, and checking the console over and over again. Such a tedious task can have a negative effect on your progress. That's for any other article.

Let me show you the way you can run your JavaScript code without any browser. Without further ado, let dive into its details.

Setting up JavaScript using Node.js

Let us break it down in multiple steps.

Step 1: Download Node.JS

You would only need one thing, which is Node.js. First you need to download node.js by going to the following link:

https://nodejs.org/en/

There will be two versions available. Download the latest Current version of Node.js. That's it for the downloading part.

Step 2: Install Node.JS

Install the downloaded copy of node.js. The steps of installations are pretty straightforward and self-explanatory. Just press NEXT, NEXT etc. you will eventually finish the installation part of it.

Step 3: Make Folder and Create .js Document

Always make sure to create a folder. It will not only help you to organize your code, but it will also help you to do the steps that are required to run JavaScript code using Node.js.

Step 4: Run npm command

Open Command Prompt! Click the search windows and type cmd and hit enter.

The CMD would look like this:

And navigate to the folder using the following command line:

cd yourDirectory cd yourFolder

cd = Change Directory.

You can do the above two steps in one step, but to make things easy for you, I have broken down into two steps. I have made "NodeJS" folder on Desktop, and created "javanode.js" file in it. The above commands would look like this: After doing the above mentioned steps, run the following command on Command Prompt(CMD):

npm init

And the just press Enter multiple times upon names, versions etc. If you want to give them name, go for it. But for now I skipped that. At the end, you will be asked to type yes. Just type yes. That's it for the setting up. The commands would look like this:

Let's test it!

Write the following code in your .js file:

var a = 10;
var b = 20;

console.log(a+b);

And save it!

Go back to the command prompt, and type the following command:

node yourJavaScriptFile.js

Make sure you change yourJavaScriptFile name to your own .js file name. The output you will get:

30

Summary

In this article you have learned how to run your JavaScript code without using the browser. You have also been exposed to the application Node.js, which is a JavaScript runtime Engine. That's it for this article! Enjoy JavaScripting!


This post is curated by IssueHunt that an issue based bounty for open-source projects.

https://issuehunt.io/

issuehunt-image