Running Node.js programs with files, arguments, and automatic restarts

Choose how to run JavaScript and pass command-line arguments to a script.

🎯 Introduction

By the end of this article, you will be able to choose how to run JavaScript and pass command-line arguments to a script.


🛠 Choose the script path

You have already run a JavaScript file. Normally, use node path/to/file, with the path relative to the terminal's current directory. If the file is in scripts, run node scripts/greet.mjs; after entering scripts, run node greet.mjs instead.

Create greet.mjs to practice passing arguments at startup. The next section provides its contents.

Running node alone opens the REPL, an interactive execution environment. Enter .exit to leave it. This course consistently saves examples as files and runs them.

🧱 Accept command-line arguments

Command-line arguments are inputs passed to a program at startup. You can pass a name to the script instead of editing the file each time.

First put this line in greet.mjs to see what Node.js receives:

greet.mjs
console.log(process.argv)

Run node greet.mjs Alex. The result looks like this:

[ '/local/path/node', '/project/path/greet.mjs', 'Alex' ]

process provides information about the running program, and argv is its array of startup arguments. The first two entries are the Node.js executable path and script path. The name passed to the script is at argv[2]. The exact first two paths vary by computer.

Once you have identified the input position, replace the contents with:

greet.mjs
const name = process.argv[2] ?? 'friend'
console.log(`Hello, ${name}`)

Running node greet.mjs Alex prints Hello, Alex; running node greet.mjs prints Hello, friend because the default is used when no name is supplied.

Running node greet.mjs "Alex Smith" prints Hello, Alex Smith. Quotes keep text containing spaces in one argument. All these arguments are strings; convert and validate them when you need numbers.


🧰 Rerun automatically after changes

During development, use Node.js's --watch option to watch for file changes:

node --watch greet.mjs Alex

The program first prints its greeting. After you edit and save the file, Node.js restarts it and the terminal shows the new result. --watch comes before the filename because it is a Node.js option; Alex after the filename is a script argument.

Watch mode keeps running; press Ctrl+C when finished. A regular node greet.mjs run exits naturally when no work remains.


❓ Questions and answers

Q1: Why does entering node not exit immediately?

You entered the REPL, which waits for expressions. Use .exit to leave it.

Q2: Do command-line arguments become numbers automatically?

No. They are strings. Convert and validate them when numbers are required.

Q3: Do I need to rerun after editing a file?

Regular file execution requires restarting. With --watch, Node.js watches changes and restarts for you.


🧪 Exercise

Make greet.mjs accept a name and a city. Run it with a name containing spaces and check that the full argument arrives.

// Write your code here

🎉 Try these techniques with your own examples!