Execute JavaScript Code With Node.js
JavaScript·4 min read·Jan 27, 2026
Node.js is a runtime environment built on Google's V8 JavaScript engine that allows developers to interpret and execute JavaScript code directly from the command-line interface instead of a web browser.
Its event-driven and non-blocking I/O model allows it to handle many concurrent connections efficiently, which makes it well suited for building scalable, high-performance backend applications like web servers or streaming services.
Beyond that, Node.js provides a set of core modules that are not natively present in JavaScript and that include all sorts of functionalities necessary for creating server-side applications, such as file system operations, networking, data streams, and more, which are completed by a vast ecosystem of third-party modules available through the npm registry.
In Node.js, there are essentially two ways to run JavaScript code in the command-line interface: the REPL and modules.
Node.js modules
The most common way to execute JavaScript code in Node.js is to write the code into a regular file with a .js file extension called a module:
$ cat script.js
console.log("Hello, World!");
And pass the file as an argument of the node command as follows:
$ node script.js
Hello, World!
Node.js will then execute the JavaScript instructions present in the file and print any eventual output the script may generate into the terminal.
💡 This is the preferred method because it's the simplest way to work on real code without losing it. It allows you to save it, come back later to it, and run it again with one command.
The Node.js REPL
The Node.js Read-Eval-Print Loop (REPL) is an interactive programming environment that allows you to write and execute JavaScript code directly in the terminal, without the need to create separate files.
To start the Node.js REPL, you can use the node command without arguments:
$ node
Once executed, it will print the current Node.js version in use and a special command prompt (>):
$ node
Welcome to Node.js v20.3.0.
Type ".help" for more information.
>
Once inside the REPL, you can type JavaScript code after the prompt, and press the ENTER key to execute it, which will print the output and return value of the command:
$ node
Welcome to Node.js v20.3.0.
Type ".help" for more information.
> console.log('Hello, World!');
Hello, World!
undefined
Additionally, you can use special commands, such as .help to get a list of available commands:
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the REPL
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Press Ctrl+C to abort current expression, Ctrl+D to exit the REPL
Or .exit to exit the REPL:
> .exit
$