TExcelTAD
Home › Node.js › Node.js Get Started

Node.js Get Started

To write Node.js programs you need two things: Node itself and a text editor. This lesson shows how to install Node, run a file, use the interactive REPL and pass arguments to a script.

Installing Node.js

Download the LTS (Long-Term Support) version from nodejs.org, or use a version manager so you can switch versions per project:

Tool Platform Example
Official installer Windows, macOS download from nodejs.org
nvm macOS, Linux nvm install --lts
fnm all fnm install 22
Package manager Linux sudo apt install nodejs (may be older)

Installing Node also installs npm, the package manager. Check both from a terminal:

bash · try it yourself
$ node --version
$ npm --version
v22.12.0
10.9.0

Note: Shell sessions in this course are simulated. Your version numbers will differ — anything 22 or newer works for this course.

Running a file

Create a file named hello.js:

js
// hello.js
const user = "learner";
console.log(`Welcome to Node.js, ${user}!`);

Then run it by passing the file name to node:

bash
$ node hello.js
Welcome to Node.js, learner!

Node reads the file, runs it top to bottom, and exits when there is no more work to do. Here is the same program as a playground:

node · try it yourself
const user = "learner";
console.log(`Welcome to Node.js, ${user}!`);
Welcome to Node.js, learner!

The REPL

Run node with no file to start the REPL (Read–Eval–Print Loop). Type an expression and Node prints its value immediately — perfect for quick experiments.

bash · try it yourself
$ node
> 6 * 7
> "node".toUpperCase()
> [1, 2, 3].map(n => n * 2)
> .exit
Welcome to Node.js v22.12.0.
Type ".help" for more information.
42
'NODE'
[ 2, 4, 6 ]

Useful REPL commands: .help, .exit (or Ctrl+D twice), .editor for multi-line input, and _ holds the last result.

Command-line arguments

Everything you type after the file name ends up in process.argv. The first two entries are the path to node and the path to your script, so your own arguments start at index 2.

node · try it yourself
// Pretend the user ran:  node greet.js Ada Grace
const args = ["Ada", "Grace"]; // in a real script: process.argv.slice(2)

for (const name of args) {
  console.log(`Hello, ${name}!`);
}
console.log("argv[0] ends with node:", /node(\.exe)?$/.test(process.argv[0]));
Hello, Ada!
Hello, Grace!
argv[0] ends with node: true

Useful node flags

Command What it does
node file.js run a script
node --watch file.js re-run automatically when the file changes
node -e "code" evaluate a short snippet
node -p "expr" evaluate and print an expression
node --env-file=.env app.js load environment variables from a file
node --test run the built-in test runner
node --inspect app.js enable the debugger
bash · try it yourself
$ node -p "Math.max(4, 9, 2)"
$ node -e "console.log('one-liner works')"
9
one-liner works

Tip: node --watch is built in since Node 18. You no longer need nodemon for simple auto-restart during development.

Test yourself

How do you run a file named app.js with Node?

  1. npm app.js
  2. node app.js
  3. run app.js
  4. node --file app.js

If you run node tool.js add milk, what is process.argv[2]?

  1. tool.js
  2. node
  3. add
  4. milk

Which flag restarts your script automatically when its file changes?

  1. --reload
  2. --inspect
  3. --watch
  4. --live