Scoped Packages Domain level Packages

In NPM’s package management system, there is a scoped Packages mechanism for domain-level package management by grouping packages under a namespace named @scope/package. This is how we see packages like @angular/core, @angular/ Router, and @r/horse. So how do you create and manage such domain-level packages? Steps steps go~

Set the scope in the NPM project

NPM automatically creates a scope with the same name as the user name when the user registers an account. After that, the user can also create other ORGs as scopes. Here’s an example of a user-specific scope:

$scoped -- shockw4ver $scoped -- shockw4ver $scoped -- shockw4verCopy the code

Then press Enter to initialize it (or add the -y parameter ~), or you can fill in the information nicely for easy review

The operation scope

After successful initialization, the name value in package.json file is @shockw4ver/ scoped-package-demo, that is, the package is scoped-Package-demo in the shockw4ver namespace.

If you want to keep developing NPM packages within this scope, you can run the global NPM configuration command to do so:

$ npm config set scope shockw4ver
Copy the code

Run NPM config list to see:

; Cli configs metrics - registry = "https://registry.npmjs.org/" scope = "@ shockw4ver" the user-agent = "NPM / 5.5.1 node/v8.9.3 darwin x64" ; userconfig /Users/zhangyongchao/.npmrc //registry.npm.taobao.org/:always-auth = false //registry.npm.taobao.org/:email =  "[email protected]" //registry.npm.taobao.org/:username = "shockw4ver" @asambojur:registry = "https://registry.npmjs.org/" registry = "https://registry.npmjs.org/" ; node bin location = /usr/local/bin/node ; cwd = /Users/zhangyongchao/Desktop/Samuel/demos ; HOME = /Users/zhangyongchao ; "npm config ls -l" to show all defaults.Copy the code

This has been written to.npmrc as a global NPM configuration.

Release scoped package

The publication of scoped Package is almost the same as the publication of a normal package, with the exception of setting permissions:

$ npm adduser Username: shockw4ver Password: Email: (this IS public) [email protected] Logged in as shockw4ver to scope @asambojur on https://registry.npmjs.org/. $ npm Publish --access=public # publish --access=public # publish --access=public # publish --access=publicCopy the code

Wait a moment:

+ @ shockw4ver/[email protected]Copy the code

That’s a success, heh heh

Cooperative development

Collaborative development is no different from a regular package. Owner simply adds collaborators to Collaborator. Of course, you should synchronize with a version management tool first to ensure that the release number is not repeated with each release.

Private Modules

Private Modules is a Private module service provided by NPM, similar to Github’s Private Repository. You can use this service for a fee when you want to use NPM to manage code in your namespace that you don’t want to expose. Official description:

When you sign up for private modules as an individual user, your scope is your username. If you create an npm Org, the orgname can be the scope. Orgs can be free or paid. Using a public Org is the only free way to use scopes in package names

That is, a personal username or Org can be your scope, while Orgs can be paid or free, where free Org is the only way to use scope through the package name