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 है।
| Feature | Node.js | npm |
|---|---|---|
| Main purpose | JavaScript को browser के बाहर run करना | Packages और dependencies manage करना |
| Example | node app.js | npm install express |
| Typical use | Backend और scripts run करना | Libraries install करना |
| Installation | Node.js runtime install करना | आमतौर पर Node.js के साथ मिलता है |
Node.js install करने के बाद versions check करें:
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:
npm install expressयह command current project में Express package install करती है।
Package की जानकारी देखने के लिए:
npm view express
npm view express version
npm view express descriptionPackage install करने से पहले उसका नाम, documentation, maintenance status और security advisories check करना अच्छी practice है।
npm Project कैसे Initialize करें?#
सबसे पहले project directory बनाएँ:
mkdir my-npm-project
cd my-npm-project
npm init -yयह command default package.json file बनाती है।
Project structure कुछ इस तरह हो सकता है:
my-npm-project/
├── node_modules/
├── package.json
└── package-lock.jsonnode_modules और package-lock.json initialization के समय जरूरी नहीं कि मौजूद हों। ये dependencies install होने पर सामान्यतः बनते हैं।
npm init क्या करता है?
npm init project setup करने के लिए questions पूछता है, जैसे project name और version।
npm initअगर default answers के साथ setup करना हो:
npm init -y-y का अर्थ है कि npm default values accept कर लेगा।
package.json क्या है?#
package.json project की metadata, scripts, dependencies और अन्य configuration information रखती है।
Example:
{
"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
| Field | Purpose |
|---|---|
name | Package या project का नाम |
version | Project/package version |
description | Project का short description |
scripts | Reusable commands |
dependencies | Runtime dependencies |
devDependencies | Development dependencies |
engines | Supported 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.jsonresolved 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 init | Interactive project setup |
npm init -y | Default project setup |
npm install | Project dependencies install करना |
npm install express | Express install करना |
npm install -D nodemon | Development dependency install करना |
npm uninstall express | Package remove करना |
npm update | Dependency ranges के भीतर eligible updates install करना |
npm outdated | Outdated packages check करना |
npm list | Installed dependency tree देखना |
npm list --depth=0 | Top-level dependencies देखना |
npm run | Available scripts list करना |
npm cache verify | npm cache verify करना |
npm audit | Dependency security advisories check करना |
npm ci | Lockfile के आधार पर clean installation करना |
npm install के Different Uses#
1. सभी Dependencies Install करना
अगर project में package.json मौजूद है, तो:
npm installnpm project dependencies install करता है और जरूरत के अनुसार lockfile update कर सकता है।
2. Specific Package Install करना
npm install expressइससे Express project dependency के रूप में add होता है।
3. Development Dependency Install करना
npm install -D nodemon-D या --save-dev package को devDependencies में add करता है।
Testing tools, linters और development utilities अक्सर यहाँ रखे जाते हैं।
4. Exact Version Install करना
npm install express@5.1.0यह command Express की specified version install करने के लिए है। यह version केवल example है; अपने project के लिए compatible और currently available version verify करें।
अगर package.json में exact version pin करना हो, तो:
npm install --save-exact express@5.1.0Version 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:
npm install express
npm install -D eslintImportant: कोई package dependencies में होना चाहिए या devDependencies में, यह इस बात पर निर्भर करता है कि application के production runtime में उसकी जरूरत है या नहीं।
npm Scripts क्या हैं?#
npm scripts package.json में defined commands होते हैं। इनकी मदद से आप बार-बार इस्तेमाल होने वाले commands को short names दे सकते हैं।
Example:
{
"scripts": {
"start": "node app.js",
"dev": "node --watch app.js",
"test": "node --test"
}
}अब scripts इस तरह run कर सकते हैं:
npm start
npm run dev
npm testध्यान दें कि scripts तभी काम करेंगे जब उनके corresponding commands या files project में available हों और आपका Node.js version उन्हें support करता हो।
npm start और npm run में अंतर
npm startstartscript चलाता है।npm run devdevscript चलाता है।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 अलग है।
| Feature | npm install | npm ci |
|---|---|---|
| Main use | Development और dependency changes | Clean, reproducible installation |
| Lockfile required | नहीं, हर स्थिति में नहीं | हाँ |
| Lockfile mismatch | जरूरत के अनुसार lockfile update कर सकता है | Error देकर fail करता है |
Existing node_modules | आमतौर पर existing install को reconcile करता है | Installation से पहले हटाता है |
package.json / lockfile | Update हो सकते हैं | इन files को modify नहीं करता |
npm ci कब use करें?
CI/CD pipelines, automated testing और deployment में npm ci उपयोगी है, जब project में committed lockfile मौजूद हो।
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:
2.4.72— Major version4— Minor version7— 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:
{
"dependencies": {
"package-a": "^2.4.7",
"package-b": "~2.4.7"
}
}| Range | सामान्य अर्थ |
|---|---|
^2.4.7 | 2.x line में compatible versions, 3.0.0 से कम |
~2.4.7 | 2.4.x patch updates, 2.5.0 से कम |
2.4.7 | Exact version |
>=2.4.7 | Version 2.4.7 या उससे ऊपर |
Caret ranges में 0.x versions के लिए special rules होते हैं। इसलिए शुरुआती version numbers पर सिर्फ caret देखकर assumptions न बनाएँ।
Reference: npm SemVer documentation.
Packages Update कैसे करें?#
पहले outdated packages check करें:
npm outdatedइसके बाद eligible dependency updates के लिए:
npm updateयह command declared version ranges के अनुसार packages update करती है। यह जरूरी नहीं कि हर package को latest major version पर ले जाए।
अगर major version upgrade करना है, तो पहले package changelog और migration guide पढ़ें।
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 देखने के लिए:
npm view express versionPackage की available versions देखने के लिए:
npm view express versionsPackage metadata देखने के लिए:
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:
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
npm install eslintयह package current project में install करता है और सामान्यतः project की node_modules directory में उपलब्ध होता है।
Project-specific dependencies के लिए local installation preferred है।
Global Installation
npm install -g package-nameGlobal 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 करने के लिए:
npm auditFix options देखने के लिए:
npm audit fixnpm 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 करने के लिए:
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.jsonreview करें। - दोनों 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 या
npxuse करें।
npm Best Practices#
- Project की
package.jsonऔरpackage-lock.jsonको version control में रखें। - Team projects और CI/CD में जरूरत के अनुसार
npm ciuse करें। - Dependency updates के बाद tests और build run करें।
- Unknown packages install करने से पहले review करें।
- Production और development dependencies सही classify करें।
npm auditfindings को review करें।- बिना समझे
--forceऔर--legacy-peer-depsका इस्तेमाल न करें। - Global installation केवल तब करें जब उसकी वास्तविक जरूरत हो।
- Secrets और private registry tokens को public repository में commit न करें।
- Package scripts चलाने से पहले समझें कि वे कौन-से commands execute करते हैं।
npm Quick Reference#
| Task | Command |
|---|---|
| npm version | npm --version |
| Project initialize | npm init -y |
| Install dependencies | npm install |
| Install package | npm install express |
| Install dev dependency | npm install -D eslint |
| Remove package | npm uninstall express |
| Clean installation | npm ci |
| List direct dependencies | npm list --depth=0 |
| Find outdated packages | npm outdated |
| Update eligible packages | npm update |
| Run start script | npm start |
| Run custom script | npm run dev |
| View package version | npm view express version |
| Check advisories | npm audit |
| Verify cache | npm cache verify |
| Run package executable | npx package-name |
Knowledge Check#
- npm का मुख्य काम क्या है?
- Node.js और npm में क्या अंतर है?
package.jsonऔरpackage-lock.jsonक्या store करती हैं?dependenciesऔरdevDependenciesमें क्या अंतर है?npm installऔरnpm ciकब use किए जाते हैं?- SemVer में Major, Minor और Patch का क्या अर्थ है?
^और~version ranges में क्या अंतर है?npm outdatedक्या करता है?npxका उपयोग कब किया जाता है?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 रखें.