Introduction to Executing Node Code

Learning Objectives

If you have Node installed on your computer, the commands node and npm are available in your environment. Make sure the Node version is a recent one (20.x or higher).

To verify your Node version, open a terminal and run the command node -v. If the prompt returns something around "command 'node' not found, ...", you don't have the node command. Follow this link on instructions to install node.

How to execute your applications

There are broadly two ways of executing node programs.

  1. an interactive shell/mode, also called REPL (Read-Eval-Print-Loop)
  2. a script by passing the first name of the file to node command

Your first node in a REPL

REPLs are for simple commands. They are not intended to run long programs. They are more suited for testing functionalities of individual code snippets.

To get in REPL mode, run node in a terminal (type node and press Enter).

REPL in action

1. Waits

bash$ node
Welcome to Node.js v<your_number>
...
>

2. Read

bash$ node
Welcome to Node.js v<your_number>
...
> Math.random()

3. Evaluate and Print

bash$ node
Welcome to Node.js v<your_number>
...
> Math.random()
> 0.<random number>
>

4. Start from 1. Again (Loops)


Node will read the line, evaluate, print the result, and loop over these three things for everything you type until you exit the session. To exit, press the key Ctrl + D or Ctrl + C twice or type ".exit" and hit enter in the REPL.

Your first node in a script

Create a directory with mkdir (make a new directory) and cd (change directory) into it:

bash$ mkdir node-example-1
$ cd node-example-1

Open up the directory in your favorite code editor. Create a file name test.js. If you use a linux environment, you can just do:

bash$ touch test.js // creates a file
$ vim test.js // opens the file with a linux built-in vim editor

Put the same Math.random() line as we did with REPL into it:

node-example-1/test.js
jsMath.random()

To execute that script, in your terminal, type the node command passing the file name as its first argument:

bash$ node test.js // <node-command> <file-name>

On running the command above, you'll notice it does nothing. To print the random number, use the global console object.

node-example-1/test.js
jsconsole.log(Math.random())

Executing node-example-1/test.js will output a random number and immediately terminate the script. We're using both JavaScript (Math object) and an object from the Node API (console). Taa daa!

A little diversion to talk `console`

The console object in node is similar to the one available in browsers but they are not the same. This one is a mask that writes the value of its arguments to the default standard output stream (stdout) of the running node process.

The console object is one of many top-level global scope objects that we can access in Node without declaring any dependencies or importing them.

The standardized globalThis is the global object that the console is a part of. All properties of globalThis can be accessed directly without prefixing with the object name: for example, instead of globalThis.console.log use console.log that works the same and fine.

Closing

You ran your first NodeJS program. One in node's REPL and the other in a file like a pro. Nothing is too small to celebrate. Congratulations! You are heading in the right direction.

References

Footnotes

  1. Nodejs.org how to use the nodejs repl Pasted on Sat, Jul 26, 2025.

This page was published on 16/06/2025, last updated on 15/11/2026. Contributed to by kelvinsekx.