<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Md Amir Gauhar</title>
    <description>The latest articles on Forem by Md Amir Gauhar (@mdamirgauhar).</description>
    <link>https://forem.com/mdamirgauhar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F674728%2F46762d9f-effd-4000-b336-428238cd626c.png</url>
      <title>Forem: Md Amir Gauhar</title>
      <link>https://forem.com/mdamirgauhar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mdamirgauhar"/>
    <language>en</language>
    <item>
      <title>Polyfills made easy</title>
      <dc:creator>Md Amir Gauhar</dc:creator>
      <pubDate>Wed, 12 Jan 2022 05:41:20 +0000</pubDate>
      <link>https://forem.com/mdamirgauhar/polyfills-made-easy-4p89</link>
      <guid>https://forem.com/mdamirgauhar/polyfills-made-easy-4p89</guid>
      <description>&lt;p&gt;Helloo, my fellow developers!!!&lt;/p&gt;

&lt;p&gt;Let's talk about &lt;strong&gt;Polyfills&lt;/strong&gt; today. If you are new to this terminology, I'll make sure that this will make complete sense to you.&lt;/p&gt;

&lt;p&gt;Let's just begin...&lt;br&gt;
 So, a polyfill basically is a piece of javascript code that is used to provide or fill in some functionality that one browser supports but other might not.&lt;/p&gt;

&lt;p&gt;Lets make it easy for you to understand by taking an example.&lt;br&gt;
Let us talk about &lt;strong&gt;Array.prototype.forEach()&lt;/strong&gt;. The forEach() method executes a provided function once for each array element.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;forEach() calls a provided callbackFn function once for each element in an array in ascending index order.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1, 2, 3, 4, 5]
arr.forEach(val =&amp;gt; {
  console.log(val * 2)
})

// The above piece of code will take each element of that array/list and will multiply it by 2.
 2
 4
 6
 8
 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets pretend that we don't have support for &lt;em&gt;&lt;strong&gt;forEach()&lt;/strong&gt;&lt;/em&gt;.&lt;br&gt;
&lt;code&gt;// Simulate browser incompatibility&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Array.prototype.forEach = null&lt;/code&gt;&lt;br&gt;
 If we try to run the above code again we'll get the following error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BxQ4vsc---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf3aj346ne7z5186p15t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BxQ4vsc---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vf3aj346ne7z5186p15t.png" alt="Error" width="875" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's write a very simple polyfill for forEach() .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (!Array.prototype.forEach) {

  // polyfill
  Array.prototype.forEach = function (callback) {
    // callback here is the callback function
    // which actual .forEach() function accepts
    for (let value of this)
      callback(value)
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we re-run the forEach() method again, it will work perfectly fine.&lt;/p&gt;

&lt;p&gt;Lets take a complete look at our code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0CuUuz2N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23x515r1tjfpbtrt5836.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0CuUuz2N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23x515r1tjfpbtrt5836.png" alt="polyfill of forEach()" width="880" height="1135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Voilla&lt;/strong&gt;&lt;/em&gt;, we just created a very very rough polyfill for forEach().&lt;br&gt;
Hope you all liked it...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Firebase Google authentication with React</title>
      <dc:creator>Md Amir Gauhar</dc:creator>
      <pubDate>Fri, 13 Aug 2021 18:49:14 +0000</pubDate>
      <link>https://forem.com/mdamirgauhar/firebase-google-authentication-with-react-gop</link>
      <guid>https://forem.com/mdamirgauhar/firebase-google-authentication-with-react-gop</guid>
      <description>&lt;p&gt;Hellooo, my fellow developers.&lt;br&gt;
Today we're going to learn about authenticating our react app with google sign-in using Firebase.&lt;/p&gt;

&lt;p&gt;First of all let us talk a little bit about what actually Firebase is.&lt;br&gt;
&lt;strong&gt;Google Firebase&lt;/strong&gt; is a Google-backed application development software that enables developers to develop iOS, Android and Web apps. Firebase provides tools for tracking analytics, reporting and fixing app crashes, creating marketing and product experiment.Firebase offers a number of services, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Analytics &lt;/li&gt;
&lt;li&gt;Authentication &lt;/li&gt;
&lt;li&gt;Cloud messaging &lt;/li&gt;
&lt;li&gt;Realtime database&lt;/li&gt;
&lt;li&gt;Performance and many more...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.&lt;/p&gt;

&lt;p&gt;Now let's jump into our project.&lt;/p&gt;

&lt;p&gt;First of all we’ll setup our Firebase project. To do that go to &lt;a href="//firebase.google.com"&gt;firebase.google.com&lt;/a&gt; and login in with our google account. We’ll now initialize our app.&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Get Started&lt;/strong&gt; or &lt;strong&gt;Go to console&lt;/strong&gt; at the top right of the webpage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx32amjykyo9klcjzok71.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx32amjykyo9klcjzok71.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we’ll be taken to &lt;strong&gt;Add Project&lt;/strong&gt; page. Select Add project and keep the project name whatever you like. Now it’ll ask us to choose a Firebase account. We’ll choose the default one. After that it'll redirect us to the Project Overview page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wfx2sseh1mm06aax7t7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wfx2sseh1mm06aax7t7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To add an app click on the web icon. It’ll allow us to create an app and allow us to give a nickname to our app. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftshednpr4iuno32gvnf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftshednpr4iuno32gvnf4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now after registering our app let’s copy the firebaseConfig object which will help us later to initialize our web app and connect it with firebase. The config object will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rwluiqh24j0hq4na2oq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rwluiqh24j0hq4na2oq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to enable the authentication, let’s get back to project overview page and click on the authentication tab then set up sign-in method and  enable the Google authentication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73ea0tl1qsm31a8hskq8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73ea0tl1qsm31a8hskq8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yaay!!!&lt;/strong&gt;  we are done with the first part of our project. Now here comes the fun part. Let’s write some code.&lt;/p&gt;

&lt;p&gt;To begin with coding first of all we’ll create our starter project directory using the following command in the terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npx create-react-app google-auth    &lt;em&gt;//google-auth is my directory name&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It will create a basic react app boilerplate for us.&lt;/p&gt;

&lt;p&gt;Now we’ll install firebase in our app using the following command in the terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install firebase&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; If you’re installing firebase for the first time on your system, type the following in the terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm install -g firebase-tools&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's put all that to our use.&lt;br&gt;
We’ll create a service folder and add firebase.js into it and add the following code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;src/service/firebase.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import firebase from "firebase/app";
import 'firebase/auth';

const firebaseConfig = {
  apiKey: "API_KEY",
  authDomain: "PROJECT_ID.firebaseapp.com",
  databaseURL: "https://PROJECT_ID.firebaseio.com",
  projectId: "PROJECT_ID",
  storageBucket: "PROJECT_ID.appspot.com",
  messagingSenderId: "SENDER_ID",
  appId: "APP_ID",
  measurementId: "G-MEASUREMENT_ID",
};

// Initialize Firebase 
firebase.initializeApp(firebaseConfig);

export const auth = firebase.auth();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we’ll create a sign-in function which will let us sign-in with google.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;src/service/firebase.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const auth = firebase.auth();

const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });

export const signInWithGoogle = () =&amp;gt; auth.signInWithPopup(provider);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole code will look like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filmx5a107qgbgt0iwiyp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Filmx5a107qgbgt0iwiyp.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use this function, we’ll import it to our Login component and add a onClick handler on the sign in button. So. lets create a Login component inside a components folder and add the following code:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;src/compenents/Login.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { signInWithGoogle } from '../services/firebase';

import '../App.css';

const Login = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button className="button" onClick={signInWithGoogle}&amp;gt;&amp;lt;i className="fab fa-google"&amp;gt;&amp;lt;/i&amp;gt;Sign in with google&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Login;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's import the Login component into our App.js file.&lt;br&gt;
And that’s it. We can now sign-in to our app via our google accounts.&lt;/p&gt;

&lt;p&gt;Now let us store the user information so that we can use it in our application. In the App.js file add the following codes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;src/App.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react';

import Login from './components/Login';
import Home from './components/Home';
import firebase from './services/firebase';

import './App.css';



function App() {
  const [user, setUser] = useState(null);

  useEffect(() =&amp;gt; {
    firebase.auth().onAuthStateChanged(user =&amp;gt; {
      setUser(user);
    })
  }, [])

  console.log(user);

  return (
    &amp;lt;div className="app"&amp;gt;
      &amp;lt;Login /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create a Home component to use the information that we got after logging in. Create a Home Component inside the components app and add the following code into it:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;src/components/Home.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

import { auth } from '../services/firebase'

import '../App.css';

const Home = ({ user }) =&amp;gt; {
  return (
    &amp;lt;div className="home"&amp;gt;
      &amp;lt;h1&amp;gt;Hello, &amp;lt;span&amp;gt;&amp;lt;/span&amp;gt;{user.displayName}&amp;lt;/h1&amp;gt;
      &amp;lt;img src={user.photoURL} alt="" /&amp;gt;
      &amp;lt;button className="button signout" onClick={() =&amp;gt; auth.signOut()}&amp;gt;Sign out&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Home;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's import the Home component into our main App.js file. After importing it'll look something like the following:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu88togkdzu2syba3c4a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu88togkdzu2syba3c4a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkylro3mkiq6sozl14z3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkylro3mkiq6sozl14z3w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6eqnvn0n9rkoujbt97c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6eqnvn0n9rkoujbt97c.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F766zwurxj1e835nbaabl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F766zwurxj1e835nbaabl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Voila&lt;/em&gt;&lt;/strong&gt;, we just created a mini app where we can login with our google account.&lt;br&gt;
Hope you all liked it....&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>react</category>
      <category>google</category>
      <category>authentication</category>
    </item>
    <item>
      <title>How did I publish my 1st NPM package.</title>
      <dc:creator>Md Amir Gauhar</dc:creator>
      <pubDate>Tue, 27 Jul 2021 04:05:49 +0000</pubDate>
      <link>https://forem.com/mdamirgauhar/how-did-i-publish-my-1st-npm-package-25h5</link>
      <guid>https://forem.com/mdamirgauhar/how-did-i-publish-my-1st-npm-package-25h5</guid>
      <description>&lt;p&gt;Hello fellow developers! &lt;br&gt;
We have came across various npm packages which had made our lives more simpler and easier. If you don’t know what NPM is then let me help you to know a little bit about it. NPM is package manager for NodeJS which was created in 2009 as an open source project to help JavaScript Developers easily share their codes in form of packages.&lt;/p&gt;

&lt;p&gt;So in this article I’ll talk about how I published my 1st NPM package. Creating your first NPM package may seem incredibly intimidating but it is actually surprisingly easy. The main focus of this articles isn’t to build a bad-ass npm package but to explain how to build and publish a npm package.&lt;/p&gt;

&lt;p&gt;Now let’s get started...&lt;br&gt;
To publish a NPM package, all we need is the NPM command Line tool which is also called &lt;em&gt;npm&lt;/em&gt;. When we install NodeJS in our system, we automatically get npm installed in our computer. To download NodeJS visit &lt;a href="https://nodejs.org"&gt;here&lt;/a&gt;.&lt;br&gt;
After installing &lt;em&gt;npm&lt;/em&gt;, we can go ahead and start creating our package. Now in the terminal we’ll do the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;h6&gt;
  
  
  creating the project directory
&lt;/h6&gt;

&lt;p&gt;mkdir reverse-string &lt;/p&gt;
&lt;h6&gt;
  
  
  change into the project directory
&lt;/h6&gt;

&lt;p&gt;cd reverse-string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before we start writing our code, we need to add a &lt;strong&gt;package.json&lt;/strong&gt; file to our project. For that run we need to run the following command in the terminal :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm init&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_ghBrpyD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voggngswmsh2jyu53oyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_ghBrpyD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/voggngswmsh2jyu53oyj.png" alt="npm init"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we need to answer some questions which is basically about the package we are creating. After answering the questions, the &lt;strong&gt;package.json&lt;/strong&gt; will be created in the root of the project and will look like this&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--34_ktNkA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xywzelvvss1h7e3fmvbv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--34_ktNkA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xywzelvvss1h7e3fmvbv.png" alt="package.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let’s start writing our code. We are going to create a package to reverse a string.&lt;br&gt;
Create an &lt;em&gt;index.js&lt;/em&gt; file in the root of the project and add the following code for reversing the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverse(string) {
  return string
    .toLowerCase()
    .split("")
    .reverse()
    .join("");
};

module.exports = reverse;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s publish the package…&lt;br&gt;
In order to publish the package to NPM registry we need to create an account in the NPM registry. After creating the account go to the email we provided to verify our account. Then we'll go to the terminal and authenticate ourselves using:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm login.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After entering all the credentials, we can now publish our package using the following command:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm publish &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that we may not be able to publish the package if someone else already has a package with same name in the registry. We can simply change the package name  to some unique name or simply change it to @username/package-name.&lt;br&gt;
In my case I’ll rename my package name to &lt;strong&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/mdamirgauhar"&gt;@mdamirgauhar&lt;/a&gt;
/reverse-string&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we have a name-spaced package, NPM tries to make it a private package instead of public. In order to publish our package we need to run the following command in the terminal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm publish --access=public.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Voila&lt;/strong&gt;, we have created our first npm package. Hope you liked it..&lt;/p&gt;

</description>
      <category>npm</category>
      <category>node</category>
    </item>
  </channel>
</rss>
