Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 24, 2020 02:50 pm GMT

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-oddreturn (n % 2) === 1;

or this:

// is-numberif (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.

And for the fun of it I also included is-number as a dependency to my package.

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'; // webORconst linearArray = require('linear-array'); // serverconsole.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.

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
  • Rungit initin your terminal
  • Push to Github repo with the same name as the local project name
  • Runnpm initin your terminal
  • Register your profile on https://www.npmjs.com/
  • Runnpm loginin your terminal
  • Runnpm publishin 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!


Original Link: https://dev.to/ispoljari/i-just-created-my-first-npm-package-it-ain-t-much-but-it-s-honest-work-5h94

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To