What Is Node.js? Install It and Run Your First Script

Node.js and Express: Build a Back End API from Scratch · Node.js Basics · 6 min read · Updated

From file to running program

  1. You write a JavaScript file, such as hello.js.
  2. You run it with the node command. Node reads the file and runs it with the V8 engine.
  3. Node can use your computer directly: files, the network and the command line.
  4. The results appear in the terminal, because console.log writes there and not in a browser.

JavaScript began as a language that runs inside web browsers. It made buttons click and menus open. Node.js takes the same language and lets it run on a computer or server, outside the browser. That one change means a person who knows JavaScript can also write the back end of a website: the part that stores data, checks passwords and answers requests from the front end.

Node.js is not a new language and it is not a framework. It is a runtime: a program that reads your JavaScript file and runs it. Under the hood it uses V8, the same engine that runs JavaScript in Chrome, plus a library of tools for things a browser does not allow, such as reading files, opening network connections and starting servers. In this course you will use Node.js to build a real back end, a small task API with login, step by step.

What people build with Node.js

  • Web servers and APIs that a website or a mobile app talks to. This is what you will build.
  • Command line tools, for example scripts that rename a thousand files or clean a spreadsheet export.
  • Build tools. Many front end tools you may already use, such as Vite and webpack, run on Node.js.
  • Real-time apps such as chat, using web sockets.
  • Automation and scheduled jobs, such as sending a daily report.

Node.js is a good fit when your program spends most of its time waiting for things, such as the network or a database. It is less suited to heavy number crunching on a single thread, such as video editing. We will see why in the lesson on async code.

Install Node.js

Go to the official website, nodejs.org, and download the version marked LTS. LTS means long-term support: it receives security fixes for a long time, so it is the safe choice for learning and for real projects. Run the installer and keep the default options. This course needs Node.js 20.11 or newer, and newer LTS versions are fine. If you already have Node.js, you only need to check the version.

Open a terminal. On Windows, use PowerShell or the Terminal app. On macOS, use Terminal. On Linux, use your usual terminal. Then type these two commands.

node -v
npm -v

Each prints a version number such as v22.12.0 for Node and 10.9.0 for npm. The numbers on your computer will probably be different, and that is fine. npm is Node's package manager. It comes with Node and downloads libraries that other people wrote. You will use it from the next lesson.

The REPL: try JavaScript instantly

Type node on its own and press Enter. You get a prompt where you can type JavaScript and see the result straight away. This is called the REPL (read, evaluate, print, loop). It is great for quick experiments.

> 2 + 3
5
> const prices = [120, 80, 650]
undefined
> prices.reduce((a, b) => a + b, 0)
850
> .exit

Press Ctrl+C twice, or type .exit, to leave. The REPL is for testing small ideas. Real programs live in files.

Your first script

Create a new folder called taskapi for the whole course, and open it in your editor (VS Code is a popular free choice). Create a file named hello.js with the code below.

const name = 'Asha';
const hours = 3;
console.log(`Hello, ${name}! You have studied for ${hours} hours.`);
console.log(process.version.startsWith('v') ? 'Node is running' : 'odd');
console.log(process.argv.slice(2));

Run it from the terminal, in the same folder, and add two extra words after the file name.

node hello.js one two
Hello, Asha! You have studied for 3 hours.
Node is running
[ 'one', 'two' ]

Look at what happened. console.log printed to the terminal instead of the browser console. The backtick string with ${name} is a template string, which puts a variable inside text. And process.argv is a list of the words typed on the command line. We removed the first two entries with slice(2), because those are the path to Node and the path to your script. process is a global object that Node gives you. It describes the running program.

What is different from the browser?

  • There is no window and no document. A server has no web page. Code that touches the page, such as document.getElementById, will fail.
  • There is a process object, a way to read files, and a way to start a server.
  • Your code runs once, on your machine, and you control the version of Node. In a browser, your code runs on every visitor's device and version.
  • The rest of the language is the same: variables, functions, arrays, objects, classes, promises, async and await.

Because the language is the same, you can already read most of the code in this course. The new part is the tools Node provides, and the habits of back-end work: validating input, handling errors and protecting data.

Where this course goes

There are 15 lessons. First the basics: modules, files and async code. Then plain Node servers, then Express, a library that makes servers easier to write. Then you build and test a small task API with login, store data in a file and in MongoDB, protect it, and get it ready for a server. Every code sample in the course was run before it was written here.

Your task

  1. Install the LTS version of Node.js and run node -v and npm -v.
  2. Create the folder taskapi and the file hello.js. Change the name and the number of hours.
  3. Run node hello.js with three words after it and check that all three appear in the list.
  4. Open the REPL, work out the total of three numbers with reduce, and leave with .exit.

How to check it worked

  • node -v prints a version that starts with v20 or higher.
  • Running node hello.js one two prints your greeting and the list [ 'one', 'two' ].
  • You can explain in one sentence why document does not exist in Node.js.

Frequently asked questions

Is Node.js a programming language?

No. The language is JavaScript. Node.js is a runtime that runs JavaScript outside the browser and adds tools for files, networks and servers.

Which Node.js version should I install?

The current LTS (long-term support) version from nodejs.org. It gets security fixes for a long time. This course needs Node.js 20.11 or newer.