Your First Node.js Script and the REPL
The fastest way to understand Node.js is to run some code. This lesson shows you how to execute scripts and use the interactive REPL before writing any project code.
Installing Node.js
Section titled “Installing Node.js”If you haven’t already, install Node.js from nodejs.org. Choose the LTS version. Verify the installation:
node --version # v20.x or highernpm --version # 10.x or higherThe REPL
Section titled “The REPL”Node.js includes an interactive shell called the REPL (Read-Eval-Print Loop). Run it with just node:
nodeYou get a > prompt. Type any JavaScript:
> 2 + 24> const name = 'Bulletin'undefined> `Hello from ${name}`'Hello from Bulletin'> process.version'v20.x.x'> .exitThe REPL is for quick experiments. Notice process — that’s a global Node.js object that doesn’t exist in browsers.
Your first script
Section titled “Your first script”Create a file hello.js:
const message = 'Hello from Node.js'console.log(message)console.log('Node version:', process.version)console.log('Current directory:', process.cwd())Run it:
node hello.jsOutput:
Hello from Node.jsNode version: v20.x.xCurrent directory: /your/current/directoryThat’s it. No browser, no HTML, no index.html — just JavaScript executing directly.
Differences from browser JavaScript
Section titled “Differences from browser JavaScript”Some things you expect from browser JS don’t exist in Node.js:
| Browser | Node.js |
|---|---|
window | global (or globalThis) |
document | Not available |
localStorage | Not available |
fetch | Available (Node 18+) |
process | Not available in browser |
require() | Available (CommonJS) |
__dirname | Available (CommonJS) |
And some things Node.js has that browsers don’t:
process— access to the running process, environment variables, argumentsfsmodule — read and write the file systempathmodule — work with file paths across operating systemshttpmodule — create raw HTTP servers
Passing arguments
Section titled “Passing arguments”Node.js exposes command-line arguments via process.argv:
console.log(process.argv)node args.js hello world# ['node', '/path/to/args.js', 'hello', 'world']process.argv[0] is node, process.argv[1] is the script path, and arguments start at index 2.
Exercise
Section titled “Exercise”- Open the REPL and explore: try
Object.keys(process)to see what’s on theprocessobject. - Create
greet.jsthat takes a name fromprocess.argv[2]and printsHello, [name]!. If no argument is passed, printHello, stranger!. - Run it:
node greet.js Alice→Hello, Alice!andnode greet.js→Hello, stranger!
- Install Node.js LTS; verify with
node --version. - The REPL (
nodewith no arguments) is a quick scratchpad for experiments. - Run scripts with
node filename.js. processis a Node.js global: it hasversion,cwd(),argv, and much more.- Browser globals like
window,document, andlocalStoragedon’t exist in Node.js.