← Back to Node.js Course | Chapter 3: npm & Packages | Lesson 6 of 7

Packages बनाना

अपना खुद का package बनाने का मतलब है एक module लिखना, इसे package.json में describe करना, और publish करना।

In this page:

  1. Packages बनाना
Syntax
javascript
// index.js
function functionName() {
  // body
}
module.exports = { functionName };

// package.json: "name", "version", "main": "index.js"

Packages बनाना

एक package को name और version वाले एक package.json और module.exports के जरिए exported एक entry file की ज़रूरत है। npm link आपको इसे locally test करने देता है, और npm publish इसे registry पर upload करता है। @you/tool जैसे Scoped names name clashes से बचते हैं।

Note: क्या publish हो इसे control करने के लिए files field या .npmignore इस्तेमाल करें।

उदाहरण: Creating packages

javascript
// index.js of a tiny package
function slugify(text) {
  return text.toLowerCase().trim().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
module.exports = { slugify };

// usage from another file
console.log(module.exports.slugify("  Hello, Node World!  "));

// Output:
// hello-node-world

⚠️ 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. #}
आम गलतियां
  1. accidentally secrets या tests publish करना
  2. publish करने से पहले version bump करना भूल जाना
  3. public registry पर naming clashes
चैप्टर सारांश
  • module.exports से export करें
  • package.json को name और version चाहिए
  • npm link locally test करता है
  • npm publish इसे release करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.