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:
$ node --version
$ npm --versionv22.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:
// hello.js
const user = "learner";
console.log(`Welcome to Node.js, ${user}!`);Then run it by passing the file name to node:
$ 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:
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.
$ node
> 6 * 7
> "node".toUpperCase()
> [1, 2, 3].map(n => n * 2)
> .exitWelcome 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.
// 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 |
$ node -p "Math.max(4, 9, 2)"
$ node -e "console.log('one-liner works')"9
one-liner works
Tip:
node --watchis built in since Node 18. You no longer neednodemonfor simple auto-restart during development.
Test yourself
How do you run a file named app.js with Node?
npm app.jsnode app.jsrun app.jsnode --file app.js
If you run node tool.js add milk, what is process.argv[2]?
tool.jsnodeaddmilk
Which flag restarts your script automatically when its file changes?
--reload--inspect--watch--live