Publishing a package on the Node Package Manager (NPM) allows other developers to easily install and use your code. Here’s a step-by-step guide on how to publish your package on NPM:
1. Create your Node.js package:
- Create a directory for your package and navigate to it in your terminal.
- Inside the directory, create a
package.json
file. You can do this manually or by runningnpm init
and following the prompts to generate thepackage.json
file.
2. Add your code:
- Place your code and any necessary files within the package directory. Ensure that your code is organized and functional.
3. Add a README file:
- Create a README file (e.g.,
README.md
) in your package directory to provide documentation and information about your package.
4. Prepare the package for publishing:
- Make sure your
package.json
file contains the necessary metadata such asname
,version
,description
,author
, andlicense
. Fill in these details appropriately.
5. Set up an NPM account:
- If you don’t have an NPM account, you’ll need to create one by running
npm adduser
in your terminal and following the prompts to register.
6. Log in to your NPM account:
- Use
npm login
to log in to your NPM account using your username, password, and email.
7. Publish your package:
- Run
npm publish
in your package directory. This command will package your code, upload it to the NPM registry, and make it publicly available.
8. Update your package (optional):
- If you make changes to your package, increment the version number in your
package.json
file. Follow semantic versioning (SemVer) guidelines when updating version numbers (e.g., 1.0.0 to 1.0.1).
9. Publish updates (optional):
- After updating your package, use
npm publish
again to publish the new version.
10. Managing access (optional): – You can manage who has access to your package by configuring package access settings on NPM. You can set it as public or private, and you can control who can publish updates.
11. Verify your package: – You can verify that your package has been published by visiting the NPM website (https://www.npmjs.com/package/your-package-name) or by searching for it using npm search your-package-name
.
That’s it! Your package should now be published on NPM and accessible to other developers worldwide. Remember to keep your package up to date and maintain good documentation to make it easier for others to use and contribute to your code.
How to publish your own package
Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages.
Okay, so let’s start!
- npm init:
When you are in your desired directory, open the CLI, and use this command:
npm init
A text utility appears and you are asked to enter a few details to create a basic package.json file such as package name, version, description, author, etc.
Enter the details of the package as you please. The defaults will be mentioned in parentheses like this →
package name: (test-package) version: (1.0.0) description: This is a test package . . .
Just hit Enter if you want the default options.
A basic package.json will be created looking something like this :
{ "name": "test-package", "version": "1.0.0", "description": "This is a test package", "main": "app.js", "author": "ashramtech", "license": "ISC" }
Obviously, you can change the package.json file later if you want.
- Source
Now, you need to prepare the source code. If the code is not too big, it is typically just write in the main file(app.js here). Else, conventionally a src directory is used where your abstract code is stored in several files.
Remember to export the code using module.exports
- Test
You now need to thoroughly test your code before publishing. This is how you, as a developer, confirm that your package can actually be used.
- Publish
If this is your first time, you probably won’t be logged in with your npm account. If you don’t have an account, go to npmjs.com and create your account.
Now, in your terminal, log in with your npm account.
npm login
And now to publish your package,
npm publish
And you’re done!
You can now check your published project at www.npmjs.com/<username>/<pkg-name>.
The details regarding the package such as version, author name, description, etc. are extracted from the package.json file you created earlier.
Conclusion
Now you’ve got a basic idea about what npm is and how you can publish your own package to the npm registry. If you want to know more about npm and its basic commands please follow this article An Introduction to the npm : An Absolute Beginner’s Guide