![Koda School](/_next/image?url=%2Fimages%2Flogo.png&w=96&q=75)
KodaSchool
Learn To Code
How to create and publish an NPM package
Learn how to create and publish your own NPM package with this step-by-step guide. Share your code with the community and promote collaboration with ease.
topics
Introduction
NPM, which stands for Node package manager, is one of the largest software registries on the internet. It houses over a million packages. In this guide, you will learn how to create and publish your own npm package, allowing others to download and use it.
Prerequisites
Before we begin, you need to have the following ready:
- Node.js: If you haven't already, install it. NPM usually comes pre-installed with Node.
- Basic knowledge of JavaScript
What is NPM package
NPM is a collection of files, mostly written in JavaScript, that provides specific functionality. This can be a library e.g. JQuery , utility or a tool that will help developers integrate into their code to fulfil certain functionalities.
Benefits of publishing an NPM package
NPM comes along with a lot of benefits, including:
- Code reusability. This will allow you to share your code with the community of developers and reusing existing packages.
- Version control. You can easily manage the different versions of the packages you publish.
- Collaboration. You will be able to collaborate or use other developer’s packages in your package.
Setting up your development environment.
1. Choose a unique package name.
Make sure it hasn't been used already. You can search the NPM registry to verify its availability.
2. Create a new project folder for your package and navigate into it.
Run npm init
to create a package.json
file. Provide relevant information such as package name, version, description, entry point, and more.
After running the npm init
you will have a package.json
file which will contain something similar to this:
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2Fbe7e32f18357927a0a28b98e3238937b798c03f2-1456x520.png%3Fw%3D1456%26auto%3Dformat&w=3840&q=75)
You will be prompted to answer some few setup questions. Answer them accordingly but if you would like to get default values you can skip by pressing enter or run the following command npm init –yes
We can then modify some few things in our package.json
file, the description, the author.
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2F95eb27616e51e71dadf9a0a459aad12614a6ef71-1456x520.png%3Fw%3D1456%26auto%3Dformat&w=3840&q=75)
Write your code
The next thing to do is to create the code functionality for our package.
We then need to create the necessary files that will be loaded when other applications would need your modules. You should ensure your entry point matches the one specified in package.json
.
In my case we can create a file, index.js
and add our code inside.
function HelloNpm(){
return ‘Hello, this is my first npm package’
};
module.exports = HelloNpm;
After writing your code, you then need to export it for other users who would download it.
Creating a README file
It is usually the best practice to include a readme file for your package to make it easier for others to understand what your package does.
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2F65072b427647a15f7b69ec5ebfc050093bfca006-1456x520.png%3Fw%3D1456%26auto%3Dformat&w=3840&q=75)
Publishing Your Package
That is basically all we need to publish our package to npm.
You need to have an account in the npm registry, if not create one where you can publish your package.
Run the npm login
and enter your credentials to login.
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2Fe931c9d81cf6de9ee05464959fc3ae6219685ce2-811x204.png%3Fw%3D811%26auto%3Dformat&w=3840&q=75)
Now that we’re set up, you can publish your package with this command:
npm publish
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2F441fe6783171602c35398a4c6d0cb149734dc8b3-1060x441.png%3Fw%3D1060%26auto%3Dformat&w=3840&q=75)
By default, NPM publishes to the public registry. If you want to publish a scoped package with public visibility, use:
npm publish --access public
And that's it! Your package is now available for others to use.
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2Ffff67714cdfe3ff12bb680437d237a228b12a987-1596x825.png%3Fw%3D1596%26auto%3Dformat&w=3840&q=75)
Downloading your package manager
Now that your package is available on the registry, others or even you can download and use it by running the following command:
npm install <your-package-name>
npm install kodashcoolnpmtutorial
![Image](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fbu96b58t%2Fproduction%2F4fc42e99c9a113f6f761b5d78b86da54edaa6b17-1071x93.png%3Fw%3D1071%26auto%3Dformat&w=3840&q=75)
This will install your package including all the dependencies in it. That’s it. You can go beyond this.
Conclusion
We’ve done all it takes to set up, write our package and publish it. In a simple way, all we have done is initialized our package, written our functionality and finally published it.
share
Software Engineer | Product Manager | Technical writer |