npm Scripts को समझना
In this page:
{
"scripts": {
"scriptName": "command"
}
}
npm run scriptName
scripts Section क्या करता है
package.json का scripts section common commands के लिए named shortcuts define करता है, जैसे "start": "vite" या "build": "vite build", ताकि आप पूरा underlying command type करने के बजाय npm run build type करें।
उदाहरण: Listing and Running npm Scripts
$ npm run
Lifecycle scripts included in my-app:
dev
vite
build
vite build
preview
vite preview
$ npm run build
vite v5.0.0 building for production...
✓ 34 modules transformed.
dist/index.html
dist/assets/index-4f3a2b1c.js
⚠️ Run this in your own terminal or Node.js environment.
Common Scripts जो आप Use करेंगे
लगभग हर React project एक dev/start script (local dev server run करता है), एक build script (production bundle बनाता है), और अक्सर एक test script (test suite run करता है) के साथ ship होता है।
उदाहरण: Common Scripts You'll Use
npm run dev # local dev server
npm run build # production bundle
npm test # run test suite
⚠️ Run this in your own terminal or Node.js environment.
अपना खुद का Script जोड़ना
आप scripts section में खुद एक custom entry जोड़ सकते हैं, जैसे "lint": "eslint src", और फिर इसे npm run lint से वैसे ही run कर सकते हैं।
उदाहरण: Adding Your Own Script
// package.json
{
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint src"
}
}
$ npm run lint
> [email protected] lint
> eslint src
src/App.jsx
12:7 warning 'count' is assigned a value but never used no-unused-vars
⚠️ Run this in your own terminal or Node.js environment.
runkeyword के बिना एक custom script run करना (start/testके लिए काम करता है लेकिन custom names के लिए नहीं)।- कोई command मौजूद है यह मान लेने से पहले package.json का
scriptssection check न करना। - यह realize किए बिना कि यह default behavior बदल देता है एक built-in script name (जैसे
start) को overwrite करना।
- npm scripts package.json के
scriptssection में defined shortcuts हैं। npm startऔरnpm testको शब्दrunके बिना run किया जा सकता है; custom scripts कोnpm run <name>चाहिए।- Common React scripts में start (dev server), build (production bundle), और test शामिल हैं।
- आप linting या formatting जैसे tasks के लिए अपने खुद के custom scripts जोड़ सकते हैं।
Applicable नहीं -- npm scripts terminal में run होते हैं, browser में नहीं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: