node filename.js after installing Node.js from nodejs.org.
1. What is Node.js
JavaScript was designed for browsers โ it runs inside Chrome, Firefox, or Safari and can manipulate web pages. But for most of its life, JavaScript couldn't do anything outside a browser tab. You couldn't read files, connect to a database, or listen for HTTP requests.
Node.js changed that in 2009. It takes Chrome's V8 JavaScript engine โ the same one that compiles your browser's JavaScript into fast machine code โ and embeds it in a standalone runtime you can install on any computer or server.
index.js
Compiles to machine code
Non-blocking I/O, event loop
Real system operations
Node.js is event-driven and uses non-blocking I/O. This means when Node asks the OS to read a file or make a database query, it doesn't sit idle waiting. It registers a callback and moves on to handle other work. When the file is ready, it fires the callback. This makes Node extremely efficient at handling thousands of simultaneous connections โ perfect for API servers.
UrbanCart's checkout page triggers many simultaneous operations: check inventory, validate payment, send confirmation email, update order database. With Node's non-blocking I/O, all of these can be kicked off concurrently without blocking each other. The server stays responsive to the next customer's request while waiting for the database to reply.
2. Why Node.js
Node.js has become one of the most popular server-side runtimes in the world. Here's why teams choose it:
One Language
JavaScript on both frontend and backend. UrbanCart's React components and its API server share the same language, patterns, and even utility functions.
NPM Ecosystem
Over 2 million packages on npm. Need JWT authentication, email sending, image processing, or PDF generation? There's a battle-tested package for it.
Great for APIs
Node's non-blocking model excels at I/O-heavy workloads โ reading from databases, calling external APIs, serving JSON. Exactly what a REST API does.
3. Hello World in Node.js
Once you have Node.js installed (node --version to verify), you can run any .js file directly from the terminal.
That's it. No build step, no bundler, no browser. Just node filename.js and your code runs. console.log works exactly the same as in the browser โ it just prints to your terminal instead.
4. npm & package.json
npm stands for Node Package Manager. It ships with Node.js and serves two purposes: installing packages from the npm registry, and managing your project's configuration through package.json.
Initialising a Project
Run npm init (or npm init -y to skip the questions) in a new folder. It creates package.json:
dependencies
Packages your app needs in production โ Express, dotenv, cors. Installed with npm install package-name.
devDependencies
Packages only needed during development โ nodemon (auto-restart), jest (testing). Installed with npm install -D package-name.
npm install, npm downloads all dependencies into a node_modules/ folder. Never commit this folder to git โ add it to .gitignore. Anyone who clones your repo runs npm install to restore it from package.json.
5. Running Scripts
The "scripts" field in package.json lets you define shortcut commands. Instead of remembering long commands, you run npm run scriptname.
The start script (npm start) is special โ it doesn't need run. Same for npm test. All other custom scripts need npm run scriptname.
Reading Command-Line Arguments with process.argv
Node passes command-line arguments via process.argv. The first two elements are always the node binary path and the script path โ your actual arguments start at index 2.
6. CommonJS vs ES Modules
Node.js has two module systems. Understanding both is essential because you'll encounter them in different codebases and tutorials.
CommonJS (CJS) โ the original
Uses require() to import and module.exports to export. Files use the .js or .cjs extension. This is the default in Node.js and still used by most packages.
ES Modules (ESM) โ the standard
Uses import / export โ the same syntax as browser JavaScript and React. Files use .mjs or .js with "type": "module" in package.json.
require(), stick with CommonJS. You cannot mix them in the same file without a workaround.
7. Global Objects
Node.js provides several global objects available in every file without importing anything. These replace browser globals like window and document.
.env file, add .env to .gitignore, and use the dotenv package to load them into process.env at startup. Example: require('dotenv').config() at the top of your entry file.
8. Reading & Writing JSON with fs
Node's built-in fs (file system) module lets you read and write files. For JSON data โ like a product catalogue stored on disk โ fs.readFileSync and fs.writeFileSync are the simplest approach.
9. UrbanCart Filter Script
Let's put it all together. This script loads products.json, filters for in-stock gadgets under a price threshold (passed via command-line argument), and writes the results to a new file.
UrbanCart's operations team runs nightly scripts to generate filtered product feeds for promotional emails. This script accepts a max price argument and writes matching in-stock gadgets to deals.json.
products.json and filter-gadgets.js from the examples above, then run node filter-gadgets.js 40 to filter gadgets under $40, or node filter-gadgets.js 100 to get all in-stock gadgets.
10. Quick Reference
A summary of the key Node.js concepts covered in this lesson.
Quiz
require() do in Node.js?process.env used for in Node.js?server.js?You completed the Node.js quiz.
You just brought JavaScript to the server.
Now build a full-stack career that runs anywhere.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ