Back to Blog
Lesson 3 of the Node.js: Build Your First Server & CLI course
Node.jsJuly 14, 20264 min read

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.

Node.jsterminalREPLJavaScriptbackenddevelopment

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:

Bash
node

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.

  1. Create a new folder for your project and navigate into it using your terminal (as discussed in our guide on navigating the file system).
  2. Create a file named app.js.
  3. Open app.js in your code editor and add the following line:
JAVASCRIPT
console.log("My first Node.js script is running!");
  1. 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:

Bash
node 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

FeatureREPLScript Execution
PersistenceEphemeral (lost on exit)Permanent (saved in file)
Use CaseQuick experimentationProduction code/applications
InputOne line at a timeEntire file contents
WorkflowIterative/DebuggingAutomation/Server startup

Practice Exercise

  1. Open your terminal and start the REPL.
  2. Inside the REPL, declare a variable const name = "Your Name"; and then print it using console.log(name).
  3. Exit the REPL.
  4. Create a new file called greeter.js.
  5. Inside the file, create an array of names, iterate over them using forEach, and console.log a greeting for each name.
  6. Run the script using node greeter.js.

Common Pitfalls

  • Forgetting to save the file: A classic mistake. If you modify app.js but don't save it, node app.js will execute the previous version of your code.
  • Running the wrong file: Ensure your terminal's working directory matches the folder where your .js file resides. Use ls (macOS/Linux) or dir (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 + C is 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.

Similar Posts