How to Run Javascript Without a Browser (With NodeJS)

Welcome to a quick guide on how to run Javascript without a browser. As many of you code ninjas already know, Javascript is the programming language that runs behind web pages. But can we run Javascript without a browser? Can it run independently, that is, not on a web page? The answer is – Yes.

One way to run Javascript without a browser is to:

  1. Install an independent Javascript runtime such as NodeJS.
  2. Then run the Javascript in the command line – node SCRIPT.JS

But just how does that work? Let us walk through the installation process and run a simple example script – Read on to find out!

 

TABLE OF CONTENTS

 

 

INSTALLING NODEJS

All right, here are the steps on how to install NodeJS and run Javascript in the command line.

 

STEP 1) DOWNLOAD & INSTALL NODEJS

First, head over to the download page of NodeJS and download the relevant installer. There are 2 versions available – The long-term support (LTS) version, and the current version. The LTS version is like the known stable version in a way, and the current version is the “sufficiently stable” latest build. Both versions work nonetheless, and it is up to your own choice.

After downloading, just launch the installer and follow along with the installation wizard. It should be very straightforward.

 

 

STEP 2) CREATE A DUMMY SCRIPT

Next, create a dummy script to run. Maybe the classic “hello world”:

dummy.js
for (var i=0; i<5; i++) {
  console.log("HELLO WORLD!");
}

 

STEP 3) RUN IT!

Finally, fire up the command line (or terminal), and run the dummy script with Node.

D:\>node dummy.js
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

That’s it – We now have Javascript running in the command line, without a browser.

 

 

EXTRA) NODE PACKAGE MANAGER (NPM)

NodeJS has a ton of packages (plugins) that you can download and use in your own project. By default, all installations of Node come with the NPM, and we can use it to pull various components.

Simply start by doing a search on the NPM website for what you need. For example, if we need a Node driver for MongoDB, just do a search and the package page will usually show how to install and the usage examples:

$ npm i mongolass --save

 

EXTRA) WHAT ELSE CAN NODEJS DO?

Definitely a lot more than “hello world”. 😆 There are thousands of Node components, and here are just a few of the common ones:

  • HTTP server – Run a web server using Node.
  • Websocket – Allow persistent connections. Create chat applications or even games.
  • MySQL – Connect to MySQL databases.
  • Threading – Multi-tasking with Javascript.
  • Chart – Create charts.

 

 

EXTRAS

That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.

 

WHAT OTHER JAVASCRIPT PLATFORMS ARE THERE?

There are plenty of other Javascript platforms. If you somehow don’t like NodeJS, or that is not what you are looking for, try out one of the following:

  • Apache Cordova – Not quite “without a browser”, but this allows you to develop mobile apps using good old HTML/CSS/JS (A browser wrapped like a native app).
  • Vert.x
  • Electron

There are actually quite a lot more, but I will just leave links below.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “How to Run Javascript Without a Browser (With NodeJS)”

Leave a Comment

Your email address will not be published. Required fields are marked *