DEV Community

Ivan Spoljaric
Ivan Spoljaric

Posted on • Updated on

I just created my first NPM package. It ain't much but it's honest work

Yesterday I learned that these NPM packages exist:

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;
Enter fullscreen mode Exit fullscreen mode

or this:

// is-number

if (typeof num === 'number') {
    return num - num === 0;
  }
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

So it turns out that all of this actually isn't very complicated.

Honest work meme

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!

Top comments (17)

Collapse
 
fatardy profile image
Aradhya Alamuru

Haha I had a very similar post planned - you beat me to it!

The is-even package has a dependency - is-odd lolol

var isOdd = require('is-odd');
module.exports = function isEven(i) {
  return !isOdd(i);
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ispoljari profile image
Ivan Spoljaric

Yeah, but that is some genius level stuff :)

Collapse
 
fatardy profile image
Aradhya Alamuru

Lol the only thing left is to get it into the react core or something

Collapse
 
nomade55 profile image
Lucas G. Terracino

I was surprised with the is-odd and is-even download amount. But you know, sometimes you just need to know wether it is or not.

Collapse
 
konradlinkowski profile image
Konrad Linkowski

Those useless packages have so many downloads, because some basic,useful package use it. And for example webpack use this useful package. So now everyone installing webpack or something that uses webpack (basically any top is framework) is counted as download.

Collapse
 
intrnl profile image
intrnl

would be more appropriate to say that webpack doesn't actually use is-odd and is-even, one of the dependencies uses it.

Collapse
 
richytong profile image
Richard Tong

I think the idea of your package is actually pretty useful. Just found something similar that has ~2k weekly downloads.

npmjs.com/package/range

My guess is it's just preference for is-odd and is-even.

Collapse
 
raounek profile image
touibeg mohamed

thank you for your short work ...

Collapse
 
ispoljari profile image
Ivan Spoljaric

No problem. Thank you for taking the time to read my post.

Collapse
 
orkhanfarmanli profile image
Orkhan Farmanli

JS community fascinates me every passing day

Collapse
 
elleattwell profile image
Harry Balls

Well, that's odd!

Collapse
 
fasani profile image
Michael Fasani

Keeps the playing field even!

Collapse
 
coly010 profile image
Colum Ferry

Gotta check the numbers on that one

Collapse
 
milan997 profile image
milan997

Why is line "num - num === 0" needed?

Collapse
 
ispoljari profile image
Ivan Spoljaric • Edited

Ambar explained it pretty well.

I think the best, and easiest way, to check if a variable is of type 'number' in JS, is to use the

Number.isFinite(num)

method.

It will weed out anything that is not a number, including NaN and Infinity.

Collapse
 
supercoww profile image
Ambar Mutha • Edited

If simply true is returned, input NaN (not a number) will also return true. It turns out typeof NaN is number. Also NaN - NaN is NaN so this expression num - num === 0 returns true only when num is a number except NaN.

Collapse
 
ispoljari profile image
Ivan Spoljaric • Edited

Yeah. And same goes for Infinity and -Infinity.