Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

JavaScript in Node

NodeJS is a program that allows you to run JavaScript outside the web browser. In the previous lessons, we looked at setting up the development environment for JavaScript so we wont cover how to install NodeJS in this lesson. If you are new to NodeJS then you can read this tutorial to learn more

In this lesson, we will create a JavaScript file then use node in the command line to execute the code. As usual, we will be working with Visual Studio Code as the code editor.

Topics to be covered

We will cover the following topics in this lesson

Lesson Prerequisites

For you to successfully complete this lesson, you should have the following requirements

  • NodeJS - we will be executing JavaScript files directly using NodeJS from the command line. Alternatively you can execute the files in the web browser but that takes a a little more time and involves working with multiple programs.
  • Web Browser - a web browser can be used as an alternative to executing JavaScript files that we will be working with.
  • Visual Studio Code - we will be using it as the code editor. It comes with a built-in terminal that will allow us to easily execute JavaScript code from within the text editor. You can also use any code editor that you prefer.
  • JavaScript Basics - this is an added advantage and not mandatory. We will explain every piece of code covered in these lessons.

Lesson Practical Examples Project Setup

This is a practical based lesson. For you to successfully work out the examples given in this lesson, you will need to setup the following directories and files

Create a new directory called javascript-node

Open the directory javascript-node in Visual Studio Code or your favorite code editor

Create the following files inside your project folder.

  • node.js - this file will be used to for code dealing with the switch statement

The following visualizes our project setup

javascript-node
-node.js

JavaScript NodeJS Hello World

We will start with a very basic example and create a hello world application.

Open the file node.js

Add the following code to the file node.js

console.log('Hello World');

HERE,

  • console.log('Hello World'); prints the line Hello World in the terminal.

if you are using Visual Studio Code, then you can click on the Terminal menu then select New Terminal. This will open the integrated terminal in the root directory of our application

Run the following command to execute the file.

node app.js

You should be able to get the following results

Hello World

The following image shows the results in the terminal for Visual Studio

JavaScript Hello World

NodeJS Get User Input From Terminal

Let's now look at a more slightly advanced hello world application. we will create a simple program that asks the user their name then display the value in the terminal.

NodeJS comes with a good number of built-in modules that you can use to speed up development. One of the modules that comes with NodeJS is readline module. It allows the user to enter something in the terminal then the value can be captured and stored into a variable that we can use in our program.

Modify the contents of the file node.js as follows

const getinput = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
})

getinput.question(`What's your name? `, (name) => {
    console.log('Hello ' + name + '!')
    getinput.close()
})

HERE,

  • const getinput = require('readline').createInterface({...} loads the readline module and assigns it to the constant variable getinput
  • getinput.question(...) is used to ask the user to enter some input in the terminal. The value from the terminal is the assigned to the variable name which we then print in the terminal using the console.log(..) function.

Save the changes then run the following command in the terminal

node node.js

You should be able to get the following results.

Node User Input

Conclusion

As you can see from the above examples, it is possible to execute JavaScript code using NodeJS by simply passing in the JavaScript file name to the node program in the command line. The goal of this lesson was to show you how to execute JavaScript code using NodeJS. If You are interested in NodeJS then you can read our tutorials on NodeJS

Summary

NodeJS is a container program that allows you to execute JavaScript outside the web browser. You can execute JavaScript files from the command line using the node command. You need to download and install NodeJS before you can do this. NodeJS is a cross platform program that can run on Windows, MacOS, and Linux based operating systems.

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

JavaScript Separation of Concerns

JavaScript Variables

Leave A Reply