Mastering the Node.js REPL and Running Scripts from the Terminal
Learn to execute JavaScript in the Node.js REPL and run standalone scripts from the terminal—essential skills for every backend developer.
Previously in this course, we covered how to set up your development environment with NVM and explored the Node.js architecture. Now that your environment is ready, it's time to actually run code.
In this lesson, we move beyond theory to practice. You will learn how to use the Node.js interactive shell to test small snippets of code and how to execute persistent scripts stored in files.
What is the REPL?
The REPL stands for Read-Eval-Print Loop. It is a simple, interactive computer programming environment that takes a single user input, evaluates it, and returns the result to the console.
Think of it as a "scratchpad" for your code. If you want to quickly test how a JavaScript function behaves or check the value of a variable without creating a full project, the REPL is your best friend.
Launching and Using the REPL
To start the REPL, open your terminal (ensure you have Node.js installed via NVM) and type:
Bashnode
You will see a prompt change to >. You are now inside the Node.js runtime environment. Try typing a simple expression:
JAVASCRIPT> 10 + 5 15 > console.log("Hello, Node!") Hello, Node! undefined
Note: The undefined you see after the console.log is the return value of the function. console.log prints to the terminal but doesn't return a value, so Node reports undefined.
To exit the REPL, press Ctrl + C twice, or type .exit and hit enter.
Running Standalone JavaScript Files
While the REPL is great for testing, most of your work as a backend engineer will involve writing scripts stored in files. These files, usually ending in .js, contain the logic for your servers and utilities.
Let’s create your first script.
- Create a new folder for your project and navigate into it using your terminal (as discussed in our guide on navigating the file system).
- Create a file named
app.js. - Open
app.jsin your code editor and add the following line:
JAVASCRIPTconsole.log("My first Node.js script is running!");
- Save the file.
Executing with the node command
To run this file, head back to your terminal inside the directory where app.js exists and run:
Bashnode app.js
You should see the output: My first Node.js script is running!.
This is the fundamental way you will run your backend services. When we eventually build our REST API, you will use this same command to start your server.
Comparison: REPL vs. Script Execution
| Feature | REPL | Script Execution |
|---|---|---|
| Persistence | Ephemeral (lost on exit) | Permanent (saved in file) |
| Use Case | Quick experimentation | Production code/applications |
| Input | One line at a time | Entire file contents |
| Workflow | Iterative/Debugging | Automation/Server startup |
Practice Exercise
- Open your terminal and start the REPL.
- Inside the REPL, declare a variable
const name = "Your Name";and then print it usingconsole.log(name). - Exit the REPL.
- Create a new file called
greeter.js. - Inside the file, create an array of names, iterate over them using
forEach, andconsole.loga greeting for each name. - Run the script using
node greeter.js.
Common Pitfalls
- Forgetting to save the file: A classic mistake. If you modify
app.jsbut don't save it,node app.jswill execute the previous version of your code. - Running the wrong file: Ensure your terminal's working directory matches the folder where your
.jsfile resides. Usels(macOS/Linux) ordir(Windows) to verify the file is there. - Syntax Errors in REPL: If you make a typo in the REPL, sometimes it gets "stuck." If you find yourself in a state where the prompt has changed (e.g., from
>to...), it means you haven't closed a parenthesis or bracket.Ctrl + Cis your "reset" button.
Frequently Asked Questions
Q: Can I use modern JavaScript (ES6+) in the REPL?
A: Yes! Because the REPL runs on the same V8 engine as your script files, you can use const, let, arrow functions, and other modern features.
Q: Does the REPL save history? A: Yes, you can use the up and down arrow keys to navigate through your previous commands during the current session.
Q: How do I clear the terminal screen?
A: You can type console.clear() in the REPL, or simply use Ctrl + L in most terminal emulators to clear the view.
Recap
In this lesson, we transitioned from understanding the theory of Node.js to executing code ourselves. We learned that the REPL is an invaluable tool for rapid prototyping and exploration, while the node command is the workhorse for running your actual application files. These two methods of Node.js execution are the foundation of your workflow as a backend developer.
Up next: We will begin organizing our code properly by learning how to initialize projects with NPM and create our package.json file.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.


