Node.js: Publishing an npm package
How to publish a Node.js module on npm#
Before you can publish your Node.js module on npm, you have to create one. If you don't know how to create one, read "Node.js: Writing modules".
Assuming you are done implementing the functionality of your module, create these two additional files in the module directory.
package.json
- The module meta data JSON file. Add thename
andversion
properties at the minimum. If you want your package to be used by others, make sure to include thelicense
property. Refer to https://docs.npmjs.com/creating-a-package-json-file for the possible properties in thepackage.json
file.README.md
- Serves as a basic documentation for your module. Use Markdown for formatting the content.
You can run npm init
in the project directory to interactively generate the package.json
.
$ npm init
Make sure the contents of the package.json
are specified correctly and accuarately.
Then, log in to your npm account:
$ npm login
Username: hacksparrow
Password:
Email: (this IS public) captain@hacksparrow.com
Logged in as hacksparrow on https://registry.npmjs.org/.
Run npm publish
to publish your Node.js module as an npm package:
$ npm publish
npm notice
npm notice 📦 my-module-abc@1.0.0
npm notice === Tarball Contents ===
npm notice 209B package.json
npm notice 54B index.js
npm notice === Tarball Details ===
npm notice name: my-module-abc
npm notice version: 1.0.0
npm notice package size: 309 B
npm notice unpacked size: 263 B
npm notice shasum: dbe99d75071b244efcdd8243e90fe51635de759a
npm notice integrity: sha512-YluHzjIsUCTIN[...]3LCjKahwNhVwg==
npm notice total files: 2
npm notice
+ my-module-abc@1.0.0
Version 1.0.0
of my-module-abc
has been published on npm.
If you wish to include repository information about your module, add the information in its repository
property as shown below.
"repository": {
"type": "git",
"url": "https://github.com/hacksparrow/my-module-abc.git"
}
npm init
would have already added the repository details. No further actions are required.Then initialize the project directory as a git repository and add a remote named origin
pointing to the location of your remote repository.
$ git init
$ git remote add origin git@github.com:hacksparrow/my-module-abc.git
Summary#
Creating an npm package is just a matter of adding the package.json
file in the Node.js module directory. The package can then be published using the npm publish
command.