npm Scoped Packages

Rumman Ansari   Software Engineer   0000-00-00 00:00:00   63  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Scoped Packages

Scope is like a namespace for a package. A user or an organization can opt to have all their packages published under one scope.

Scope folder is referred as "@" followed by the name of the scope like username/an organization.

@scopename/packagename

With over 500,000 packages already registered in the npm registry, scoped packages will allow users to worry less about a package name being already taken.

Note: Scoped modules, require a version of npm greater than 2.7.0.

Using a Scoped Package

Installing a scoped package:

npm install @scopename/packagename –save

Configuring package.json:

{
  "dependencies": {
  "@scopename/packagename": "^1.0.0"
  }
}

Using in a require:

var mypackage = require("@scopename/packagename")
Initializing a Scoped package

While creating a scoped package using npm init, add the created scope as an option to that command.

npm init --scope=scopename

In the package.json file, refer the package name as:

{
  "name": "@scopename/packagename"
}
Publishing a Scoped Package
  • By default, the scoped packages are private. Publishing and downloading private modules requires a paid subscription to npm registry.
  • However, public scoped modules don't require a paid subscription. They are free. To publish a public scoped module, set the access option while publishing it.
npm publish --access=public
Private Packages in npm

All scoped packages in npm are published as private by default. These packages are in private scope with access restricted to few users, who can collaborate to build/use the package.

To publish a package as private:

>npm publish 
//OR
>npm publish --access restricted

These packages can be used along with the public packages in the same project without any issues.

To change a package from a public to private scope:

>npm access restricted <package_name>

This will ensure that the package is removed from listings on the site.

MCQ Available

There are 5 MCQs available for this topic.

5 MCQ