Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 4

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
Level: GeneralLength: Mid2 min read

Read at another depth

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:

Code example in javascript
// 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:

Code example in bash
node hello.js

You should see this printed on the next line:

Code example in text
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:

Code example in javascript
// 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:

Code example in bash
node greet.js Maria

Output:

Code example in text
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 hello instead of node hello.js — the command is node, 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:

Code example in javascript
// 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?

Report an issue

Chapter: Running Your First Script

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that