Using Scoped NPM Packages
javascript·@ghasemkiani·
0.000 HBDUsing Scoped NPM Packages
[NPM](https://npmjs.com/) is the most widely used package manager for [NodeJS](https://nodejs.org/). It has more than 600,000 packages with hundreds of millions of downloads per day. NodeJS uses directory hierarchy for finding modules. Specifically, it searches `node_modules` subdirectory in the current folder or its parent folders for the required modules. Naming packages is important for several reasons. Firstly, your package name must be unique. Secondly, your chosen name should be expressive for your package. Now this leads to a lot of problems. Consider, for example, that you want to create a package named `dom` for working with the document object model. Chances are someone has already put a package with that name on `npm`. On the other hand, let's assume that you need a package for working with DOM. Can you offhandedly run `npm install dom` and expect to find what you want? As you can see, a simple name cannot satisfactorily define a package. What we need is some sort of namespace. [Scoped packages](https://docs.npmjs.com/misc/scope) give you this functionality. Instead of `dom`, you can name your package `@yourname/dom`. This prevent name clashes and defines your `dom` package more thoroughly. In order to create a scoped package, add `--scope yourname` to `npm init` command: ``` npm init --scope=yourname ``` This adds a line like the following to your `package.json` file: ``` { "name": "@yourname/dom" } ``` When requiring scoped packages, you must use the complete name: ``` const dom = require("@yourname/dom"); ``` The same applies to including the package as a dependency in `package.json`: ``` { "dependencies": { "@yourname/dom": "^1.0.0" } } ``` In npm, scoped packages are private by default (and they require paid membership in npmjs.com). If you want to publish your scoped package publicly, you should set the public access option when publishing it (only the first time): ``` npm publish --access=public ``` When installing scoped packages, you must use the complete name: ``` npm install "@yourname/dom" ``` After installing a scoped package, `npm` creates a subdirectory `@yourname` under `node_modules` and puts your package in that folder: ``` node_modules │ └──@yourname │ └──dom │ ├──package.json └──index.js ``` Namespacing is very useful for differentiating packages. Scoped NodeJS packages are already being used in many popular JavaScript projects.
👍 ghasemkiani, mshahabi, nanocheeze, cybershrapnel, xaunya, xtdevelopment, threadripper, shariaislam, naeemahmedd, zf90, abdulmanan, sam1210, bestbiz, dxdei, hopsy, thepreacher, danielvd, johnwjr7, blazing, hafiz34, satyamsharma, nataliemk, pepskaram, hasib56789, haji, chaidirchai, iskandar17, angela.ghkh, semirs, mazyar, life.story, rohit12123, sushovon002, nayim533, scipio, divaa, nimik, zpzn, hsa61, fel1xw, cosmicboy123, orchidea17, chandrayunita, kikee, kangsukin, arfa07, albob, albanna, ramadhan167, apam, stupidamerican, sinilga, princepr, karnain, abysoyjoy, asrilmaulana, aldialbest, davidfar, sempurna, master11641, nrsplastic, fauzan11, binam, aisamuddin, ipoelkip, lmmover, mhosann, kabil, mishrpx27, himanshusindhal, coinstantview, arman.ithm, phasewalker,