Overview

2 Running a Node.js application

This chapter introduces the fastest path to seeing JavaScript run outside the browser: writing a small script, executing it with Node.js from the terminal, and experimenting interactively in the REPL. You’re guided from a simple “print to the console” program to loading code into the REPL for quick testing, all to establish a smooth feedback loop for learning and debugging. Along the way, the chapter highlights how Node’s console output parallels browser debugging and encourages a disciplined setup with strict mode to catch common mistakes early.

You begin by creating a JavaScript file that logs a message, then run it from the command line with the node command (with or without the .js extension) from the file’s directory. If the script runs correctly, its message appears in the terminal, illustrating how console.log writes to standard output in Node. The chapter also recommends enabling strict mode (by placing 'use strict'; at the top of files) to prevent pitfalls like accidental globals or invalid assignments, reinforcing good habits as you start building applications.

Next, you move into the Node REPL for rapid, line-by-line experimentation. You can load existing code into a session with .load filename, use the imported variables immediately (for example, iterating over an array of messages and logging each one), and then save your interactive work back to a file with .save filename (note that it overwrites existing files). Practical tips include starting the REPL in the same directory as your files or using absolute paths, and exiting with .exit, Ctrl-C twice, or Ctrl-D twice. The chapter closes by encouraging practice with console output, including string interpolation, to build familiarity with Node’s debugging-friendly workflow.

Running a JavaScript file with Node.js
Running a JavaScript file with Node.js

FAQ

How do I create my first JavaScript file for Node.js?Open your editor, add console.log('Hello, Universe!');, and save the file as hello.js.
How do I run a JavaScript file with Node.js?Open terminal, navigate to the file’s directory (for example, cd ~/Desktop), then run node hello.js or node hello.
Do I need to include the .js extension when running a file?No. Node.js accepts both node hello.js and node hello.
Nothing prints when I run my file—what should I check?Confirm the file has content, that your changes are saved, and that you’re running node from the file’s directory.
What does console.log do in Node.js?It writes output to your terminal (standard output), similar to the browser’s developer console.
What is strict mode and how do I enable it?Strict mode catches common mistakes. Add 'use strict'; at the top of each JavaScript file in your project.
How do I start REPL and load a JavaScript file into it?Start REPL with node, then load a file with .load filename.js. The file’s variables become available in the REPL.
How do I loop through an array and print items in REPL?Use messages.forEach(msg => console.log(msg));. REPL will also echo undefined because console.log returns undefined.
How do I save code from a REPL session?Run .save filename.js. Note: it will overwrite an existing file with the same name.
How do I work with files outside the current directory in REPL?Use the file’s absolute path with .load (for example, .load /full/path/to/messages.js) or start REPL from the file’s folder.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Get Programming with Node.js ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Get Programming with Node.js ebook for free