Creating And Publishing NPM Organization Scoped Package

NPM Organization Scoped Package

In this article, you will explore what are scopes regarding npm packages for NPM organization scoped package and what scoped packages are – focusing on how to create, publish and install them for your project.

Whenever you’re creating a package, one of the mandatory fields is that an npm package must have a name that should be unique and that will represent the package when we’ll publish it to the npm registry.

We can give the name of the package as: “sample-package.”
However, if the package is already published with a specific name we’re giving, it is impossible to publish the package with that name.

Therefore, we can rename our package and come up with a new name, but there is another technique we should be aware of, so let’s talk about it now.

NPM Organization Scoped Packages

Scoped Packages is a method to group related npm packages and connect them by a specified “scope” that acts like a namespace.

This kind of package provides us few benefits:

  • We don’t need to worry about whether the package exists as long as we haven’t previously published a Scoped Package with that name.
  • Since only scope members can publish within our scope, we don’t need to be concerned about this happening.
  • Instead of switching to a private npm registry before installing or publishing a package, we can just set the private registry for the scope in advance and once.

One of the major advantages of utilizing an Organization is that you may publish packages under a shared namespace called Organization scope, publicly or privately.

However, an Organization’s scope makes managing package access for multiple packages and users much easier.

So, here you will learn how to publish an Organization scoped package, publicly or privately.

Example of NPM Organization Scoped Package

1. Create a Simple Calculator project

  • Create a folder called ‘Npm Package.’
  • Under this folder, create package.json – you can create package.json manually or let ‘npm init’ create it automatically.
  • Package.json file will look like this:
	
{
  "name": "simple-calc-package",
  "version": "1.0.0",
  "description": "This is a node package which contains functions like Addition,Subtraction,Multiplication.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Manisha Kumari",
  "license": "ISC"
}
  • Create a file named index.js in the same folder and write all the package logic in that file.
  • For example, I have the following JavaScript for the package named ‘simple-calc-package’ .
	
function addition(a,b){
	return a + b;
}
function subtraction(a,b){
	return a - b;
}
function multiplication(a,b){
	return a * b;
}


module.exports = {
	addition: addition,
	subtraction: subtraction,
	multiplication: multiplication
}

2. Create an NPM Account and Org

  • Go to https://www.npmjs.com.
  • Create an account there. You can also convert the username (account name) as an Org name or create a new Org. So, I’m creating a new Organization named ‘bosctechlab’ (we can use it like @bosctechlab/). You can invite other developers also, who can access this package. But for now, I’m the sole member of this Org.

3. On the command line, run the command (To create an Organization Scoped package):

	
npm init --scope=

To verify the package uses your Organization Scope, open the package.json file and check that the name is @your_org_name/.

For example –

	@bosctechlab/simple-calc-package

4. log in to NPM through the terminal

Run the command:

	npm adduser

When prompted, please enter the account Username, Password, and Email (It will send an OTP to the email and also enter them)

5. Publish a private Organization Scoped Package
Every Scoped Package is published as private by default. So, if you run npm publish, it will fail unless you pass the access flag.

On the npm website, private packages will display private underneath the package name.

6. Publish a private Organization Scoped Package
Run the command to make an Organization Scoped Package public:

			
npm publish --access public

That’s all; we are done adding the package, which is ready to use and download. You can confirm it by visiting your npm account.

Inside your org, you’ll see the package like :

Conclusion

To verify if our package is working fine, you can install the package using the following:

	
npm i @bosctechlab/simple-calc-package

Create a file ‘index.js‘ and import the package and call the function like:

And bingo! You may view the output on the console.

Packages help programmers to do their work faster and will improve collaboration. It is smarter. However, creating and publishing the NPM Organization Scoped Package needs careful planning, attention to detail, and knowledge of package management tools and processes. Thus, with the right tools and resources, it is manageable, which helps you to share the code and collaborate with others in your Organization.

Bosc Tech Labs is a well-known and trustworthy mobile app development company in the USA. We have an expert team in NodejS who helps you build top-notch and user experience apps that helps you to satisfy a client’s needs.


Book your appointment now

Frequently Asked Questions (FAQs)

1. What are npm Scoped Packages?

Npm scopes are the method of groping the related packages together. A scope enables you to create a package with a name that is similar to another package created by another user without causing any issues. Using the scope, you can differentiate public and private packages by including the scope prefix @SCOPE_NAME and the configuration method.

2. What happens when you do npm publish?

Publishing the package to the registry so that the name can install it. Hence, by default, npm will be published to the public registry. Hence, it is overridden by specifying the various default registries or will use the scope in name to merge with the scope-configured registry.

3. How to publish private scope in npm?

To publish a private Scoped Package into an npm registry, you must have an npm Private Modules account. A module that has been published with an npm publish or npm public access restricted will be available in the npm registry with some restrictions.

Get in touch





    Stay up-to-date with our blogs