Running Your First Script
Now that Node.js is installed on your machine, it is time to make it do some work. In this chapter you will write a small JavaScript file and run it with the node command. You will also see how to pass extra information to your script from the command line. These are small steps, but they are the foundation for everything else in this tutorial.
What Does "Running a Script" Mean?
A script is simply a text file containing JavaScript code. When you run a script, a program reads that file and executes each line, top to bottom. In the browser, the browser does that reading and executing. With Node.js, a program called node does the same job — but outside the browser.
Your First Program
Open a plain text editor and create a file named hello.js. The .js ending tells you (and Node.js) that the file holds JavaScript code. Type this line into it:
// hello.js — run with: node hello.js
console.log('Hello, Node.js!');console.log is a command that prints text to the screen. The line starting with // is a comment — a note for humans that Node.js ignores.
Now open your terminal, move to the folder where you saved the file, and type:
node hello.jsYou should see this printed on the next line:
Hello, Node.js!That is it. You have just run JavaScript outside a browser. Nothing was downloaded, no page was opened — the node program read your file and followed its instructions directly.
Giving Your Script Extra Information
Real programs often need input. You can pass words after the command, and your script can read them. Save this as greet.js:
// greet.js — run with: node greet.js Maria
const name = process.argv[2];
console.log(`Hello, ${name}!`);process.argv is a list of everything typed on the command line. Item 0 is the node program, item 1 is your file name, and item 2 is the first word you type after that. Run it like this:
node greet.js MariaOutput:
Hello, Maria!A Few Things Worth Knowing
- Node.js runs your file once, top to bottom, then exits. There is no page that stays open.
- You must run the command from the folder that contains your file, or give the full path to it.
- A common mistake is typing
node.js helloinstead ofnode hello.js— the command isnode, and the file name comes after. - If you change the file, run the command again to see your changes.
Try a Slightly Bigger Script
Here is a script that does a small piece of real work — a tiny calculation loop:
// countdown.js — run with: node countdown.js
for (let i = 5; i > 0; i--) {
console.log(i);
}
console.log('Liftoff!');Run it with node countdown.js and watch it count down. Change the starting number and run it again. Editing, running, checking the result — that loop is exactly how you will work with Node.js from now on.
Where This Fits
Running a file with node is the basic unit of everything in this tutorial. The web servers, file handling, and modules coming later are all just longer scripts started the same way: with the node command.
Exercise
What command do you type to run a file named hello.js with Node.js?
Show answer
node hello.js — the program is called node, and the file name follows it.
Exercise
In process.argv, which position holds the first word you type after the file name?
Show answer
Position 2 — positions 0 and 1 are taken by the node program and the script file name.
Spotted a mistake, or code that does not work?