Skip to content

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.

If you haven’t already, install Node.js from nodejs.org. Choose the LTS version. Verify the installation:

Terminal window
node --version # v20.x or higher
npm --version # 10.x or higher

Node.js includes an interactive shell called the REPL (Read-Eval-Print Loop). Run it with just node:

Terminal window
node

You get a > prompt. Type any JavaScript:

> 2 + 2
4
> const name = 'Bulletin'
undefined
> `Hello from ${name}`
'Hello from Bulletin'
> process.version
'v20.x.x'
> .exit

The REPL is for quick experiments. Notice process — that’s a global Node.js object that doesn’t exist in browsers.

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:

Terminal window
node hello.js

Output:

Hello from Node.js
Node version: v20.x.x
Current directory: /your/current/directory

That’s it. No browser, no HTML, no index.html — just JavaScript executing directly.

Some things you expect from browser JS don’t exist in Node.js:

BrowserNode.js
windowglobal (or globalThis)
documentNot available
localStorageNot available
fetchAvailable (Node 18+)
processNot available in browser
require()Available (CommonJS)
__dirnameAvailable (CommonJS)

And some things Node.js has that browsers don’t:

  • process — access to the running process, environment variables, arguments
  • fs module — read and write the file system
  • path module — work with file paths across operating systems
  • http module — create raw HTTP servers

Node.js exposes command-line arguments via process.argv:

args.js
console.log(process.argv)
Terminal window
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.

  1. Open the REPL and explore: try Object.keys(process) to see what’s on the process object.
  2. Create greet.js that takes a name from process.argv[2] and prints Hello, [name]!. If no argument is passed, print Hello, stranger!.
  3. Run it: node greet.js AliceHello, Alice! and node greet.jsHello, stranger!
  • Install Node.js LTS; verify with node --version.
  • The REPL (node with no arguments) is a quick scratchpad for experiments.
  • Run scripts with node filename.js.
  • process is a Node.js global: it has version, cwd(), argv, and much more.
  • Browser globals like window, document, and localStorage don’t exist in Node.js.