Publishing a package to the npm registry so that it can be installed by name and can be used by many other developers around the globe.
In this article we cover publishing a demo package used for very basic functionality
Prerequisites
You should know few things before proceeding ahead
- Install Node.js and NPM on your system
- Have basic knowledge of [JavaScript]
- Have an account on npm
Before we start, make sure you have an account on npm, if not make one now.
Creating a package
First create a folder, in this case demo-hello-world-pkg and move to the newly created directory
cd demo-hello-world-pkg
Now initialize your package
npm init -y
Well, this package is already installed in the npm registry, with mentioned details in package.json
file, So you just modify it with your own.
If you are not publish it on a github repository, then skip the highlighted once. otherwise replace it with your own
{
"name": "demo-hello-world-pkg",
"version": "1.0.0",
"description": "\" Demo package - Just for test\"",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["test","demo"],
"repository": { "type": "git", "url": "https://github.com/rahul3v/demo-hello-world-pkg.git" }, "bugs": { "url": "https://github.com/rahul3v/demo-hello-world-pkg/issues" }, "author": "rahul",
"license": "ISC",
"homepage": "https://github.com/rahul3v/demo-hello-world-pkg"
}
Now create a index.js
file in the same directory
// To print a massage on console
exports.printMsg = function() {
console.log("This is a demo package");
}
// To return 'Hello World' string
exports.hello = function() {
return "Hello World";
}
// To return current date
exports.today = function() {
return Date();
}
Before publishing
Now we have to login to our npm account and for that we have to use
npm login
Now it will ask for your NPM account Username
, Password
and a public email of your account after login.
When you enter your Password
it will not show on screen, so don't panic, Just enter your correct account password and press enter
Username:xxxxxx
Password:
Email: (this IS public) xxxxx@xxxx.xxx
Logged in as xxxxxx on https://registry.npmjs.org/.
Publishing
We have two ways for that
- unscoped - To publish unique package in the entire npm registry
- scoped - To publish unique or similar package associated with your account or your Organization
unscoped
Remember in unscoped, package name must be unique in the entire npm registry (like your username) If you want the use same name as already their then use scoped publishing
For unique package - search the package name on npm registry, if their is an exact match then it will show else you found a unique package name, now you can use that
Publishing unscoped is very easy just type in your package root directory
npm publish
scoped
Publishing packages associated with your or your Organization's account
You have to made a little change in package.json
file
- For a user-scoped package, replace my-scopeName with your userName
- For a Org-scoped package, replace my-scopeName with your OrgName
{
"name": "@my-scopeName/demo-hello-world-pkg",
...
}
Publishing scoped is also very easy now, just type in your package root directory
npm publish
By default, scoped packages are published with private visibility. To publish a scoped package with public visibility, use
npm publish --access public
See your published package
Now visit your account to see your published package, replace my-username with your username
https://npmjs.com/~my-username
Now you can create a project in NodeJs and try Using your npm package
Now you can logout your account
npm logout
Updating Package
Well for updating, first make your changes and then update a bit in package.json
file with updating next version
number like
{
"name": "demo-hello-world-pkg",
"version": "1.0.1",
...
}
Before publishing you must login into your account. Now you can follow the publishing same as before
Conclusion
In this article we have learned how to publish our own package
Now you can create one and share among your friends