hashnode-blogs

Letโ€™s use a js package ๐ŸŽ

๐Ÿšฉ we would only discuss about javascript packages

What is a package? ๐Ÿค”

It is a piece of code written by some smart people which does something. If you want that functionality without writing all the code by yourself, you can use that particular package.

for example ๐Ÿ–Š๏ธ

you are creating a website and at some point, you need to check if the email entered by the user is valid or not,
You have two potential ways,

function isEmail(emailString) {
    // check for @ sign
    var atSymbol = emailString.indexOf("@");
    if(atSymbol < 1) return false;
    
    var dot = emailString.indexOf(".");
    if(dot <= atSymbol + 2) return false;
    
    // check that the dot is not at the end
    if (dot === emailString.length - 1) return false;
    
    return true;
}

isEmail('awe@some.com'); // => true
var validator = require('validator');

validator.isEmail('awe@some.com'); //=> true

How to start using a package? ๐Ÿง‘โ€๐Ÿ’ป

Prerequisites

Lets start from scratch ๐Ÿ’ซ

npm init -y
  npm install validator
const validator = require('validator');
console.log(validator.isEmail('some@thing.com'));

At this time, your directory should look like

/**
* hello-packages
* -- node_modules
* -- index.js
* -- package-lock.json
* -- package.json
*/
 node index.js
true

check this out to explore more features and functionality that validator package can offer.

check this out to know what a package.json file is

Using a package in a project that doesnโ€™t use modules but static HTML files

you can use their cdn https://unpkg.com/validator@latest/validator.min.js

<script src="https://unpkg.com/validator@latest/validator.min.js"></script>

Add a script tag in your HTML head with cdn url.

How to check what features and functionality a package offer? ๐Ÿชถ

Most of them have great documentation.
or you can check out their GitHub repositories.

Who writes these packages? ๐Ÿง 

Smart people like you and me.
Most of the packages are free to install, and you can contribute to their GitHub repos.

What is npm?

npm stands for node package manager.
It is a command line client. which means that you can use it to

It is also an online database of public and paid-for private packages, called the npm registry.

Source code: All the codes used in this blog. ๐Ÿ˜Š