0.3 · First script & the Node REPL

Getting started

0.3First script & the Node REPL

Goal: Run JavaScript in Node in two ways (REPL and .js files), use console.log and discover Node-specific global objects.
Estimated time: 8 min · self-paced reading · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • Two modes: REPL to experiment, .js file to develop: node alone for interactive, node app.js for lasting work.
  • console.log prints to the terminal: the basic tool to see, trace and debug your values.
  • Node globals, no DOM: process, __dirname, global — and never window or document in Node.

Running mini-case — Salim runs his first Hello

Salim, a junior developer in Algiers, opens the terminal: he types node alone and tests 1 + 1 in the REPL, then exits it. Then he creates app.js with a welcome console.log, runs it with node app.js, displays the received arguments and the current folder. When he looks for window out of front-end habit, Node tells him it does not exist: he understands that here you access the system, not a page. His "edit, run, read" round trip works.

Self-contained case for this section: it returns in the lesson, diagram, script and 48h challenge of section 0.3.

1. REPL and script file

Two ways to run JavaScript. The REPL (Read-Eval-Print Loop): by typing node alone, you open an interactive environment that evaluates each line at once — perfect to experiment, test an expression or explore a function (you exit with .exit). The main way: the script file. You write your code in a .js file and run it with node app.js — this is how you build real, saved and versionable applications. Salim remembers: REPL for the throwaway draft, file to build.

2. console.log and Node globals

console.log() prints to the terminal: the "print" equivalent, your basic tool to see values and debug. Node provides its own global objects: process (arguments via process.argv, environment via process.env, exit via process.exit()), __dirname and __filename (current folder and file), and global. Salim displays them one by one in his app.js in Algiers and reads each value in the terminal.

3. Node is not a browser

Key difference from the front end: in Node, no window, document or DOM — there is no web page. Instead, access to the system (files, network, processes) via modules and globals. Code that manipulates the DOM does not run in Node, and vice versa. Salim stuck this reminder above his screen: "here, no page — files, arguments, processes". Mastering these two modes and these globals means being ready to write real programs.

Common misinterpretation: "The REPL is enough to develop, and window exists everywhere in JS". No. The REPL is ephemeral: nothing is saved, long code is painful there — it is for quick testing, not building. And window, document and the DOM do not exist in Node: trying to manipulate a page there always fails. Best practice: develop in .js files (saved, re-readable, versionable) and reserve the REPL for throwaway tries.
Run Node code Throwaway draft or lasting work: pick the right mode. REPL node alone,interactive, throwawayFile .js node app.js,to developNode globals process,__dirname, global
Throwaway draft or lasting work: pick the right mode.
Text version of the diagram (SEO / screen readers / translation): 1) Salim, a junior developer in Algiers, opens the REPL with node alone: he tests expressions and exits with .exit. 2) He creates app.js with console.log, runs it with node app.js, then displays process.argv and __dirname. 3) He notes that window and document do not exist: Node accesses the system, not a page. Lesson: REPL to experiment fast, file to develop durably.

🔎 Concrete example — 🔎 Concrete example — Hello, Node!

console.log("Hello, Node !");
console.log(process.argv);
console.log(__dirname);
// run: node app.js Salim

Reading: Salim sees his message, the argument list then the current folder.

Lesson: the edit, run, read chain works end to end.

Section vocabulary

REPL
Interactive environment opened with node alone: reads, evaluates and prints JavaScript line by line; ideal for experimenting.

Simple example: Salim types 1 + 1 in the REPL and immediately reads the result.

What it is not: a saved and versionable file.

Reflex: open the REPL to test an idea before writing it in a file.

Run a file
Main way to work: write your code in a .js file and run it with node file.js, which runs the whole file.

Simple example: Salim runs node app.js and sees his Hello appear.

What it is not: typing a whole program line by line in the REPL.

Reflex: as soon as it exceeds two lines, switch to a file.

console.log()
Display function in the terminal; basic tool to see values, trace execution and debug.

Simple example: Salim displays each variable with console.log before looking further.

What it is not: a visual output in a web page.

Reflex: display first, assume after.

process
Node global object for the current process: arguments (process.argv), environment (process.env), exit (process.exit()).

Simple example: Salim reads the first name passed as an argument via process.argv in Algiers.

What it is not: window or document, which do not exist in Node.

Reflex: read argv for any tiny command-line tool.

Node vs browser globals
Node offers process, __dirname, __filename and global with system access; it has neither window, nor document, nor DOM.

Simple example: Salim replaces document with __dirname when looking for where he is.

What it is not: the same environment on both sides.

Reflex: when facing window is not defined, switch to system logic.

In practice — In practice — Run your first Node code

  1. Open the REPL with node alone; test a few expressions then exit with .exit, like Salim.
  2. Create app.js with a welcome console.log and run it with node app.js.
  3. Display process.argv and __dirname from the file; rerun with an argument (node app.js Salim).
  4. Note that window and document do not exist in Node; instead you access the system.
You run JavaScript in Node in two ways (interactive REPL and .js files), use console.log and discover Node-specific global objects.

Ready-to-say script — tell about your first run (30 s)

"I tested in the REPL with node alone, then I wrote app.js with a console.log. With node app.js Salim, I see my message, my arguments and my current folder. And I checked: no window here, we talk to the system."

Say it while showing the terminal: first the REPL, then running the file with an argument.

Copy-paste template — my first app.js

const prenom = process.argv[2] || "Salim";
console.log("Hello, Node ! Bienvenue " + prenom + ".");
console.log("Dossier courant : " + __dirname);
console.log("Arguments recus : " + JSON.stringify(process.argv.slice(2)));

No argument

Run node app.js with nothing: the template shows Salim by default, then rerun with your first name to see process.argv change.

REPL explorer

Before the file: test each line of the template one by one in the REPL, then assemble them in app.js — the draft becomes lasting.

Pair in Algiers

Salim dictates, his colleague types: one reads the terminal aloud (message, folder, arguments), the other checks each displayed value.

Debugging reflex

Silent program: add a console.log after each step (input, computation, output) and rerun — the oldest debugger is still the fastest.

✓ Best practices

  • Use the REPL to test an idea or explore a function.
  • Develop in .js files (saved, versionable).
  • Know process, __dirname and global from the start.

✗ Common pitfalls

  • Writing a whole program in the ephemeral REPL.
  • Looking for window or document in Node (they do not exist).
  • Forgetting that console.log is the basic debugging tool.

Key takeaways

  • Two ways to run: the REPL (node alone, to EXPERIMENT) and script files (node app.js, to DEVELOP).
  • console.log() = terminal output: basic tool to see values and debug.
  • Node global objects: process (argv, env, exit), __dirname and __filename, global — specific to Node.
  • Key difference: NO window, document or DOM in Node; system access instead.

48h challenge — apply tomorrow

Create app.js with Salim's template, run it with then without an argument, and test one line of the file in the REPL.

Section exit checklist
  • I run the REPL and a .js file explaining when to use each.
  • My app.js displays message, arguments and current folder.
  • I name the Node globals and the absence of window and document.
Self-rating 1-5: do I choose REPL or file based on use?Self-rating 1-5: did I display argv and __dirname unaided?

Frequently asked questions

What is the difference between the REPL and running a file, and when should you use each?

The REPL (node alone) evaluates line by line with immediate results: ideal to experiment (test a function, explore a module, check an idea). It is ephemeral: nothing is saved, unsuitable for long code. File execution (node app.js) runs a whole saved .js file, editable in VS Code and versionable: it is the real work mode (server, script, tool). In practice: develop in files and open the REPL as an occasional draft before integrating.

What really changes between browser JavaScript and Node JavaScript?

The language is identical (syntax, types, functions, classes, Promises, async/await — same V8 engine). What changes is the environment: no DOM in Node (no document, no window, no HTML elements), but system access forbidden to browsers (files, network, processes via process, system via modules). Globals differ (process, __dirname, global vs window, document) and Node has its npm ecosystem. Coming from the front end is an asset: you already know the language, you just have to learn the Node environment.

Check your understanding

When should you use the REPL rather than a .js file?

Additional resources (optional, to go further)

Level 0 examFresh, harder questions to test what you've learned