I just created my first NPM package. It ain't much but it's honest work
Ivan Spoljaric
Posted on July 24, 2020
Yesterday I learned that these NPM packages exist:
- https://www.npmjs.com/package/is-odd
- https://www.npmjs.com/package/is-even
- https://www.npmjs.com/package/is-number
I have to admit that this both surprised and amused me.
I was under the opinion that the average NPM package is a little bit more contrived than this:
// is-odd
return (n % 2) === 1;
or this:
// is-number
if (typeof num === 'number') {
return num - num === 0;
}
But the thing that surprised me the most, is that developers actually use these packages as dependencies on their projects.
is-odd has 500k weekly downloads!
And what makes this whole thing even funnier is that this package has a dependency of it's own, and it is the above stated is-number package.
So the final size of is-odd comes down to cca. 6.5kB.
I don't understand why this package, nor it's sister is-even package, are so popular when it is so easy to "implement" the functionality they offer with vanilla JS (it requires just a single line of code).
But don't get me wrong. I am not trying to be negative.
Who knows why these packages exists and why they became so popular.
For example, one of the reasons might be that the author initially created is-odd because he was practicing how to publish a package to NPM.
But this is just conjecture at this point and it is irrelevant to the remainder of this post and moral of the story :)
I just wanted to explain my motivation before I came to the main topic of the post.
I present to you my very own, and first, published NPM package called linear-array
Here it is: https://www.npmjs.com/package/linear-array
Just to be clear, I am completely aware of the actual uselessness of this package.
But I decided to create it anyway because of the already stated reasons, and more importantly, because I wanted to learn how NPM packages are published.
What it does:
Returns an array filed with linearly increasing numbers, starting from 0 up to the given value - 1 (without offset), or from 1 to the value itself (with offset).
How to use it:
import linearArray from 'linear-array'; // web
OR
const linearArray = require('linear-array'); // server
console.log(linearArray(1)); //=> [0]
console.log(linearArray(3)); //=> [0,1,2]
console.log(linearArray(5)); //=> [0,1,2,3,4]
console.log(linearArray(1, true)); //=> [1]
console.log(linearArray(3, true)); //=> [1,2,3]
console.log(linearArray(5, true)); //=> [1,2,3,4,5]
So it turns out that all of this actually isn't very complicated.
Here's a short workflow on how to do it:
- Think of a package that you could create and a unique name (check on the NPM repository that the name is not already taken)
- Create a local folder with the same name as your future NPM package
- Add the necessary files (index.js, README, LICENSE, and test.js if you want) and fill it with markdown and code
- Run
git init
in your terminal - Push to Github repo with the same name as the local project name
- Run
npm init
in your terminal - Register your profile on https://www.npmjs.com/
- Run
npm login
in your terminal - Run
npm publish
in your terminal
Take a look of the project repo of linear-array if you get stuck somewhere.
That's it.
Thanks for reading this post until the end.
The moral of the story is, it doesn't matter if you think that your idea for an NPM package is a shitty one.
What does matter is the learning journey.
And because it is fun to try new things like this.
P.S.
If you actually find some use for my package and decide to install it on your project, I'm begging you to reconsider and that you don't do actually do it. 😂
Just copy the code directly from the index.js file on the project repo page.
Cheers!
Posted on July 24, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.