Skip to content

Node.js Cheat Sheet – Complete Guide in Hindi

Updated 13 Oct 2026 4 min scan

Node.js एक popular JavaScript runtime है, जिसकी मदद से आप JavaScript का उपयोग browser के बाहर भी कर सकते हैं। इसका इस्तेमाल backend development, REST APIs, command-line tools, automation scripts और real-time applications बनाने में होता है।

अगर आपने JavaScript और Fetch / REST APIs सीख लिए हैं, तो Node.js अगला important step है। इस guide में आप Node.js के basics से लेकर modules, npm, file handling, asynchronous programming और basic HTTP server तक सीखेंगे।

Node.js क्या है?#

Node.js एक JavaScript runtime environment है। यह Google के V8 JavaScript engine का उपयोग करता है और browser के बाहर JavaScript code execute करने देता है।

Simple words mein, browser में JavaScript का इस्तेमाल webpage को interactive बनाने के लिए होता है, जबकि Node.js की मदद से JavaScript का उपयोग server-side code और system tools बनाने में किया जा सकता है।

Node.js का उपयोग क्यों किया जाता है?

  • Backend applications और REST APIs बनाने के लिए।
  • Database के साथ काम करने के लिए।
  • Files पढ़ने और लिखने के लिए।
  • Automation और command-line tools बनाने के लिए।
  • Real-time applications, जैसे chat applications, बनाने के लिए।
  • Build tools और development servers चलाने के लिए।

Node.js कहाँ इस्तेमाल होता है?

Use CaseExample
Backend developmentUser login और product APIs
API developmentREST API server
File handlingCSV या JSON files process करना
AutomationRepetitive tasks automate करना
Real-time appsChat और live notifications
Development toolsBuild scripts और tooling

Node.js कैसे काम करता है?#

Node.js event-driven architecture और non-blocking I/O का उपयोग करता है। इससे यह कई I/O operations को efficiently handle कर सकता है।

मान लीजिए server को database से data प्राप्त करना है। जब database operation चल रहा होता है, तो Node.js दूसरे कामों को आगे बढ़ा सकता है, बजाय इसके कि हर operation के पूरा होने तक पूरा application रुक जाए।

एक simplified flow:

Code
Client Request
      |
      v
Node.js Application
      |
      v
Request Processing
      |
      +----> File / Database / Network I/O
      |
      v
Response to Client

Node.js में JavaScript callbacks सामान्यतः Event Loop पर execute होते हैं। कुछ expensive operations system या Worker Pool की मदद से process हो सकते हैं।

Important: इसका मतलब यह नहीं है कि हर task automatically parallel चलता है। लंबे CPU-intensive operations Event Loop को block कर सकते हैं और application की responsiveness घटा सकते हैं।

Node.js Install कैसे करें?#

Node.js की official website से installer डाउनलोड करें:

https://nodejs.org

Project शुरू करने से पहले supported LTS release चुनना एक अच्छा default है। Existing project में काम करते समय उसकी required Node.js version भी check करें।

Installation verify करने के लिए Terminal या Command Prompt में चलाएँ:

Code
node --version
npm --version

अगर दोनों commands version number दिखाती हैं, तो Node.js और npm command line से accessible हैं।

JavaScript File Run करना

एक file बनाएँ: app.js

Code
console.log("Hello from Node.js!");

अब Terminal में उस directory में जाएँ और चलाएँ:

Code
node app.js

Output:

Code
Hello from Node.js!

इस तरह आप JavaScript code को browser के बिना execute कर सकते हैं।

Node.js REPL क्या है?#

REPL का अर्थ है Read-Eval-Print Loop। यह interactive environment है, जिसमें आप JavaScript expressions को तुरंत execute कर सकते हैं।

Terminal में चलाएँ:

Code
node

अब JavaScript expressions try करें:

Code
2 + 3

Output:

Code
5

REPL से बाहर निकलने के लिए Ctrl + C दो बार दबाएँ या .exit लिखें।

REPL छोटे experiments और debugging के लिए उपयोगी है।

Node.js Project कैसे बनाएँ?#

एक नया project directory बनाएँ:

Code
mkdir my-node-app
cd my-node-app
npm init -y

npm init -y एक default package.json file बनाता है।

Example project structure:

Code
my-node-app/
├── node_modules/
├── app.js
├── package.json
└── package-lock.json
  • app.js — application का entry file हो सकता है।
  • package.json — project metadata, scripts और dependencies रखता है।
  • package-lock.json — npm dependency tree की resolved versions को lock करता है।
  • node_modules/ — installed packages रखता है।

node_modules हर project में शुरुआत से मौजूद नहीं होता। Dependencies install करने के बाद यह directory बन सकती है।

package.json क्या है?#

package.json Node.js project की configuration और metadata file है।

Example:

Code
{
  "name": "my-node-app",
  "version": "1.0.0",
  "description": "A beginner Node.js project",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  }
}

अब application चलाने के लिए:

Code
npm start

यह package.json में defined start script execute करता है।

ध्यान दें कि main field और scripts.start का purpose अलग है। main package entry point के रूप में उपयोग हो सकता है, जबकि scripts.start application start करने का command define करता है।

npm क्या है?#

npm का पूरा नाम historically Node Package Manager के रूप में जाना जाता है। इसका उपयोग JavaScript packages install और manage करने तथा project scripts चलाने के लिए किया जाता है।

Node.js install करने पर सामान्यतः npm भी उपलब्ध होता है।

Common npm Commands

CommandPurpose
npm init -yDefault package.json बनाना
npm installProject dependencies install करना
npm install expressPackage install करना
npm install -D nodemonDevelopment dependency install करना
npm uninstall expressPackage remove करना
npm runAvailable scripts देखना
npm startstart script चलाना
npm run devdev script चलाना, यदि defined हो
npm outdatedOutdated packages check करना

Dependency और Dev Dependency

Dependency: वह package जिसकी application को runtime पर जरूरत होती है।

Dev dependency: वह package जो development या testing के दौरान उपयोग होता है।

Example:

Code
npm install express
npm install -D nodemon

यहाँ Express को regular dependency और Nodemon को development dependency के रूप में install किया गया है।

Node.js Modules क्या हैं?#

Modules code को अलग-अलग files या reusable units में organize करने का तरीका हैं।

Node.js में built-in modules, local modules और third-party packages उपयोग किए जा सकते हैं।

1. Built-in Modules

ये Node.js के साथ उपलब्ध होते हैं। Examples:

  • node:fs — files के साथ काम करने के लिए।
  • node:path — file और directory paths के लिए।
  • node:http — HTTP server बनाने के लिए।
  • node:os — operating system information के लिए।
  • node:events — event-driven programming के लिए।

इनके लिए सामान्यतः npm package install करने की जरूरत नहीं होती।

2. Local Modules

आप अपनी JavaScript files को reusable modules के रूप में बना सकते हैं।

3. Third-party Packages

ये npm जैसे package registry से install किए जाते हैं। Example: Express।

CommonJS और ES Modules#

Node.js में modules के दो common systems हैं।

FeatureCommonJSES Modules
Importrequire()import
Exportmodule.exportsexport
Exampleconst fs = require("node:fs")import fs from "node:fs"
Typical extension.cjs.mjs

.js files का module behavior project की package.json configuration और Node.js के module detection rules पर depend कर सकता है। एक project में module system consistent रखना आसान रहता है।

CommonJS Example

math.js:

Code
function add(a, b) {
  return a + b;
}

module.exports = { add };

app.js:

Code
const { add } = require("./math.js");

console.log(add(5, 3));

Output:

Code
8

ES Modules Example

ES Modules के लिए package.json में यह setting रखें:

Code
{
  "type": "module"
}

math.js:

Code
export function add(a, b) {
  return a + b;
}

app.js:

Code
import { add } from "./math.js";

console.log(add(5, 3));

Output:

Code
8

यहाँ दोनों files ES Modules syntax का उपयोग करती हैं।

File System – Files पढ़ना और लिखना#

Node.js का node:fs module files और directories के साथ काम करने देता है।

File पढ़ना

notes.txt file बनाएँ और उसमें कुछ text लिखें।

Code
const fs = require("node:fs/promises");

async function readFile() {
  try {
    const content = await fs.readFile("notes.txt", "utf8");
    console.log(content);
  } catch (error) {
    console.error("Could not read file:", error.message);
  }
}

readFile();

यह example Promise-based file system API का उपयोग करता है।

utf8 text encoding बताता है। इसे छोड़ने पर file content सामान्यतः Buffer के रूप में मिल सकता है।

File लिखना

Code
const fs = require("node:fs/promises");

async function writeFile() {
  try {
    await fs.writeFile("output.txt", "Hello Node.js!");
    console.log("File written successfully");
  } catch (error) {
    console.error("Could not write file:", error.message);
  }
}

writeFile();

writeFile() file create कर सकता है और existing file को overwrite भी कर सकता है। अगर पुराने content को सुरक्षित रखना है, तो सही API और options चुनें।

Security tip: User-provided file paths को बिना validation के use न करें। ऐसा करने से अनधिकृत files तक पहुँचने का खतरा हो सकता है।

Path Module क्या है?#

node:path file और directory paths को platform के अनुसार handle करने में मदद करता है।

Code
const path = require("node:path");

const filePath = path.join("uploads", "images", "photo.png");

console.log(filePath);
console.log(path.extname(filePath));
console.log(path.basename(filePath));

यहाँ:

  • path.join() path segments को जोड़ता है।
  • path.extname() file extension निकालता है।
  • path.basename() path का अंतिम component निकालता है।

Path separators Windows और Unix-like systems में अलग हो सकते हैं। इसलिए paths को manually concatenate करने की बजाय node:path का उपयोग करना बेहतर है।

Asynchronous Programming#

Node.js applications में file operations, network requests और database calls अक्सर asynchronous होते हैं।

JavaScript में asynchronous operations को handle करने के लिए Promises और async/await बहुत उपयोगी हैं।

Promise Example

Code
function getMessage() {
  return Promise.resolve("Data received");
}

getMessage().then((message) => {
  console.log(message);
});

async/await Example

Code
async function loadMessage() {
  const message = await Promise.resolve("Data received");
  console.log(message);
}

loadMessage();

async function हमेशा Promise return करता है। await उस function के execution को Promise settle होने तक pause करता है, लेकिन इससे पूरा Node.js process अनिवार्य रूप से block नहीं होता।

Event Loop क्या है?

Event Loop Node.js को asynchronous I/O operations के results और callbacks process करने में मदद करता है।

इसे ऐसे समझें: एक coordinator अलग-अलग operations के complete होने पर उनके callbacks को आगे process करवाता है, ताकि application कई I/O tasks को efficiently handle कर सके।

Important: अगर कोई callback बहुत लंबा synchronous calculation करता है, तो Event Loop दूसरे callbacks को समय पर execute नहीं कर पाएगा। CPU-intensive work को जरूरत के अनुसार optimize, partition या worker-based approach में move करना पड़ सकता है।

Environment Variables#

Environment variables का उपयोग configuration values को application code से अलग रखने के लिए किया जाता है।

Example:

Code
console.log(process.env.NODE_ENV);
console.log(process.env.PORT);

अगर environment variables set नहीं हैं, तो इन expressions का result undefined हो सकता है।

एक .env file का example:

Code
PORT=3000
NODE_ENV=development

Recent Node.js versions में .env file load करने के लिए built-in support उपलब्ध है। Compatible version में application इस तरह चलाया जा सकता है:

Code
node --env-file=.env app.js

Environment variables सामान्यतः strings के रूप में उपलब्ध होते हैं। जरूरत पड़ने पर उन्हें validate और convert करें।

Security tip: .env file में credentials या secrets हों, तो उसे public Git repository में commit न करें। Production में platform की secure environment configuration का उपयोग करें।

Node.js से HTTP Server बनाना#

Node.js का built-in node:http module basic HTTP server बनाने की सुविधा देता है।

server.js file बनाएँ:

Code
const http = require("node:http");

const server = http.createServer((req, res) => {
  res.writeHead(200, {
    "Content-Type": "text/plain; charset=utf-8"
  });

  res.end("Hello from Node.js server!");
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Server start करें:

Code
node server.js

अब browser में यह URL खोलें:

Code
http://localhost:3000

आपको Hello from Node.js server! दिखाई देगा।

HTTP Server Code को समझें

  • http.createServer() server create करता है।
  • req incoming request को represent करता है।
  • res client को response भेजने के लिए उपयोग होता है।
  • res.writeHead() response status और headers set करता है।
  • res.end() response complete करता है।
  • server.listen() दिए गए port पर connections accept करना शुरू करता है।

यह एक basic server है। Production application के लिए routing, input validation, security controls, logging और error handling भी जरूरी हैं।

Node.js और Express.js में अंतर#

Express.js Node.js पर काम करने वाला popular web framework है। यह routing और middleware जैसे common web-development tasks को सरल बनाता है।

FeatureNode.jsExpress.js
क्या है?JavaScript runtimeWeb framework
HTTP serverBuilt-in node:http से बना सकते हैंNode.js के ऊपर routing और middleware abstractions देता है
Routingखुद implement कर सकते हैंRouting APIs उपलब्ध हैं
MiddlewareBasic server में खुद manage करना पड़ सकता हैMiddleware system उपलब्ध है
InstallationRuntime install करना होता हैnpm package के रूप में install होता है

Express use करने के लिए पहले Node.js और npm setup होना चाहिए।

Common Node.js Errors#

1. node: command not found

Possible reasons:

  • Node.js install नहीं है।
  • Terminal को Node.js executable नहीं मिल रहा।
  • PATH configuration सही नहीं है।

Fix: Installation verify करें, terminal restart करें और node --version चलाएँ।

2. Cannot find module

Possible reasons:

  • Import या require path गलत है।
  • Required package install नहीं है।
  • File का नाम या extension गलत है।

Fix: File path check करें और जरूरत होने पर npm install चलाएँ।

3. EADDRINUSE

इसका अर्थ है कि requested port पहले से use हो रहा है।

Fix:

  • Existing server process बंद करें।
  • कोई दूसरा port use करें।
  • Check करें कि application की दूसरी instance तो नहीं चल रही।

4. SyntaxError: Cannot use import statement outside a module

यह अक्सर तब होता है जब ES Modules syntax को ऐसे context में use किया जाता है जहाँ file CommonJS के रूप में interpret हो रही हो।

Fix:

  • Project में "type": "module" configure करें, या
  • .mjs extension use करें, या
  • CommonJS syntax के साथ consistent रहें।

5. File read करने पर ENOENT

इसका अर्थ है कि requested path पर file नहीं मिली।

Fix: File name, path और working directory verify करें। Relative paths current working directory के अनुसार resolve हो सकते हैं।

Node.js Best Practices#

  • Project में Node.js version को document और manage करें।
  • Built-in modules को node:fs, node:path जैसे explicit specifiers के साथ import करना अच्छा practice है।
  • package.json और lockfile को version control में रखें।
  • Async operations के errors handle करें।
  • Secrets को source code में hardcode न करें।
  • User input validate करें और file access को restrict करें।
  • Production code में अनावश्यक synchronous I/O से बचें।
  • Event Loop को लंबे CPU-intensive tasks से block न करें।
  • Dependencies को review करें और unnecessary packages install न करें।
  • Production server में logs, monitoring और graceful shutdown की योजना रखें।

Node.js Quick Reference#

TaskCommand / API
Node.js versionnode --version
npm versionnpm --version
JavaScript file runnode app.js
Project initializenpm init -y
Package installnpm install package-name
Built-in file systemnode:fs
Promise-based file systemnode:fs/promises
Path utilitiesnode:path
HTTP servernode:http
Environment variablesprocess.env
Current directoryprocess.cwd()
Platform informationprocess.platform
Exit codeprocess.exitCode

Knowledge Check#

  1. Node.js क्या है और इसका उपयोग क्यों किया जाता है?
  2. Node.js और browser JavaScript में क्या अंतर है?
  3. npm का क्या काम है?
  4. package.json और package-lock.json में क्या अंतर है?
  5. CommonJS और ES Modules में क्या अंतर है?
  6. node:fs/promises का उपयोग क्यों किया जाता है?
  7. Event Loop क्या करता है?
  8. process.env का उपयोग किसलिए होता है?
  9. EADDRINUSE error का क्या अर्थ है?
  10. Node.js और Express.js में क्या अंतर है?

Practice Exercises#

Exercise 1: Hello Node.js

एक app.js file बनाएँ जो आपका welcome message और JavaScript calculation का result print करे।

Exercise 2: File Handling

एक text file बनाएँ, उसे Node.js से पढ़ें और उसका content console में print करें।

Exercise 3: Reusable Module

एक math.js module बनाएँ जिसमें add() और subtract() functions export हों। उन्हें app.js में import करके use करें।

Exercise 4: Environment Variables

PORT नाम का environment variable set करें और Node.js application में उसका value print करें।

Exercise 5: Mini HTTP Server

node:http का उपयोग करके एक server बनाएँ जो / पर welcome message और /about पर about information return करे।

Exercise 6: Mini Project

एक simple Notes CLI application बनाएँ जो:

  • नई note file में save करे।
  • Existing notes पढ़ सके।
  • Missing file या invalid input पर useful error message दिखाए।

Summary#

इस Cheat Sheet में आपने सीखा:

  • Node.js क्या है और इसका उपयोग कहाँ होता है।
  • Node.js install और JavaScript files run कैसे करते हैं।
  • REPL, npm और package.json क्या हैं।
  • Built-in, local और third-party modules कैसे काम करते हैं।
  • CommonJS और ES Modules में क्या अंतर है।
  • File System और Path modules कैसे use करते हैं।
  • Promises, async/await और Event Loop की basics।
  • Environment variables और basic HTTP server।
  • Common errors, security और best practices।

Node.js सीखने के बाद आप JavaScript का उपयोग backend applications और command-line tools बनाने में कर सकते हैं।

आगे क्या सीखें?#

अगला topic है npm Cheat Sheet। इसमें आप package installation, dependency management, scripts, versioning और project workflows को detail में सीखेंगे।

Suggested internal links:

Publish करने से पहले internal links के URLs verify करें और केवल published pages के links रखें.