The editor, the Node REPL and running a file
Three tools carry the whole course: an editor for the file, a terminal for the command, and the runtime for the answer. There is nothing else to install. The editor saves plain text; the terminal starts node with the name of the file; the runtime reads the file, runs it and writes its output back to the terminal.
The runtime also has a second mode that no file needs: started without a file name, node is a REPL, a shell that reads one expression at a time and prints its value. This lesson sets up the editor, shows a REPL session, runs a file twice, and reads the two things every program can ask about itself: its arguments and its own path.
The editor
Any plain text editor will do, and one that understands JavaScript saves time. The three features that matter are syntax highlighting, so that a string and a name look different; bracket matching, because every block in the language is a pair of braces; and an integrated terminal, so that the file and the command are in the same window. The editor must save the file as UTF-8 and must not add a byte order mark.
Put the work in one folder, for example ~/js-course, and open that folder rather than a single file. Every command in the course then runs from that folder, and a file named on the command line is the file the editor has open. The extension is .js; the runtime does not require it, but every other tool does.
The Node REPL
Run node with no arguments and the prompt changes to >. Every line is read, evaluated and printed, which makes the REPL the fastest way to answer a question about the language: what a method returns, what a type is, what an operator does with two values of different types. A session looks like this one:
node
Welcome to Node.js v24.22.1.
Type ".help" for more information.
> 1 + 1
2
> const sum = (a, b) => a + b;
undefined
> sum(2, 3)
5
> .exit
A declaration prints undefined because a declaration has no value; an expression prints its result. The prompt accepts any statement, keeps the names you declare for the length of the session, and forgets all of them when you leave with .exit or Ctrl+D. Nothing typed at the prompt is saved to a file, which is exactly why it is a good place to experiment.
Running a file
A file is run by naming it after the runtime. The program below asks for its own source and reports three facts about it. require loads the file system module, __filename is the absolute path of the running file, and readFileSync returns the text of the file as a string.
const fs = require('node:fs');
const source = fs.readFileSync(__filename, 'utf8');
const lines = source.trimEnd().split('\n');
console.log('file:', __filename.split('/').pop());
console.log('lines:', lines.length);
console.log('first line:', lines[0]);
node self.js
file: self.js
lines: 8
first line: const fs = require('node:fs');
The count is eight because the file has eight lines, with the blank ones included. The first line is the require call itself: a program that reads its own source sees the line that does the reading, and that is the point of the example rather than a trick.
Arguments and paths
process.argv is the list of words the shell passed to the runtime. The first two entries are the path of the runtime and the path of the script, and everything after them is what the user typed. Most programs want only that tail, which is what slice(2) returns.
const args = process.argv.slice(2);
console.log('node:', process.argv[0].split('/').pop());
console.log('script:', process.argv[1].split('/').pop());
console.log('count:', args.length);
console.log('args:', args.join(', '));
node args.js alpha beta
node: node
script: args.js
count: 2
args: alpha, beta
Both paths end in a file name here because the command named a file in the current folder. Start the same file by an absolute path and the second entry becomes that path, while __filename was absolute from the start. The lesson on arguments in the later part of the course turns this list into a small option parser.
Exercises
- Open the REPL and evaluate typeof null, 0.1 + 0.2 and "5" * 2. Write down all three answers before you press Enter.
- Run self.js after adding one blank line at the end. Which number changes, and why does the first line stay the same?
- Run node args.js with no arguments. What are the three lines that follow? Which one is easy to get wrong in a script that expects one argument?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How do you start the Node.js REPL?
Answer
node with no file name
With no script argument the runtime reads expressions from the terminal, and .exit leaves it.
-
What does the REPL print for a
constdeclaration?Answer
undefined
A declaration has no value, so the REPL prints undefined; an expression prints its result.
-
Which command runs the file hello.js?
Answer
node hello.js
The runtime takes the path of the file as its first argument; there is no run subcommand.
-
What are the first two entries of
process.argv?Answer
The path of the runtime and the path of the script
The runtime and the script come first, which is why a program reads its own arguments from slice(2).
-
What does
__filenamehold inside a script run withnode self.js?Answer
The absolute path of the running file
The name is always absolute, while process.argv[1] is the path exactly as the shell passed it.
-
What does this program print?
const args = process.argv.slice(2); console.log(args.length);when it is run asnode count.js one two three?Answer
3
slice(2) drops the runtime and the script, and three words remain.
-
What does this program print?
console.log(typeof __filename);Answer
string
__filename is a string that holds the absolute path of the file.
-
What does the REPL keep when you leave it with .exit?
Answer
Nothing: the names declared in the session are gone
The REPL keeps state for one session only, and nothing typed at the prompt is saved to a file.
-
Why does the file in this lesson read its own source?
Answer
To show that a program can find and read the file it runs from
__filename and readFileSync together reach the running file, which is the ground for the file lessons later in the course.
-
Which module does the program load to read a file?
Answer
node:fs
fs is the file system module, and readFileSync is one of its functions.
The code on this page was run with Node.js 24.22.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container. The REPL session is a transcript of the same lines fed to the runtime, not a saved script.
Article author: Arthur Isaev