…in my case was easy as ABC.

What
I’ve never published a package to NPM but I have always wanted to try it. Why? The primary reason is to have some idea how one would go about authoring a package. The secondary reason is because all the cool kids do it.
I was working with a simple internal project and saw the opportunity to extract one core piece of it to its own package. The script is almost as simple as it gets; given some input it returns a string.
My initial thoughts about the success rate for this task were neutral, but, being a Finn, they were leaning towards the negative side.
Turns out, any negative thoughts I had were totally wrong.
Testing the package
First things first, let’s have a look the package.json
file.
1{2 "name": "digistories-generator",3 "version": "1.0.0",4 "src": "src/index.js",5 "main": "dist/index.js",6 "devDependencies": {7 "microbundle": "^0.11.0"8 }9}
With some basic information omitted, this is the entire package.json
file of my package.
The name
must be unique across the npm-world and main
should point to your index javascript file.
You can (and should!) test your script before publishing it. There are a few ways of testing locally, but I chose to use the link
command found in npm.
1cd <your_package_root_dir>/2npm link
What this does is it installs this local package to your global space. After this command has been executed successfully you can use the link
command again but this time in a different project where you will test it.
1cd <some_other_node_project>/2npm link <package_name>
The package name is the one you defined in the package.json
.
Now you can just
1import yourcode from "package_name";2// use your code
This of course depends on your code and what it exports and all the other good stuff. 😏
Now that we have the custom package imported it can be used as you normally would any other package that you had installed with npm install
or yarn add
.
The really cool thing about this setup is the fact that when I did changes to the script and built it (using yarn microbundle
) the app that I used for testing spotted the change and hot-reloaded itself. 🤩
Publishing
Once I was fairly sure that the package would work as I wanted it and it could be used after installing, it was time to publish it. The publishing part was the easiest of the whole process.
After creating an account to npmjs.com and logging in with npm login
it was just a matter of calling npm publish
.
Then I unlinked
the local package with
1cd <your_package_root_dir>/2npm unlink
Now the package is published and could be installed with npm install
or yarn add
. Success!
Conclusion
The time it took to find the right commands and test and publish the package was easily under 60 minutes. Given that this was my first time doing this, that’s a really good time.
There’s a chance of a possible maybe that I’ll do this again!
Further reading
I used these two articles as my sources in this process:
- Node.js — How to test your new NPM module without publishing it every 5 minutes
- How to Build and Publish an npm Package
A big thanks to both of the authors!