01 / 21

Running with
Node.js

Learn how to execute JavaScript outside the browser using Node.js and the node file.js command in your terminal.

JavaScript

What is Node.js?

JavaScript was originally built to run only inside web browsers. Node.js changed everything by providing a runtime environment that lets you execute JavaScript directly on your computer, without needing a browser at all.

Browser vs. Node.js

Browser V8 / SpiderMonkey DOM, window, alert() fetch(), localStorage Runs in Chrome, Firefox... vs Node.js V8 Engine fs, path, os modules http, process, npm Runs in your terminal

Why Run JS Outside the Browser?

Build servers
Create web servers and APIs that handle thousands of requests
Automate tasks
Write scripts to rename files, process data, or generate reports
Use one language
JavaScript for both front-end and back-end: full-stack development

Node.js opened the door for JavaScript to become a general-purpose programming language, not just a browser scripting tool.

The Core Command: node file.js

$ node app.js The runtime (executes JS) Your file (the code to run)

How node file.js Works

Terminal $ node app.js Node.js Runtime Reads app.js Compiles to machine code V8 Engine Executes code Output Printed to terminal Terminal Hello, world! The entire cycle happens in milliseconds

REPL vs. File Execution

node (REPL) node file.js
How to start Type node and press Enter Type node app.js
Input One line at a time, interactively Entire file is read at once
Best for Quick experiments, testing Real programs, scripts, servers
Code saved? Lost when you exit Saved in a .js file permanently
Exits when You type .exit or Ctrl+C Code finishes running

Running Your First Script

1
Create a file. Open a text editor and save a file with a .js extension, such as hello.js.
2
Write JavaScript. Type your code inside the file, for example: console.log("Hello, world!");
3
Open a terminal. Navigate to the folder where you saved your file using the cd command.
4
Run the command. Type node hello.js and press Enter.
5
See the output. The terminal prints: Hello, world!

Example 1: Hello World

// hello.js
console.log("Hello, world!");
$ node hello.js
Hello, world!

Example 2: Variables and Logic

// math.js
const radius = 5;
const area = Math.PI * radius * radius;
console.log("Area:", area.toFixed(2));
$ node math.js
Area: 78.54

Example 3: Error Messages

// broken.js
let name = "Alice";
console.log(nmae); // typo!
$ node broken.js
ReferenceError: nmae is not defined
  at Object.<anonymous> (broken.js:2:13)

File Paths Matter

my-project/ ├── app.js ├── utils/ │   └── helper.js └── data/     └── config.json When you are in my-project/: $ node app.js ✓ Wrong folder: $ node app.js ✕ Or use the full path: $ node my-project/app.js

console.log in Node.js

In Browser console.log("Hi") Output goes to: DevTools Console → In Node.js console.log("Hi") Output goes to: Terminal / stdout

The same code, different destination. In Node.js, console.log() writes to stdout (standard output): your terminal screen. This is the primary way to see results from a Node.js script.

Passing Arguments to Scripts

// greet.js
const name = process.argv[2];
console.log("Hello, " + name + "!");
$ node greet.js Alice
Hello, Alice!

Common Mistakes

✕ Wrong directory
Running node app.js from a folder that does not contain app.js. Always cd into the correct folder first.
✕ Forgetting .js
Typing node app sometimes works, but can fail if there is ambiguity. Always include the .js extension to be explicit.
✕ Node not installed
If the terminal says "node: command not found", Node.js is not installed or not in your system PATH.

Quick Reference Summary

Command What It Does Example
node file.js Run a JavaScript file node app.js
node Open interactive REPL node then type code
node -v Check installed version v20.11.0
node -e "code" Run inline code node -e "console.log(42)"
Ctrl + C Stop a running script Exit infinite loops

Question 1 of 5

What does the node command do in node app.js?

Question 2 of 5

Where does console.log() output appear when you run a script with Node.js?

Question 3 of 5

You type node app.js and get the error "Cannot find module". What is the most likely cause?

Question 4 of 5

What is the difference between typing node alone and node file.js?

Question 5 of 5

In the array process.argv, what value is stored at index [2] when you run node greet.js Alice?