Skip to content

npm Cheat Sheet – Complete Guide in Hindi

Updated 16 Oct 2026 4 min scan

npm JavaScript development में packages install करने, dependencies manage करने और project scripts run करने के लिए इस्तेमाल होने वाला एक important tool है।

जब आप Node.js, React, Vite या किसी दूसरे JavaScript project पर काम करते हैं, तो npm commands का इस्तेमाल बार-बार करना पड़ता है।

इस Cheat Sheet में आप सीखेंगे कि npm क्या है, packages कैसे install करते हैं, package.json और package-lock.json क्या हैं, dependencies कैसे manage करते हैं और common npm errors को कैसे fix करते हैं।

npm क्या है?#

npm एक package manager और command-line tool है, जिसका उपयोग JavaScript packages को install और manage करने के लिए किया जाता है। npm registry में उपलब्ध packages को अपने project में add किया जा सकता है।

Simple words mein, अगर आपको अपने project में कोई ready-made library use करनी है, तो उसे खुद से बनाने के बजाय npm से install कर सकते हैं।

उदाहरण के लिए:

  • React — user interfaces बनाने के लिए।
  • Express — Node.js web applications और APIs बनाने के लिए।
  • Vite — frontend development और build process के लिए।
  • TypeScript — JavaScript में static type checking के लिए।

npm की मदद से आप packages install करने के साथ-साथ project scripts और dependency versions भी manage कर सकते हैं।

npm और Node.js में क्या अंतर है?#

Node.js JavaScript runtime है, जबकि npm packages और project dependencies manage करने का tool है।

FeatureNode.jsnpm
Main purposeJavaScript को browser के बाहर run करनाPackages और dependencies manage करना
Examplenode app.jsnpm install express
Typical useBackend और scripts run करनाLibraries install करना
InstallationNode.js runtime install करनाआमतौर पर Node.js के साथ मिलता है

Node.js install करने के बाद versions check करें:

Code
node --version
npm --version

अगर commands version numbers दिखाती हैं, तो दोनों command-line tools accessible हैं।

npm Registry क्या है?#

npm registry एक public package registry है, जहाँ developers JavaScript packages publish और share कर सकते हैं।

जब आप कोई package install करते हैं, तो npm configured registry से package और उसकी dependencies प्राप्त कर सकता है।

Example:

Code
npm install express

यह command current project में Express package install करती है।

Package की जानकारी देखने के लिए:

Code
npm view express
npm view express version
npm view express description

Package install करने से पहले उसका नाम, documentation, maintenance status और security advisories check करना अच्छी practice है।

npm Project कैसे Initialize करें?#

सबसे पहले project directory बनाएँ:

Code
mkdir my-npm-project
cd my-npm-project
npm init -y

यह command default package.json file बनाती है।

Project structure कुछ इस तरह हो सकता है:

Code
my-npm-project/
├── node_modules/
├── package.json
└── package-lock.json

node_modules और package-lock.json initialization के समय जरूरी नहीं कि मौजूद हों। ये dependencies install होने पर सामान्यतः बनते हैं।

npm init क्या करता है?

npm init project setup करने के लिए questions पूछता है, जैसे project name और version।

Code
npm init

अगर default answers के साथ setup करना हो:

Code
npm init -y

-y का अर्थ है कि npm default values accept कर लेगा।

package.json क्या है?#

package.json project की metadata, scripts, dependencies और अन्य configuration information रखती है।

Example:

Code
{
  "name": "my-npm-project",
  "version": "1.0.0",
  "description": "A sample Node.js project",
  "scripts": {
    "start": "node app.js",
    "dev": "node --watch app.js"
  },
  "dependencies": {
    "express": "^5.0.0"
  }
}

यह illustrative example है। Actual Express version और project scripts अपने project के अनुसार रखें।

Important Fields

FieldPurpose
namePackage या project का नाम
versionProject/package version
descriptionProject का short description
scriptsReusable commands
dependenciesRuntime dependencies
devDependenciesDevelopment dependencies
enginesSupported runtime versions declare करने के लिए
type.js files के module interpretation को configure करने के लिए

हर project में सभी fields होना जरूरी नहीं है।

package-lock.json क्या है?#

package-lock.json resolved dependency tree और package versions की जानकारी record करती है, जिससे project के installations अधिक consistent बनाए जा सकें।

इसे ऐसे समझें:

  • package.json बताती है कि project को कौन-सी dependencies और version ranges चाहिए।
  • package-lock.json resolved dependency versions का अधिक detailed record रखती है।
  • node_modules में installed package files होती हैं।

क्या package-lock.json को Git में commit करना चाहिए?

Application projects में आमतौर पर package-lock.json को version control में commit करना चाहिए।

इससे team members और CI/CD environments में dependency installation को consistent रखने में मदद मिलती है।

npm Commands Cheat Sheet#

Commandकाम
npm initInteractive project setup
npm init -yDefault project setup
npm installProject dependencies install करना
npm install expressExpress install करना
npm install -D nodemonDevelopment dependency install करना
npm uninstall expressPackage remove करना
npm updateDependency ranges के भीतर eligible updates install करना
npm outdatedOutdated packages check करना
npm listInstalled dependency tree देखना
npm list --depth=0Top-level dependencies देखना
npm runAvailable scripts list करना
npm cache verifynpm cache verify करना
npm auditDependency security advisories check करना
npm ciLockfile के आधार पर clean installation करना

npm install के Different Uses#

1. सभी Dependencies Install करना

अगर project में package.json मौजूद है, तो:

Code
npm install

npm project dependencies install करता है और जरूरत के अनुसार lockfile update कर सकता है।

2. Specific Package Install करना

Code
npm install express

इससे Express project dependency के रूप में add होता है।

3. Development Dependency Install करना

Code
npm install -D nodemon

-D या --save-dev package को devDependencies में add करता है।

Testing tools, linters और development utilities अक्सर यहाँ रखे जाते हैं।

4. Exact Version Install करना

Code
npm install express@5.1.0

यह command Express की specified version install करने के लिए है। यह version केवल example है; अपने project के लिए compatible और currently available version verify करें।

अगर package.json में exact version pin करना हो, तो:

Code
npm install --save-exact express@5.1.0

Version pin करने के बाद भी transitive dependencies और environment compatibility पर ध्यान देना चाहिए।

dependencies और devDependencies#

इन दोनों fields का purpose अलग है।

dependencies

वे packages जिनकी application को runtime पर जरूरत होती है।

Examples:

  • Express server में Express package।
  • Application में इस्तेमाल होने वाली database client library।

devDependencies

वे packages जो development, testing या build process में उपयोग होते हैं।

Examples:

  • ESLint
  • Testing tools
  • Development utilities

Install examples:

Code
npm install express
npm install -D eslint

Important: कोई package dependencies में होना चाहिए या devDependencies में, यह इस बात पर निर्भर करता है कि application के production runtime में उसकी जरूरत है या नहीं।

npm Scripts क्या हैं?#

npm scripts package.json में defined commands होते हैं। इनकी मदद से आप बार-बार इस्तेमाल होने वाले commands को short names दे सकते हैं।

Example:

Code
{
  "scripts": {
    "start": "node app.js",
    "dev": "node --watch app.js",
    "test": "node --test"
  }
}

अब scripts इस तरह run कर सकते हैं:

Code
npm start
npm run dev
npm test

ध्यान दें कि scripts तभी काम करेंगे जब उनके corresponding commands या files project में available हों और आपका Node.js version उन्हें support करता हो।

npm start और npm run में अंतर

  • npm start start script चलाता है।
  • npm run dev dev script चलाता है।
  • npm run बिना script name के available scripts list करता है।

start और test जैसे कुछ script names के लिए npm shorthand commands उपलब्ध हैं। Custom script names के लिए सामान्यतः npm run <script-name> use करें।

npm install और npm ci में अंतर#

दोनों commands dependencies install करती हैं, लेकिन उनका behavior अलग है।

Featurenpm installnpm ci
Main useDevelopment और dependency changesClean, reproducible installation
Lockfile requiredनहीं, हर स्थिति में नहींहाँ
Lockfile mismatchजरूरत के अनुसार lockfile update कर सकता हैError देकर fail करता है
Existing node_modulesआमतौर पर existing install को reconcile करता हैInstallation से पहले हटाता है
package.json / lockfileUpdate हो सकते हैंइन files को modify नहीं करता

npm ci कब use करें?

CI/CD pipelines, automated testing और deployment में npm ci उपयोगी है, जब project में committed lockfile मौजूद हो।

Code
npm ci

यह lockfile के साथ clean installation करता है। अगर package.json और lockfile synchronized नहीं हैं, तो पहले issue resolve करें।

Reference: npm ci documentation.

Semantic Versioning (SemVer)#

JavaScript packages के versions अक्सर MAJOR.MINOR.PATCH format follow करते हैं।

Example:

Code
2.4.7
  • 2 — Major version
  • 4 — Minor version
  • 7 — Patch version

सामान्य SemVer convention में:

  • Major: Breaking changes हो सकती हैं।
  • Minor: Backward-compatible functionality add होती है।
  • Patch: Backward-compatible bug fixes होते हैं।

यह convention है; package maintainers के actual release behavior को documentation से verify करना चाहिए।

Caret (^) और Tilde (~) का मतलब

Example:

Code
{
  "dependencies": {
    "package-a": "^2.4.7",
    "package-b": "~2.4.7"
  }
}
Rangeसामान्य अर्थ
^2.4.72.x line में compatible versions, 3.0.0 से कम
~2.4.72.4.x patch updates, 2.5.0 से कम
2.4.7Exact version
>=2.4.7Version 2.4.7 या उससे ऊपर

Caret ranges में 0.x versions के लिए special rules होते हैं। इसलिए शुरुआती version numbers पर सिर्फ caret देखकर assumptions न बनाएँ।

Reference: npm SemVer documentation.

Packages Update कैसे करें?#

पहले outdated packages check करें:

Code
npm outdated

इसके बाद eligible dependency updates के लिए:

Code
npm update

यह command declared version ranges के अनुसार packages update करती है। यह जरूरी नहीं कि हर package को latest major version पर ले जाए।

अगर major version upgrade करना है, तो पहले package changelog और migration guide पढ़ें।

Code
npm install package-name@latest

यह command package की latest published version install करने के लिए है। Production project में इसे चलाने से पहले compatibility और breaking changes check करें।

Best practice: Update के बाद tests और build run करें, फिर changes review करके commit करें।

npm से Package Information कैसे देखें?#

किसी package की published version देखने के लिए:

Code
npm view express version

Package की available versions देखने के लिए:

Code
npm view express versions

Package metadata देखने के लिए:

Code
npm view express

ये commands npm registry की configured source से package information प्राप्त करती हैं।

npx क्या है?#

npx npm ecosystem का command-line tool है, जिसका उपयोग package executables run करने के लिए किया जा सकता है। Modern npm versions में यह npm के साथ उपलब्ध होता है।

Example:

Code
npx cowsay "Hello World"

अगर package locally available नहीं है, तो npm configuration और command के अनुसार उसे fetch करके execute किया जा सकता है।

Security tip: npx से unknown package execute करने से पहले package name और source verify करें। Package code आपके environment में commands चला सकता है या files access कर सकता है।

npm Global और Local Installation#

Local Installation

Code
npm install eslint

यह package current project में install करता है और सामान्यतः project की node_modules directory में उपलब्ध होता है।

Project-specific dependencies के लिए local installation preferred है।

Global Installation

Code
npm install -g package-name

Global installation package को global npm location में install करता है, जिससे उसका executable कई directories से उपलब्ध हो सकता है।

Global installation हर package के लिए जरूरी नहीं है। Project dependencies को global install करने से दूसरे developers के लिए project setup inconsistent हो सकता है।

npm Audit और Security#

npm ecosystem में third-party dependencies security risk ला सकती हैं। Package install करने से पहले उसकी reputation, maintenance और permissions review करना अच्छा practice है।

Dependency advisories check करने के लिए:

Code
npm audit

Fix options देखने के लिए:

Code
npm audit fix

npm audit fix compatible dependency updates लागू कर सकता है, लेकिन इसे blindly run नहीं करना चाहिए। Changes review करें और tests run करें।

अगर किसी issue को fix करने के लिए breaking version upgrade की जरूरत हो, तो npm automatic fix न कर पाए या forceful changes की जरूरत पड़ सकती है। --force को बिना समझे use न करें।

npm Cache#

npm downloaded package data और अन्य information को cache कर सकता है।

Cache verify करने के लिए:

Code
npm cache verify

सिर्फ इसलिए cache clear न करें कि installation fail हो रही है। पहले error message, network, registry और dependency configuration check करें।

अगर cache corruption का स्पष्ट evidence है, तभी cache cleaning पर विचार करें।

Common npm Errors और Fixes#

1. EACCES या Permission Error

Possible reason: npm को किसी directory में लिखने की permission नहीं है।

Fix:

  • Node.js installation और npm directory permissions check करें।
  • उचित Node.js version manager use करने पर विचार करें।
  • हर npm command के आगे sudo लगाना default solution न बनाएँ।

2. ERESOLVE unable to resolve dependency tree

Possible reason: दो या अधिक packages की peer dependency requirements compatible नहीं हैं।

Fix:

  • Error में listed packages और required versions पढ़ें।
  • Compatible package versions install करें।
  • Framework और plugin compatibility verify करें।
  • --legacy-peer-deps को बिना समझे permanent fix न मानें।

3. E404 Not Found

Possible reason: Package name गलत है, requested version उपलब्ध नहीं है या configured registry package को नहीं ढूँढ पा रही।

Fix:

  • Package spelling check करें।
  • npm view package-name चलाएँ।
  • Private registry की configuration verify करें।

4. EADDRINUSE

यह npm-specific error नहीं है, लेकिन Node.js project run करते समय दिख सकता है। इसका मतलब requested port पहले से use हो रहा है।

Fix: Existing process बंद करें या application का port बदलें।

5. package-lock.json और package.json में mismatch

यह समस्या विशेष रूप से npm ci चलाते समय सामने आ सकती है।

Fix:

  • Dependency changes के बाद npm install चलाएँ।
  • Updated package.json और package-lock.json review करें।
  • दोनों files को साथ में version control में commit करें।

6. Package Install होने के बाद भी Command नहीं चलती

Possible reason: Package executable उपलब्ध नहीं है, गलत script use हो रही है या installation incomplete है।

Fix:

  • npm list --depth=0 से installed packages देखें।
  • package.json में scripts check करें।
  • Project-specific executable के लिए npm script या npx use करें।

npm Best Practices#

  • Project की package.json और package-lock.json को version control में रखें।
  • Team projects और CI/CD में जरूरत के अनुसार npm ci use करें।
  • Dependency updates के बाद tests और build run करें।
  • Unknown packages install करने से पहले review करें।
  • Production और development dependencies सही classify करें।
  • npm audit findings को review करें।
  • बिना समझे --force और --legacy-peer-deps का इस्तेमाल न करें।
  • Global installation केवल तब करें जब उसकी वास्तविक जरूरत हो।
  • Secrets और private registry tokens को public repository में commit न करें।
  • Package scripts चलाने से पहले समझें कि वे कौन-से commands execute करते हैं।

npm Quick Reference#

TaskCommand
npm versionnpm --version
Project initializenpm init -y
Install dependenciesnpm install
Install packagenpm install express
Install dev dependencynpm install -D eslint
Remove packagenpm uninstall express
Clean installationnpm ci
List direct dependenciesnpm list --depth=0
Find outdated packagesnpm outdated
Update eligible packagesnpm update
Run start scriptnpm start
Run custom scriptnpm run dev
View package versionnpm view express version
Check advisoriesnpm audit
Verify cachenpm cache verify
Run package executablenpx package-name

Knowledge Check#

  1. npm का मुख्य काम क्या है?
  2. Node.js और npm में क्या अंतर है?
  3. package.json और package-lock.json क्या store करती हैं?
  4. dependencies और devDependencies में क्या अंतर है?
  5. npm install और npm ci कब use किए जाते हैं?
  6. SemVer में Major, Minor और Patch का क्या अर्थ है?
  7. ^ और ~ version ranges में क्या अंतर है?
  8. npm outdated क्या करता है?
  9. npx का उपयोग कब किया जाता है?
  10. npm audit fix चलाने के बाद changes review करना क्यों जरूरी है?

Practice Exercises#

Exercise 1: npm Project Setup

एक नया directory बनाएँ और npm init -y से project initialize करें। package.json में project name और description update करें।

Exercise 2: Dependency Install करें

किसी छोटे Node.js project में Express install करें। फिर package.json में हुए changes देखें।

Exercise 3: npm Scripts बनाएँ

package.json में start और dev scripts बनाएँ। दोनों commands run करके verify करें।

Exercise 4: Dependencies Check करें

npm list --depth=0 और npm outdated चलाएँ। दोनों commands के output में अंतर समझें।

Exercise 5: Lockfile Practice

Project की package-lock.json file का purpose समझें। Dependency change करने के बाद package.json और lockfile में आए changes review करें।

Exercise 6: Mini Project

एक छोटा Node.js application बनाएँ जिसमें कम-से-कम एक external package हो। उसमें npm scripts, proper dependency management और basic error handling शामिल करें।

Summary#

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

  • npm क्या है और npm registry कैसे काम करती है।
  • Project initialize कैसे करते हैं।
  • package.json और package-lock.json का purpose।
  • Dependencies और dev dependencies कैसे manage करते हैं।
  • npm scripts और common commands।
  • Semantic versioning और version ranges।
  • npm install, npm ci और npm update का अंतर।
  • npx, global installation और security basics।
  • Common npm errors और उनके solutions।

npm को अच्छी तरह समझने से React, Vite, Node.js और TypeScript projects में dependency management आसान हो जाता है।

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

अगला topic है Build Tools (Vite) Cheat Sheet। इसमें हम समझेंगे कि frontend build tools क्या हैं, Vite development server कैसे चलाता है, production build कैसे बनती है और environment variables को कैसे configure करते हैं।

Suggested internal links:

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