0.3First script & the Node REPL
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.
🔎 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
- Open the REPL with node alone; test a few expressions then exit with .exit, like Salim.
- Create app.js with a welcome console.log and run it with node app.js.
- Display process.argv and __dirname from the file; rerun with an argument (node app.js Salim).
- Note that window and document do not exist in Node; instead you access the system.
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.
- 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.
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.
When should you use the REPL rather than a .js file?