How To Publish an NPM PackagePosted on December 8, 2019 | 3 minute read
In essence, publishing an npm package is just one command, but there are some things that you have to take care of before doing that.
Here are the steps I followed before publishing my first package:
- Create a free account on https://www.npmjs.com/.
- Log into the npm CLI by running
- Create a folder for your new package which would normally have the same name.
- Make sure that you ran
npm initand have all the right values filled-in in the
- Carefully choose the name, as that is going to be the name that everyone is going to use to install your package.
- Set the version number using the semantic versioning format. It should look something like this: “v1.2.3”. The first number is the major version and should be incremented every time you deploy a breaking change. The second number is the minor version and should go up with every new non-breaking feature. And, lastly, we have the patch/fix number. Also, at the same time, create a new release in GitHub (or your other VCS) with a matching version. (Read more)
- Add a
typesfield which will point to your types definition file. You don’t have to do this step but with the rapid increase of TypeScript and better IDEs, you are doing the developer a big favor. The types file will be a *.ts file written in TypeScript and describing the types, interfaces, etc. of your package. (Read more)
- Specify the place where your code is hosted by filling in the repository field. (Read more)
- Have a think about how you want to license your package and set the correct license value. If you are not sure, go to this website https://choosealicense.com/ which will make this very easy for you.
- Check your
.gitignorefile and verify that you are not including any personal or unnecessary files in your repository.
- Add an
.npmignorefile which will exclude specific files from your npm package. I personally have added the test files in here, as we don’t need to have them in the package.
- Take your time to write a nice
README.mdfile, where you explain to your future users how to install the package, how to use it, and maybe give some examples. The contents of this file will also appear on this website.
- Now you’re almost ready to publish, but before you do so, run
npm pack, which will generate a
*.tgzfile containing all the files exactly how they will end up in your npm package. This will let you double-check that everything has been set up correctly and you are going to publish the right thing.
- Just before publishing, you are going to run a quick test locally. Create a new folder, initialize npm (
npm init) and install your package with
npm install -S ./path/to/your/package. This will install the package from your local directory and you can try to use it as if it was already published.
- Assuming that you have done all the steps above and it all worked as expected, you can now publish your package with
Congratulations, you now have a brand new npm package.
You can see your package on npm like so: https://www.npmjs.com/package/inline-webassembly