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
nodecommand.
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:
- What Node.js is and why it was made
- A short history of Node.js
- What is inside Node.js: V8, libuv and C++ bindings
- How Node.js is different from browser JavaScript
- Node.js, Deno and Bun, and LTS versus Current versions
- How to install Node.js and check its version
- How to run scripts, use the REPL and common command-line flags
- The global object, the
consoletoolbox 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.logappears 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.logprinted 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
documentand 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
fetchand 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) andhttp(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.
| Topic | Browser JavaScript | Node.js |
|---|---|---|
| Where it runs | Inside a web page | In the terminal or on a server |
| Main global object | window | global (and globalThis) |
| Page tools | document, buttons, forms | None, there is no page |
| Files on disk | Not allowed, for safety | Yes, with the fs module |
| Program details | navigator, location | process (version, platform, arguments) |
| Who can run it | Anyone who opens the page | The person who starts the script |
Click ▶ Run and read the terminal.
windowanddocument: both areundefined. 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.
| Runtime | Made by | What it is known for |
|---|---|---|
| Node.js | Ryan Dahl, now the OpenJS Foundation | The oldest and most used. Huge npm package world. |
| Deno | Ryan Dahl again, in 2018 | Built-in TypeScript and safety permissions turned on by default |
| Bun | Jarred Sumner, first released in 2022 | Speed, 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:
| Job | Windows (PowerShell) | Mac and Linux |
|---|---|---|
| Show the current folder | pwd | pwd |
| List files | dir or ls | ls |
| Go into a folder | cd reports | cd reports |
| Go up one folder | cd .. | cd .. |
| Make a folder | mkdir reports | mkdir 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
processobject, 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
- Open nodejs.org in your browser.
- Download the LTS installer for your computer.
- Run the installer and keep the default options.
- Close any open terminal and open a new one.
- Type
node -vand press Enter.
node -v
npm -v
node -vprints the Node.js version, such asv24.11.0. Your number may be different.npm -vprints 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.jsruns the fileapp.jsin the current folder.node app.tsruns 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 atprocess.argv[2].
Click ▶ Run. The lab runs node app.js Ravi 8.
process.argv[2]becameRavi.process.argv[3]became8.- Note: arguments always arrive as text, even numbers like
8.
Watch Mode
node --watch app.js
--watchruns 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
nodewith 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
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.
.helplists 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.
| Flag | Short form | What it does | Example |
|---|---|---|---|
--version | -v | Prints the Node.js version | node -v |
--help | -h | Lists the flags Node.js knows | node --help |
--check | -c | Checks the file for typing mistakes without running it | node --check app.js |
--eval | -e | Runs the code inside the quotes | node -e "console.log(5 * 2)" |
--print | -p | Runs the code and prints the answer | node -p "5 * 2" |
--watch | Runs again when you save the file | node --watch app.js |
$ node -e "console.log('Hi from eval')"
Hi from eval
$ node -p "365 * 24"
8760
-eprints only what your code logs.-pprints the answer of the expression for you.
Click ▶ Run. The lab runs node --check app.js.
- The error:
SyntaxErrormeans the code is typed wrongly. - The cause: the closing
)is missing aftersubject. - 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
consoleandsetTimeoutlive 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()andconsole.groupEnd()indent lines into a group.console.time()andconsole.timeEnd()measure how long code takes.
Click ▶ Run and read the terminal.
- The table:
console.tablemade rows from the array and columns from the object keys. - The group:
Ashais indented because it is inside theToppersgroup. - The timer: the time in
msmay 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")read8from 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.__dirnameand__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 69are the bytes for the lettersHandi.__dirnameandrequire: 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
| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
Typing node app.js inside the REPL | The REPL runs files | Leave the REPL with .exit, then run node app.js | SyntaxError, because the REPL reads it as JavaScript |
Using document or window in a script | Node.js has a web page | Node.js has no page. Use process and modules | ReferenceError: document is not defined |
| Running a file from the wrong folder | Node.js finds files anywhere | Use cd to go to the folder first | Error: Cannot find module |
| Installing an odd-numbered version for a project | Newest is always best | Use the LTS version | Support ends soon, and tools may break |
Treating process.argv[2] as a number | Arguments keep their type | Arguments are always text. Use Number() | "8" + 1 gives "81" |
| Using an old terminal after installing | The terminal sees new programs at once | Open a new terminal | 'node' is not recognized |
TL;DR
- Node.js = a runtime that runs JavaScript outside the browser.
- Runtime = a program that runs code and gives it tools.
- Non-blocking = start a slow job, keep working, handle the result later.
- V8 = runs JavaScript. libuv = does files, network and timers with the operating system.
- LTS = the stable version with long support. Use it for real projects.
- REPL = Read, Eval, Print, Loop. Start it with
node. Flags = extra words that change a command.
-vversion,--checksyntax check,-erun code,-pprint an answer,--watchrerun on save
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
windowordocument. There isprocess. - Use the LTS version, and run files with
node file.js. globalThis,console,URLandprocessare ready to use without importing.
Next: Modules in Node.js. You will split a program into many files and share code between them.