Summary: in this tutorial, you will learn how to create a new package and publish it to the npm registry.
Publishing your package to npm
First, you need to create a new account by visiting the npmjs.com’s sign up page. The important information is the username, password, and public email. You’ll need this information when you publish your package.
Then, you create a new package e.g., galaxy-lib
. In the galaxy-lib
directory, you run the npm init --yes
command to create the package.json
file:
npm init --yes
Code language: Shell Session (shell)
After having the package.json
file, you can create a new module called index.js
that has a simple function which returns the number of galaxies:
module.exports.count = function () {
return 1000000;
}
Code language: JavaScript (javascript)
Third, to publish the package to npm, from the terminal, you use the npm login
command:
npm login
Code language: Shell Session (shell)
It will prompt you to enter the following information: username
, password
, and email
.
Finally, you run the npm publish
command to publish the galaxy-lib
package to the npm registry.
npm publish
Code language: Shell Session (shell)
If you received an error after running the npm publish
command, it’s likely that the package that you were publishing has a name that already exists. In this case, you need to change the package name in the package.json
file to something unique.
Using the published package
To use the galaxy-lib
package, you create a new Node project called galaxy-demo
and run the npm init
command:
npm init --yes
Code language: Shell Session (shell)
To install the galaxy-lib
package that you have published, you run the npm install
command:
npm install galaxy-lib
Code language: Shell Session (shell)
The following creates the app.js
and use the galaxy-lib
package:
const galaxy = require('galaxy-lib');
console.log(galaxy.count());
Code language: JavaScript (javascript)
Finally, run the app.js
program using the following command:
node app.js
Code language: Shell Session (shell)
Output:
1000000
Code language: Shell Session (shell)
Unpublishing a your package
After you publish a package to the npm registry, others may be already using it. Therefore, unpublishing a package from the npm registry is generally considered bad behavior.
If you are sure what you are doing, you can run the npm unpublish
command to unpublish a package:
npm unpublish [<@scope>/]<package_name>[@<version>]
Code language: HTML, XML (xml)
The npm unpublish
command removes the version of the package from the npm registry, deletes its entry, and removes the tarball.
To encourage other developers to not use a package or update the versions, you should use the npm deprecate
command instead:
npm deprecate <package_name>[@<version>] <deprecation_message>
Code language: HTML, XML (xml)
This npm deprecate
command updates the npm registry for a package and issues a deprecation warning to anyone who attempts to install it
Summary
- Use
npm publish
command to publish your package to the npm registry. - Avoid using the npm unpublish command to remove the published package from the npm registry.
- Use
npm deprecate
command to deprecate a published package.