Introduction to Node.js

By Syncfusion®, Inc. · Last updated

What Node.js is and why it was made, its history, V8, libuv and C++ bindings, Node.js versus browser JavaScript, Deno and Bun, LTS versus Current, installing Node.js and version managers, running scripts, the REPL, common CLI flags, and the global object, console and built-in globals.

Node.js Introduction

Node.js = a program that runs JavaScript outside the web browser.

  • You already know JavaScript from web pages.
  • Node.js lets the same JavaScript run on your own computer or on a server.
  • It can read files, talk to networks and start web servers.
  • You start it from the terminal with the node command.

Simple lesson: Node.js takes JavaScript out of the browser and lets it work on your computer.

A Real-World Example

Think of a class teacher at Greenfield School.

  • She has the marks of 40 students in a file.
  • She wants a report: total, average and the topper.
  • Doing this by hand takes an hour.
  • A 20-line Node.js script reads the file and prints the report in one second.
  • No web page and no browser is needed. Only the terminal.

Node.js is not a new language. It is a place where JavaScript runs, just like the browser is a place where JavaScript runs.

What You Will Learn

In this lesson, you will learn:

  1. What Node.js is and why it was made
  2. A short history of Node.js
  3. What is inside Node.js: V8, libuv and C++ bindings
  4. How Node.js is different from browser JavaScript
  5. Node.js, Deno and Bun, and LTS versus Current versions
  6. How to install Node.js and check its version
  7. How to run scripts, use the REPL and common command-line flags
  8. The global object, the console toolbox and other built-in tools

Your First Node.js Program

A Node.js program is a normal .js file.

  • You write JavaScript in a file, such as app.js.
  • You run it with the command node app.js.
  • Node.js reads the file from top to bottom.
  • Everything you pass to console.log appears in the terminal.

Click ▶ Run and read the Terminal panel.

  • The command: the first line shows $ node app.js. This is what you would type on your own computer.
  • The output: each console.log printed one line.
  • No browser: there is no page preview. A Node.js program talks through the terminal.

Change the marks in the marks array and press ▶ Run again. The total changes. Press Restore code to go back.

Simple lesson: write JavaScript in a file, run it with node, and read the answer in the terminal.

What Is Node.js?

Runtime = a program that runs your code and gives it tools to use.

  • The browser is a runtime. It gives JavaScript tools like document and buttons.
  • Node.js is also a runtime. It gives JavaScript tools like files, folders and networks.
  • JavaScript = the language. Node.js = one place where it runs.

Animation: the same JavaScript file can run in a browser or in Node.js

app.js

Browser (Chrome, Edge) window and document buttons, forms, pages runs when a page opens

Node.js (your computer) files and folders servers and networks runs from the terminal

The same JavaScript language runs in two places. Each place gives it different tools.

What Can You Build with Node.js?

  • Command-line tools, such as a script that makes report cards
  • Web servers and APIs, such as the backend of a shopping app
  • Build tools, such as Vite, which React and Vue projects use
  • Real-time apps, such as chat apps and live scoreboards
  • Automation scripts, such as renaming 500 photos in one go

Simple lesson: JavaScript is the language. Node.js is the runtime that lets it work outside the browser.

Why Node.js Was Created

Here is a small story first.

  • A restaurant has one waiter.
  • Table 1 orders food. The waiter goes to the kitchen.
  • He stands there until the food is ready.
  • Table 2 and Table 3 wait. Nobody takes their order.
  • A smarter waiter gives the order to the kitchen and walks back.
  • He takes orders from Table 2 and Table 3 while the food cooks.

Now the mapping: the smart waiter is how Node.js works.

Animation: a blocking waiter serves tables one by one, a non-blocking waiter takes all orders first and serves them as food gets ready Blocking: the waiter waits at the kitchen Non-blocking (Node.js): the waiter keeps taking orders Table 1

order

cooking

serve Table 2

order

cooking

serve Table 3

order … Table 1

order

cooking

serve Table 2

order

cooking

serve Table 3

order

cooking

serve all 3 served early

The purple line is time. The non-blocking waiter finishes all three tables much sooner.

  • Blocking = stop and wait until a slow job finishes.
  • Non-blocking = start a slow job, keep working, and come back when it is done.
  • Slow jobs are things like reading a file, asking a database or calling another website.
  • In 2009, most servers used a blocking style. Each waiting user held up memory.
  • Node.js was made so one program could serve many users without waiting.

Simple lesson: Node.js was built to keep working while slow jobs finish in the background.

History of Node.js

Animation: a timeline of Node.js from 2009 to today

2009Ryan Dahl showsNode.js 2010npm, thepackage tool,arrives 2015Node.jsFoundation isformed 2019Moves to theOpenJSFoundation 2022Node.js 18:built-in fetchand test runner 2027Node.js 27: onemajor version ayear

Node.js grew from one person's project into a project run by a foundation.

  • 2009: Ryan Dahl showed Node.js to the world.
  • 2010: npm arrived. It made sharing code packages easy.
  • 2015: a split in the community ended, and the Node.js Foundation was formed.
  • 2019: Node.js moved to the OpenJS Foundation, which looks after it today.
  • 2022: Node.js 18 added a built-in fetch and a built-in test runner.
  • Today: new major versions come out on a fixed plan. From Node.js 27 (2027), the plan is one major version a year.

Simple lesson: Node.js started as one person's idea and is now run by a large open community.

Inside Node.js: V8, libuv and C++ Bindings

Node.js is built from a few parts that work together.

  • V8 = the JavaScript engine from Google Chrome. It runs your JavaScript code.
  • libuv = a C library that talks to the operating system. It handles files, network and timers without blocking.
  • C++ bindings = the bridge between JavaScript and those C and C++ parts.
  • Node.js APIs = ready tools like fs (files) and http (servers) that you call from JavaScript.
  • Thread pool = a few helper workers inside libuv. They do some slow jobs, like reading files, in the background.

Animation: a file request travels from your code through V8, the C++ bindings and libuv to the operating system, and the result comes back

Your JavaScript (app.js) readFile("marks.txt")

V8 engine runs your JavaScript

Node.js APIs + C++ bindings fs, http, process

libuv event loop, thread pool

Operating system disk, network, timers

1. app.js asks for a file 2. V8 and the bindings pass the job down 3. libuv asks the operating system 4. The file data comes back up

Your code sits on top. Node.js passes slow jobs down to libuv and the operating system.

Watch the orange dot.

  • Going down: your request passes from JavaScript to V8, then to the bindings, then to libuv.
  • At the bottom: the operating system does the real work.
  • Coming up: the answer travels back and your code gets the result.

Simple lesson: V8 runs your code, and libuv does the slow work with the operating system.

Node.js vs Browser JavaScript

The language is the same. The tools are different.

TopicBrowser JavaScriptNode.js
Where it runsInside a web pageIn the terminal or on a server
Main global objectwindowglobal (and globalThis)
Page toolsdocument, buttons, formsNone, there is no page
Files on diskNot allowed, for safetyYes, with the fs module
Program detailsnavigator, locationprocess (version, platform, arguments)
Who can run itAnyone who opens the pageThe person who starts the script

Click ▶ Run and read the terminal.

  • window and document: both are undefined. Node.js has no web page.
  • process: it exists. It tells you the Node.js version and the operating system.

Code that uses document or window will crash in Node.js. Code that uses process or fs will not work in a browser.

Simple lesson: same language, different tools. Check which runtime your code is written for.

Node.js vs Deno vs Bun

Node.js is not the only JavaScript runtime outside the browser.

RuntimeMade byWhat it is known for
Node.jsRyan Dahl, now the OpenJS FoundationThe oldest and most used. Huge npm package world.
DenoRyan Dahl again, in 2018Built-in TypeScript and safety permissions turned on by default
BunJarred Sumner, first released in 2022Speed, plus a package manager and test runner in one tool
  • Deno and Bun can run many npm packages, but not always every one.
  • Most jobs, tutorials and companies still use Node.js.
  • What you learn here, such as modules, async code and streams, helps with all three.

Simple lesson: learn Node.js first. The ideas carry over to Deno and Bun.

LTS vs Current Releases

Node.js has two kinds of versions.

  • Current = the newest version, with the newest features.
  • LTS (Long Term Support) = a stable version that gets fixes for a long time.
  • Up to Node.js 26, even-numbered versions, like 22 and 24, became LTS.
  • Odd-numbered versions, like 23, stayed Current for a short time and then ended.
  • From Node.js 27 (2027), the plan changes: one major version a year, and every version becomes LTS.
  • Companies use LTS, because they want fewer surprises.

Even-numbered Node.js versions get long-term support, odd-numbered versions end early Each bar is one major version. Time goes from left to right. v20

Current

LTS: long support v21

Current

ends v22

Current

LTS: long support v23

Current

ends v24

Current

LTS: long support Up to Node.js 26: even numbers become LTS, odd numbers end early.

Use an LTS version for real projects. Try Current to see new features.

For learning and for real projects, install the LTS version shown on nodejs.org.

Simple lesson: pick LTS for safety. Pick Current only to try new features.

Prerequisites

You need a little JavaScript before this course.

  • Basic JavaScript: variables, if, loops, functions and objects. See Introduction, Function Basics and Object Fundamentals in the JavaScript course.
  • Promises and async/await: many Node.js tools are asynchronous. See Promises and Async/Await.
  • Command-line basics: open a terminal and move between folders.

These terminal commands are enough to start:

JobWindows (PowerShell)Mac and Linux
Show the current folderpwdpwd
List filesdir or lsls
Go into a foldercd reportscd reports
Go up one foldercd ..cd ..
Make a foldermkdir reportsmkdir reports

Simple lesson: basic JavaScript and a few terminal commands are all you need.

Course Roadmap

The six chapters of this course, followed by the Express.js course

1Foundations 2AsyncNode.js 3System andFiles 4Network andParallel 5Quality andProduction 6Project andReference

Next course: Express.js

Each chapter uses what the one before it taught.

  • Foundations: what Node.js is, modules and npm packages.
  • Asynchronous Node.js: the event loop, events and error handling.
  • System and Files: the process object, the file system and streams.
  • Networking and Parallelism: HTTP servers, child processes and worker threads.
  • Quality and Production: testing, debugging, security and going live.
  • Project and Reference: one full project and a cheat sheet.

After this course, the Express.js course shows how a framework makes web servers easier.

Simple lesson: each chapter builds on the last, and Express.js comes next.

Installing Node.js

  1. Open nodejs.org in your browser.
  2. Download the LTS installer for your computer.
  3. Run the installer and keep the default options.
  4. Close any open terminal and open a new one.
  5. Type node -v and press Enter.
node -v
npm -v
  • node -v prints the Node.js version, such as v24.11.0. Your number may be different.
  • npm -v prints the npm version. npm comes with Node.js.
  • If you see "not recognized", close the terminal and open a new one.

Version Managers: nvm and fnm

Version manager = a tool that installs many Node.js versions and switches between them.

  • One project may need Node.js 22. Another may need Node.js 24.
  • A version manager lets both live on one computer.
  • nvm works on Mac and Linux. nvm-windows is a separate tool for Windows.
  • fnm is a fast version manager that works on Windows, Mac and Linux.
# with nvm
nvm install --lts      # install the newest LTS version
nvm use 22             # switch to Node.js 22

# with fnm
fnm install --lts
fnm use 22

Simple lesson: install the LTS from nodejs.org, or use nvm or fnm to keep many versions.

Running Scripts with node

  • node app.js runs the file app.js in the current folder.
  • node app.ts runs a TypeScript file. Recent Node.js versions remove the types and run the code. Pick JavaScript or TypeScript for the labs with the Language menu at the top.
  • Words after the file name are arguments. The script can read them.
  • process.argv = the list of words typed in the command.
  • process.argv[0] is the path to Node.js. process.argv[1] is the script. Your own words start at process.argv[2].

Click ▶ Run. The lab runs node app.js Ravi 8.

  • process.argv[2] became Ravi.
  • process.argv[3] became 8.
  • Note: arguments always arrive as text, even numbers like 8.

Watch Mode

node --watch app.js
  • --watch runs the script again every time you save the file.
  • Press Ctrl + C to stop it.

Simple lesson: node file.js runs a script, and process.argv holds the extra words.

The Node.js REPL

REPL = Read, Eval, Print, Loop.

  • Type node with no file name and press Enter.
  • Node.js reads the line you type.
  • It evaluates (runs) it.
  • It prints the answer.
  • It loops back and waits for your next line.

Animation: the REPL reads what you type, evaluates it, prints the answer and loops back for the next line

Read

Eval

Print

Loop

> 2 + 3 5 > _ You type, Node answers

Read, Eval, Print, Loop: one line in, one answer out, then wait for the next line.

$ node
Welcome to Node.js v24.11.0.
Type ".help" for more information.
> 2 + 3
5
> const name = "Asha"
undefined
> name.toUpperCase()
'ASHA'
> _
'ASHA'
> .exit
  • _ (underscore) holds the last answer.
  • Press the up arrow to bring back an earlier line.
  • .help lists the REPL commands.
  • .exit, or Ctrl + C two times, leaves the REPL.

The REPL is a quick calculator and test bench. This page's labs run whole files instead, like node app.js.

Simple lesson: type node alone to try one line at a time.

Common CLI Flags

Flag = an extra word starting with - or -- that changes what a command does.

FlagShort formWhat it doesExample
--version-vPrints the Node.js versionnode -v
--help-hLists the flags Node.js knowsnode --help
--check-cChecks the file for typing mistakes without running itnode --check app.js
--eval-eRuns the code inside the quotesnode -e "console.log(5 * 2)"
--print-pRuns the code and prints the answernode -p "5 * 2"
--watchRuns again when you save the filenode --watch app.js
$ node -e "console.log('Hi from eval')"
Hi from eval
$ node -p "365 * 24"
8760
  • -e prints only what your code logs.
  • -p prints the answer of the expression for you.

Click ▶ Run. The lab runs node --check app.js.

  • The error: SyntaxError means the code is typed wrongly.
  • The cause: the closing ) is missing after subject.
  • Fix it: add the ) and press ▶ Run again. Now nothing is printed, which means the code is fine.

Simple lesson: flags change what node does. --check finds typing mistakes before you run.

Globals and Built-in APIs

Global = a name you can use in any file without importing it.

global and globalThis

  • globalThis = the standard name for the global object. It works in browsers and in Node.js.
  • global = the older Node.js-only name for the same object.
  • Tools like console and setTimeout live on it.
  • Adding your own values to it is usually a bad idea, because any file can change them.

Simple lesson: use globalThis when you need the global object.

The console Toolbox

console = a built-in tool for printing messages.

  • console.log() prints normal output.
  • console.error() prints error output.
  • console.table() prints an array of objects as a table.
  • console.group() and console.groupEnd() indent lines into a group.
  • console.time() and console.timeEnd() measure how long code takes.

Click ▶ Run and read the terminal.

  • The table: console.table made rows from the array and columns from the object keys.
  • The group: Asha is indented because it is inside the Toppers group.
  • The timer: the time in ms may be different every run.
  • The error line: it appears in red. In a real terminal it goes to a separate channel called stderr.

console.table is great for checking data quickly. Try adding a fourth student and run it again.

Web-Compatible Globals

Some web tools also work in Node.js. You do not need to import them.

  • URL = reads the parts of a web address.
  • TextEncoder = turns text into bytes.
  • structuredClone = makes a full, deep copy of an object.
  • fetch = gets data from a website. Its own lesson comes later.
  • setTimeout = runs a function after a delay.
  • URL: searchParams.get("class") read 8 from the address.
  • structuredClone: changing the copy did not change the original.

Node.js-Specific Globals

These names exist only in Node.js.

  • process = facts about the running program: version, platform, arguments and folder.
  • Buffer = raw bytes, used with files and networks.
  • setImmediate = runs a function right after the current code finishes.
  • __dirname and __filename = the folder and file of the script, in CommonJS files only.
  • require = loads a module, in CommonJS files only.
  • setImmediate: line 3 printed last, even though it was written first.
  • Buffer: 48 69 are the bytes for the letters H and i.
  • __dirname and require: you will meet them in Modules in Node.js.

Simple lesson: some globals come from the web, and some, like process, belong only to Node.js.

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
Typing node app.js inside the REPLThe REPL runs filesLeave the REPL with .exit, then run node app.jsSyntaxError, because the REPL reads it as JavaScript
Using document or window in a scriptNode.js has a web pageNode.js has no page. Use process and modulesReferenceError: document is not defined
Running a file from the wrong folderNode.js finds files anywhereUse cd to go to the folder firstError: Cannot find module
Installing an odd-numbered version for a projectNewest is always bestUse the LTS versionSupport ends soon, and tools may break
Treating process.argv[2] as a numberArguments keep their typeArguments are always text. Use Number()"8" + 1 gives "81"
Using an old terminal after installingThe terminal sees new programs at onceOpen a new terminal'node' is not recognized

TL;DR

  1. Node.js = a runtime that runs JavaScript outside the browser.
  2. Runtime = a program that runs code and gives it tools.
  3. Non-blocking = start a slow job, keep working, handle the result later.
  4. V8 = runs JavaScript. libuv = does files, network and timers with the operating system.
  5. LTS = the stable version with long support. Use it for real projects.
  6. REPL = Read, Eval, Print, Loop. Start it with node.
  7. Flags = extra words that change a command.

    • -v version, --check syntax check, -e run code, -p print an answer, --watch rerun on save
  8. globalThis = the global object. process = facts about the running program.

Check Yourself

Exercises

What to Remember

  • Node.js runs JavaScript outside the browser, from the terminal.
  • It was built to keep working while slow jobs finish.
  • V8 runs your code. libuv talks to the operating system.
  • There is no window or document. There is process.
  • Use the LTS version, and run files with node file.js.
  • globalThis, console, URL and process are ready to use without importing.

Next: Modules in Node.js. You will split a program into many files and share code between them.