JS Files चलाना
आप code को एक .js file में save करते हैं और इसे चलाने के लिए node command को देते हैं।
In this page:
Syntax
node file.js
node file.js arg1 arg2 // read with process.argv
JS Files चलाना
node file.js से एक file चलाएँ। Command-line arguments process.argv में available हैं, जहाँ पहली दो entries node path और script path हैं। Exit code process.exit(code) या process.exitCode से set होता है।
Note:
सिर्फ अपने arguments पाने के लिए process.argv.slice(2) इस्तेमाल करें।
उदाहरण: Running JS files
const args = process.argv.slice(2);
console.log("Script args:", args);
console.log("Args length:", args.length);
console.log("Is main module:", require.main === module);
console.log("File name ends with app.js:", __filename.endsWith("app.js"));
// Output:
// Script args: []
// Args length: 0
// Is main module: true
// File name ends with app.js: true
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- current directory के relative .js file path भूल जाना
- पहला argument उम्मीद करते हुए argv[0] पढ़ना
- async work खत्म होने से पहले process.exit call करना
चैप्टर सारांश
- node file.js एक script चलाता है
- process.argv arguments hold करता है
- slice(2) user arguments देता है
- process.exitCode exit status set करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: