<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ npm - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ npm - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 26 May 2026 22:47:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/npm/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Use the react-mui-sidebar Package to Build Responsive, Customizable Sidebars ]]>
                </title>
                <description>
                    <![CDATA[ In modern web development, a well-designed sidebar can greatly improve the user experience by providing easy navigation and access to important features. The react-mui-sidebar, powered by Material-UI, is a helpful React NPM package designed to make i... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-react-mui-sidebar-to-build-responsive-customizable-sidebars/</link>
                <guid isPermaLink="false">68e841313a3a459fe2b24ab2</guid>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Next.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ material ui ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Hitesh Chauhan ]]>
                </dc:creator>
                <pubDate>Thu, 09 Oct 2025 23:11:45 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760051493063/cc42647f-21e7-48f6-873f-b17db780a24a.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In modern web development, a well-designed sidebar can greatly improve the user experience by providing easy navigation and access to important features.</p>
<p>The <a target="_blank" href="https://www.npmjs.com/package/react-mui-sidebar">react-mui-sidebar</a>, powered by <a target="_blank" href="https://mui.com/">Material-UI</a>, is a helpful React NPM package designed to make it easier to build responsive and highly customizable sidebars.</p>
<p>In this article, we will explore the key aspects of react-mui-sidebar and how you can use it to elevate the sidebar experience in your React applications.</p>
<h2 id="heading-table-of-contents"><strong>Table of Contents</strong></h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-mui-material-ui">What is MUI (Material-UI)?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-choose-react-mui-sidebar">Why Choose react-mui-sidebar?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-get-started-with-react-mui-sidebar">How to Get Started with react-mui-sidebar</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-wrapping-up">Wrapping Up</a></p>
</li>
</ul>
<h2 id="heading-prerequisites"><strong>Prerequisites</strong></h2>
<p>Before diving into the tutorial, make sure you have the following:</p>
<ul>
<li><p><strong>Basics of React</strong> like components, props, state, and JSX</p>
</li>
<li><p><strong>Familiarity with React Router or Next.js App Router</strong> so you know how linking/navigation works</p>
</li>
<li><p><strong>Understanding of Material-UI / MUI</strong>: theming, components, styling</p>
</li>
<li><p><strong>NPM / Yarn package usage</strong>: installing, importing, and managing dependencies</p>
</li>
<li><p><strong>Basic CSS / layout skills</strong>: Flexbox, widths, responsive design, and so on</p>
</li>
</ul>
<p>Here’s what we’ll be building:</p>
<p><a target="_blank" href="https://www.npmjs.com/package/react-mui-sidebar"><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756104211843/d56fb298-d190-4d14-9923-e7ec0827a662.jpeg" alt="d56fb298-d190-4d14-9923-e7ec0827a662" class="image--center mx-auto" width="1051" height="867" loading="lazy"></a></p>
<p>Here’s a <a target="_blank" href="https://react-mui-sidebar.vercel.app/">live demo</a> you can check out, too.</p>
<h2 id="heading-what-is-mui-material-ui"><strong>What is MUI (Material-UI)?</strong></h2>
<p>Material-UI is a widely adopted React UI framework that brings Google's Material Design principles to React applications. React MUI Sidebar leverages the strengths of Material-UI, providing a seamless integration for creating visually appealing and responsive sidebars.</p>
<h3 id="heading-features-of-react-mui-sidebar"><strong>Features of React MUI Sidebar</strong></h3>
<ol>
<li><p><strong>Responsive design:</strong> React MUI Sidebar ensures a consistent and adaptive layout across various screen sizes, catering to the increasing demand for mobile-friendly interfaces.</p>
</li>
<li><p><strong>Customization options:</strong> You can easily change the appearance of the sidebar to match the overall design of you application, allowing for a cohesive and branded look.</p>
</li>
<li><p><strong>Integration with React and Next.js:</strong> React MUI Sidebar seamlessly integrates with both React and Next.js, offering a familiar and efficient development experience.</p>
</li>
<li><p><strong>User-friendly:</strong> React MUI Sidebar is designed with simplicity in mind, making it easy to incorporate into your projects. With intuitive documentation, you can quickly grasp and implement the sidebar functionality.</p>
</li>
<li><p><strong>Icons support:</strong> The sidebar comes with built-in support for icons, helping you enhance the visual appeal and usability of your applications. You can use any icon library and provide the icon component.</p>
</li>
<li><p><strong>Menu and submenu support:</strong> It provides a hierarchical structure with support for both main menus and nested submenus. This feature enables you to organize and present complex navigation structures in a clear and intuitive manner.</p>
</li>
<li><p><strong>Smooth transitions:</strong> The sidebar incorporates smooth transition effects, enhancing the overall user experience by providing a visually pleasing navigation flow. Animations are thoughtfully implemented to avoid any jarring effects during sidebar interactions.</p>
</li>
</ol>
<h2 id="heading-why-choose-react-mui-sidebar"><strong>Why Choose react-mui-sidebar?</strong></h2>
<p>There are various reasons to use this helpful tool. Here are a few big ones:</p>
<h3 id="heading-optimized-performance"><strong>Optimized Performance</strong></h3>
<p>react-mui-sidebar is designed and configured for high performance, ensuring smooth and responsive user interactions. Internally, it minimizes re-renders via memoization so that only parts of the menu that actually change are updated. It uses lightweight DOM structure and conditional rendering of nested items to avoid unnecessary mounting.</p>
<p>Since it's built on MUI, it also leverages efficient styling via the <code>sx</code> prop or styled components rather than heavy theme recalculations.</p>
<p>These features are crucial for applications, especially those with complex or intricate user interfaces, where performance issues can negatively impact the user experience.</p>
<h3 id="heading-community-strength"><strong>Community Strength</strong></h3>
<p>React MUI Sidebar benefits from a large and active user base thanks in part to the large and active Material UI community. This is helpful for users because it means there's a wealth of resources, tutorials, and assistance available.</p>
<p>A strong community can contribute to the growth of the framework, provide solutions to common issues, and foster collaboration among developers.</p>
<h3 id="heading-reliable-maintenance"><strong>Reliable Maintenance</strong></h3>
<p>react-mui-sidebar is actively supported by consistent updates and proactive maintenance from its community. Regular updates often include bug fixes, security patches, and new features, which help make sure that the framework remains current and adaptable to users’ evolving needs. This is especially important for long-term projects, and shows a commitment to the ongoing improvement and support of the framework.</p>
<h2 id="heading-how-to-get-started-with-react-mui-sidebar"><strong>How to Get Started with react-mui-sidebar</strong></h2>
<p>In this tutorial, I’ll walk you through installing and setting up react-mui-sidebar in a React and Next.js application. You’ll learn how to integrate the sidebar, add a logo, create menus, and use links.</p>
<h3 id="heading-step-1-install-react-mui-sidebar">Step 1: Install react-mui-sidebar</h3>
<p>To begin, you’ll need to install the <a target="_blank" href="https://www.npmjs.com/package/react-mui-sidebar">react-mui-sidebar package</a> into your React and Next.js project. You can do this using either npm or yarn.</p>
<h4 id="heading-using-npm">Using npm:</h4>
<pre><code class="lang-javascript">npm install react-mui-sidebar
</code></pre>
<h4 id="heading-using-yarn">Using yarn:</h4>
<pre><code class="lang-javascript">yarn add react-mui-sidebar
</code></pre>
<p>This will add react-mui-sidebar and its dependencies to your project.</p>
<h3 id="heading-step-2-import-the-react-mui-sidebar-component">Step 2: Import the React MUI Sidebar Component</h3>
<p>Once the package is installed, you can import the necessary components from react-mui-sidebar into your project. These components will allow you to customize the sidebar with menus, submenus, and even a logo.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { Sidebar, Menu, MenuItem, Submenu, Logo } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-mui-sidebar"</span>;
</code></pre>
<h3 id="heading-step-3-set-up-routing-for-react-app-or-nextjs-app">Step 3: Set Up Routing for React App or Next.js App</h3>
<p>To enable navigation inside react-mui-sidebar components like MenuItem or Logo, you need to pass a link component from either react-router or next/link using the component prop inside the corresponding component like what’s shown in the below example:</p>
<p>If you're using <strong>React Router:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Sidebar</span> <span class="hljs-attr">width</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">270px</span>"}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span>
        <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  // <span class="hljs-attr">Passing</span> <span class="hljs-attr">link</span> <span class="hljs-attr">to</span> <span class="hljs-attr">component</span> <span class="hljs-attr">for</span> <span class="hljs-attr">routing</span>
        <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>
        <span class="hljs-attr">img</span>=<span class="hljs-string">"https://adminmart.com/wp-content/uploads/2024/03/logo-admin-mart-news.png"</span>
      &gt;</span>
        AdminMart
      <span class="hljs-tag">&lt;/<span class="hljs-name">Logo</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Menu</span> <span class="hljs-attr">subHeading</span>=<span class="hljs-string">"HOME"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>
          <span class="hljs-attr">icon</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">CottageOutlinedIcon</span> /&gt;</span>}
          component={Link} // Passing link to component for routing
          link="/tes"
          badge={true}
          isSelected={true}
        &gt;
          {" "}
          {/* text for your link */}
          Link Text
        <span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Menu</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Sidebar</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>If you're using <strong>Next.js App Router:</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> Link <span class="hljs-keyword">from</span> <span class="hljs-string">"next/link"</span>;

<span class="hljs-keyword">const</span> App = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Sidebar</span> <span class="hljs-attr">width</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">270px</span>"}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span>
        <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  // <span class="hljs-attr">Passing</span> <span class="hljs-attr">link</span> <span class="hljs-attr">to</span> <span class="hljs-attr">component</span> <span class="hljs-attr">for</span> <span class="hljs-attr">routing</span>
        <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>
        <span class="hljs-attr">img</span>=<span class="hljs-string">"https://adminmart.com/wp-content/uploads/2024/03/logo-admin-mart-news.png"</span>
      &gt;</span>
        AdminMart
      <span class="hljs-tag">&lt;/<span class="hljs-name">Logo</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Menu</span> <span class="hljs-attr">subHeading</span>=<span class="hljs-string">"HOME"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>
          <span class="hljs-attr">icon</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">CottageOutlinedIcon</span> /&gt;</span>}
          component={Link} // Passing link to component for routing
          link="/tes"
          badge={true}
          isSelected={true}
        &gt;
          {" "}
          {/* text for your link */}
          Link Text
        <span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Menu</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Sidebar</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="heading-step-4-initialize-the-sidebar">Step 4: Initialize the Sidebar</h3>
<p>Now we’ll set up the Sidebar component in your application. You can set the width of the sidebar using the width prop. Here’s a simple example:</p>
<pre><code class="lang-javascript">&lt;Sidebar width={<span class="hljs-string">"270px"</span>}&gt; <span class="hljs-comment">// pass the width you want your sidebar to have</span>
  {<span class="hljs-comment">/* Sidebar Content Goes Here */</span>}
&lt;/Sidebar&gt;
</code></pre>
<p>This initializes the sidebar with a width of 270px. You can adjust this width based on your design requirements.</p>
<h3 id="heading-step-5-add-a-logo-to-the-sidebar">Step 5: Add a Logo to the Sidebar</h3>
<p>You can add a logo inside the sidebar by using the Logo component. To do so, you can provide an src URL or use the img prop to link to a CDN logo image. You can also make the logo clickable by passing a navigation link using the component and href props. Here’s how to do this:</p>
<pre><code class="lang-javascript">&lt;Sidebar width={<span class="hljs-string">"270px"</span>}&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Logo</span> 
    <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span> 
    <span class="hljs-attr">href</span>=<span class="hljs-string">”/”</span>
    <span class="hljs-attr">img</span>=<span class="hljs-string">"https://adminmart.com/wp-content/uploads/2024/03/logo-admin-mart-news.png"</span> // <span class="hljs-attr">path</span> <span class="hljs-attr">for</span> <span class="hljs-attr">image</span> <span class="hljs-attr">you</span> <span class="hljs-attr">want</span> <span class="hljs-attr">to</span> <span class="hljs-attr">use</span> <span class="hljs-attr">as</span> <span class="hljs-attr">your</span> <span class="hljs-attr">logo</span>
  &gt;</span>
    AdminMart
  <span class="hljs-tag">&lt;/<span class="hljs-name">Logo</span>&gt;</span></span>
&lt;/Sidebar&gt;
</code></pre>
<p>In this example, we’ve added a logo from a CDN using the img prop, used the component prop to pass the Link, and set the navigation path to (/) (the homepage) using the href prop. We’ve also set the text “AdminMart” as the name of the application.</p>
<h3 id="heading-step-6-create-a-menu-inside-the-sidebar">Step 6: Create a Menu Inside the Sidebar</h3>
<p>Now let’s create a menu inside the sidebar using the Menu component. You can specify a submenu heading using the subHeading prop. Inside the Menu, you can add MenuItem components for each item.</p>
<p>You can also provide a link prop along with the component prop to the MenuItem to turn the item into a clickable link.</p>
<p>Here’s how you can structure the menu:</p>
<pre><code class="lang-javascript">&lt;Sidebar width={<span class="hljs-string">"270px"</span>}&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Menu</span> <span class="hljs-attr">subHeading</span>=<span class="hljs-string">"HOME"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  <span class="hljs-attr">link</span>=<span class="hljs-string">"/"</span>  <span class="hljs-attr">badge</span>=<span class="hljs-string">”true”</span>  <span class="hljs-attr">isSelected</span>=<span class="hljs-string">{true}</span> &gt;</span>
      Modern
    <span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>&gt;</span>eCommerce<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>&gt;</span>Analytical<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Menu</span>&gt;</span></span>
&lt;/Sidebar&gt;
</code></pre>
<p>In this example:</p>
<ul>
<li><p>We’ve added a Menu with the heading “HOME”.</p>
</li>
<li><p>The first MenuItem has a link prop, so clicking it will navigate to the homepage (/).</p>
</li>
<li><p>The second and third MenuItem components are simple text items without links.</p>
</li>
</ul>
<p>The <code>badge="true"</code> prop can be used to indicate a badge or notification on the MenuItem, and the <code>isSelected={true}</code> prop marks this menu item as currently selected or active. You can customize this feature according to your needs.</p>
<h3 id="heading-step-7-add-submenus-optional">Step 7: Add Submenus (Optional)</h3>
<p>To add submenus inside the main menu, you can use the Submenu component. The Submenu can be nested inside the Menu component and contains its own set of MenuItems. Use the title prop to set the submenu heading</p>
<p>Here’s an example of adding a submenu:</p>
<pre><code class="lang-javascript">&lt;Sidebar width={<span class="hljs-string">"270px"</span>}&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Menu</span> <span class="hljs-attr">subHeading</span>=<span class="hljs-string">"SERVICES"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>   <span class="hljs-attr">link</span>=<span class="hljs-string">"/web-development"</span>&gt;</span>Web Development<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>   <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  <span class="hljs-attr">link</span>=<span class="hljs-string">"/seo-services"</span>&gt;</span>SEO Services<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Submenu</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Marketing"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  <span class="hljs-attr">link</span>=<span class="hljs-string">"/digital-marketing"</span>&gt;</span>Digital Marketing<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Link}</span>  <span class="hljs-attr">link</span>=<span class="hljs-string">"/content-marketing"</span>&gt;</span>Content Marketing<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Submenu</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Menu</span>&gt;</span></span>
&lt;/Sidebar&gt;
</code></pre>
<p>In this example:</p>
<ul>
<li><p>A submenu under the "Marketing" heading is added inside the "SERVICES" menu.</p>
</li>
<li><p>The submenu contains two MenuItems with links to different service pages.</p>
</li>
</ul>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>You have now successfully integrated a fully functional sidebar in your React and Next.js application using react-mui-sidebar. You can further customize the sidebar by:</p>
<ul>
<li><p>Modifying the width and design.</p>
</li>
<li><p>Adding more submenus, menu items, or icons.</p>
</li>
<li><p>Using links to navigate between pages.</p>
</li>
</ul>
<p>This setup provides a flexible, responsive, and easy-to-use sidebar, which is perfect for most web applications.</p>
<h3 id="heading-try-it-out">Try It Out:</h3>
<p>You can view the working demo of the react-mui-sidebar here: <a target="_blank" href="https://react-mui-sidebar.vercel.app/"><strong>View Demo</strong></a><strong>.</strong></p>
<p><strong>Note:</strong> in this tutorial, we utilized <a target="_blank" href="https://mui.com/material-ui/material-icons/">Material UI Icons</a> to construct this sidebar. Feel free to choose an alternative library or use different icons based on your specific requirements.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Publish an npm Package - Explained with Examples ]]>
                </title>
                <description>
                    <![CDATA[ If you’ve spent any time working with JavaScript, you’ve most likely come across npm—whether installing packages like Express, Lodash, or React, or running commands like npm init. While using npm is second nature for many JavaScript developers, some ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-publish-an-npm-package/</link>
                <guid isPermaLink="false">68d44cc07b532bf921e47213</guid>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ open source ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ikegah Oliver ]]>
                </dc:creator>
                <pubDate>Wed, 24 Sep 2025 19:55:44 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758743190885/78dd4f19-53eb-4101-9cf9-7c22ab5f6be2.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you’ve spent any time working with JavaScript, you’ve most likely come across npm—whether installing packages like Express, Lodash, or React, or running commands like <code>npm init</code>. While using npm is second nature for many JavaScript developers, some have never explored how those packages are created, structured, and shared with the rest of the world. Behind each command lies a powerful system for building, managing, and distributing JavaScript code at scale.</p>
<p>This article will guide you through the basics of npm, explain how it functions behind the scenes, and demonstrate step-by-step how to create and publish your own npm package to the official npm registry.  Whether you’re a beginner just starting with JavaScript or a seasoned developer who has used npm but never published a package, this guide will help you confidently navigate the entire process, from setup to sharing your code with the global developer community.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-npm">What is npm?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-components-of-npm">Components of npm</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-command-line-interface">The Command Line Interface</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-registry">The Registry</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-website">The Website</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-is-the-packagejson-file">What is the Package.json File?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-npm-works">How npm Works</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-publish-an-npm-library">How to Publish an npm Library</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-verify-and-install-your-package">How to Verify and Install Your Package</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-update-your-package">How to Update Your Package</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-notes-and-best-practices">Notes and Best Practices</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-beyond-the-basics">Beyond the Basics</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-what-is-npm">What is npm?</h2>
<p>npm, which stands for Node Package Manager, is a command-line tool for installing and managing JavaScript packages. It is an ecosystem that powers modern JavaScript development (<a target="_blank" href="http://node.js">Node.js</a>, frontend tools, frameworks, and so on). Developers use npm to share and borrow packages, and many organizations use npm to manage private development.</p>
<p>Essentially, npm is to JavaScript what pip is to Python or Maven to Java. It enables developers to reuse code written by others (most of the time to fulfil a function in their project), manage dependencies, and share their own code with the world.</p>
<p>You can use npm to:</p>
<ul>
<li><p>Get and utilize code packages in your applications, either as-is or with custom modifications.</p>
</li>
<li><p>Download and run standalone tools instantly.</p>
</li>
<li><p>Execute packages directly from the registry without installing them using npx.</p>
</li>
<li><p>Share your own code with developers around the world through the npm registry.</p>
</li>
<li><p>Limit access to specific packages so only approved developers can use them.</p>
</li>
<li><p>Create organizations to manage code, teams, and packages in one place.</p>
</li>
<li><p>Collaborate as virtual teams using shared organizational accounts.</p>
</li>
<li><p>Handle different versions of packages and their dependencies with ease.</p>
</li>
<li><p>Keep your applications up to date by syncing with the latest package updates.</p>
</li>
<li><p>Explore different packages that offer various solutions to the same problem.</p>
</li>
<li><p>Connect with developers working on similar challenges and projects.</p>
</li>
</ul>
<h2 id="heading-components-of-npm">Components of npm</h2>
<p>npm consists of three core components:</p>
<ul>
<li><p>The command line interface (CLI)</p>
</li>
<li><p>The registry</p>
</li>
<li><p>The website</p>
</li>
</ul>
<h2 id="heading-the-command-line-interface">The Command Line Interface</h2>
<p>There are different npm commands you can run on your terminal. For example, <code>npm init</code> can be used to initialize a Node project, <code>npm install</code> can be used to install a package. It also allows you to do things like:</p>
<ul>
<li><p>Publish packages (<code>npm publish</code>)</p>
</li>
<li><p>Update packages (<code>npm update</code>)</p>
</li>
<li><p>Manage versioning (<code>npm version</code>)</p>
</li>
<li><p>Run scripts (<code>npm run build</code>, <code>npm test</code>, and so on)</p>
</li>
</ul>
<p>Think of it as your control panel.</p>
<h2 id="heading-the-registry">The Registry</h2>
<p>You can find the huge public database at <a target="_blank" href="https://registry.npmjs.org">https://registry.npmjs.org</a>, where packages are stored and shared. It also contains all the meta-information surrounding the package.</p>
<p>Example, when you run:</p>
<pre><code class="lang-bash">npm install express
</code></pre>
<p>npm fetches the Express package from the registry.</p>
<p>The npm registry enables collaboration by allowing developers to:</p>
<ul>
<li><p>Publish their own packages</p>
</li>
<li><p>Install packages created by others</p>
</li>
<li><p>Discover new tools and libraries</p>
</li>
</ul>
<p>Its collaboration features include:</p>
<ul>
<li><p>Open-source packages: The code is publicly visible (usually hosted on GitHub).</p>
</li>
<li><p>Versioning: Multiple versions of the same package let users safely adopt updates.</p>
</li>
<li><p>Scoped packages: Namespaces allow teams and organizations to manage ownership.</p>
</li>
<li><p>Issues &amp; pull requests: Most npm packages link to GitHub, allowing developers too contribute fixes and enhancements.</p>
</li>
<li><p>Organisations: Teams can manage access to shared private or public packages.</p>
</li>
</ul>
<h2 id="heading-the-website">The Website</h2>
<p>At <a target="_blank" href="https://www.npmjs.com">https://www.npmjs.com</a>, this is where you can:</p>
<ul>
<li><p>Browse packages.</p>
</li>
<li><p>Read documentation.</p>
</li>
<li><p>View download stats and dependencies.</p>
</li>
<li><p>Create and manage your account, organization, and package access.</p>
</li>
</ul>
<h2 id="heading-what-is-the-packagejson-file">What is the Package.json File?</h2>
<p>A very important component of any npm tool that you’ll come across as a JavaScript developer installing an npm package, is the <code>package.json</code> file. It is a metadata file that lives at the root of every npm or Node project. It tells npm (and other tools) everything it needs to know about your project, like:</p>
<ul>
<li><p>What the project is called.</p>
</li>
<li><p>What it depends on</p>
</li>
<li><p>How to run it</p>
</li>
<li><p>How to version it</p>
</li>
<li><p>and how to publish it</p>
</li>
</ul>
<p>You can think of it like the blueprint of your JavaScript project. Without it, npm doesn't know how to work with your code.</p>
<p>You can create a <code>package.json</code> file by typing the command <code>npm init</code> in your terminal and filling in the prompts provided. Alternatively, you can create a file named <code>package.json</code> and manually populate it with JSON content.</p>
<h3 id="heading-key-fields-in-an-npm-packagejson-file">Key Fields in an npm package.json File</h3>
<p>The following is a list of commonly used fields and how they interact with npm:</p>
<ol>
<li><p><code>name</code> and <code>version</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"name"</span>: <span class="hljs-string">"my-awesome-package"</span>,
 <span class="hljs-string">"version"</span>: <span class="hljs-string">"1.0.0"</span>
</code></pre>
<ul>
<li><p>This is required for publishing.</p>
</li>
<li><p><code>name</code> must be unique (if publishing to the public npm registry).</p>
</li>
<li><p><code>version</code> follows semantic versioning (semver) (for example, major.minor.patch).  </p>
</li>
</ul>
</li>
<li><p><code>description</code>, <code>keywords</code>, <code>author</code>, and <code>license</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"description"</span>: <span class="hljs-string">"A utility to convert markdown to HTML"</span>,
 <span class="hljs-string">"keywords"</span>: [<span class="hljs-string">"markdown"</span>, <span class="hljs-string">"html"</span>, <span class="hljs-string">"converter"</span>],
 <span class="hljs-string">"author"</span>: <span class="hljs-string">"Ikegah Oliver"</span>,
 <span class="hljs-string">"license"</span>: <span class="hljs-string">"MIT"</span>
</code></pre>
<ul>
<li><p>Helps npm users discover your package</p>
</li>
<li><p>Show up on <a target="_blank" href="http://npmjs.com">npmjs.com</a>.</p>
</li>
<li><p>Sets collaboratory license</p>
</li>
</ul>
</li>
<li><p><code>scripts</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"scripts"</span>: {
   <span class="hljs-attr">"start"</span>: <span class="hljs-string">"node index.js"</span>,
   <span class="hljs-attr">"test"</span>: <span class="hljs-string">"jest"</span>,
   <span class="hljs-attr">"build"</span>: <span class="hljs-string">"tsc"</span>
 }
</code></pre>
<ul>
<li><p>Defines custom commands.</p>
</li>
<li><p>Run with npm run test, npm run build, and so on.</p>
</li>
<li><p>Automates build, test, lint, deploy processes.</p>
</li>
</ul>
</li>
<li><p><code>main</code> and <code>exports</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"main"</span>: <span class="hljs-string">"dist/index.js"</span>,
 <span class="hljs-string">"exports"</span>: {
   <span class="hljs-attr">"."</span>: <span class="hljs-string">"./dist/index.js"</span>
 }
</code></pre>
<ul>
<li><p><code>main</code>: Entry point for <code>require()</code> or import.</p>
</li>
<li><p><code>exports</code>: Controls exactly what parts of your package are exposed (especially useful for modern ESM and package security).</p>
</li>
</ul>
</li>
<li><p><code>dependencies</code> and <code>devDependencies</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"dependencies"</span>: {
   <span class="hljs-attr">"express"</span>: <span class="hljs-string">"^4.18.2"</span>
 },
 <span class="hljs-string">"devDependencies"</span>: {
   <span class="hljs-attr">"eslint"</span>: <span class="hljs-string">"^8.0.0"</span>,
   <span class="hljs-attr">"jest"</span>: <span class="hljs-string">"^29.0.0"</span>
 }
</code></pre>
<ul>
<li><p><code>dependencies</code> : Core packages your project needs to run in production (for example, <code>express</code>).</p>
</li>
<li><p><code>devDependencies</code> : Packages used only during development, like testing or build tools (for example, <code>jest</code>).</p>
</li>
</ul>
</li>
<li><p><code>engines</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"engines"</span>: {
   <span class="hljs-attr">"node"</span>: <span class="hljs-string">"&gt;=14.0.0"</span>
 }
</code></pre>
<ul>
<li><p>Specifies Node.js version your package supports.</p>
</li>
<li><p>Helps warn users before they install with an unsupported version.</p>
</li>
</ul>
</li>
<li><p><code>private</code></p>
<pre><code class="lang-json"> <span class="hljs-string">"private"</span>:<span class="hljs-string">"true"</span>
</code></pre>
<ul>
<li><p>Prevents accidental publishing to the public npm registry.</p>
</li>
<li><p>Used for monorepos and internal-only projects.</p>
</li>
</ul>
</li>
<li><p><code>files</code> (optional)</p>
<pre><code class="lang-json"> <span class="hljs-string">"files"</span>:[<span class="hljs-string">"/dist"</span>, <span class="hljs-string">"README.md"</span>]
</code></pre>
<ul>
<li><p>Controls what files get included when you run npm publish.</p>
</li>
<li><p>Reduces package size, omits build artifacts or test files.</p>
</li>
</ul>
</li>
</ol>
<p>A minimal npm <code>package.json</code> file looks like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"@oliver/markdown-to-html"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"Converts markdown to HTML with styles"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"dist/index.js"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"tsc"</span>,
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"jest"</span>
  },
  <span class="hljs-attr">"keywords"</span>: [<span class="hljs-string">"markdown"</span>, <span class="hljs-string">"html"</span>, <span class="hljs-string">"converter"</span>],
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Ikegah Oliver"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>,
  <span class="hljs-attr">"dependencies"</span>: {
    <span class="hljs-attr">"marked"</span>: <span class="hljs-string">"^5.0.0"</span>
  },
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"jest"</span>: <span class="hljs-string">"^29.0.0"</span>
  },
  <span class="hljs-attr">"engines"</span>: {
    <span class="hljs-attr">"node"</span>: <span class="hljs-string">"&gt;=14.0.0"</span>
  },
  <span class="hljs-attr">"files"</span>: [<span class="hljs-string">"dist/"</span>, <span class="hljs-string">"README.md"</span>]
}
</code></pre>
<p>The <code>package.json</code> file plays a central role in every npm workflow. It defines your project's identity, lists its dependencies, specifies useful scripts, and outlines how the package should behave when published. Without it, npm can't properly install packages, run commands, or publish your code to the registry.</p>
<h2 id="heading-how-npm-works">How npm Works</h2>
<p>When you type <code>npm install</code> in your terminal, npm initiates a behind-the-scenes process to install the necessary packages for your project. Depending on how you run the command, the process varies slightly as follows:</p>
<h3 id="heading-method-1-packagejson-already-has-dependencies"><strong>Method 1</strong> - <code>package.json</code> Already Has Dependencies</h3>
<p>If your <code>package.json</code> lists packages under <code>dependencies</code> or <code>devDependencies</code>, npm will:</p>
<ol>
<li><p><strong>Read those entries:</strong> It looks at the names and version ranges of each dependency.</p>
</li>
<li><p><strong>Contact the registry:</strong> npm queries <a target="_blank" href="https://registry.npmjs.org">https://registry.npmjs.org</a> to fetch metadata about each required package.</p>
</li>
<li><p><strong>Download the correct versions:</strong> It selects versions that match your version rules (such as ^4.17.0) and downloads the <code>.tgz</code> files.</p>
</li>
<li><p><strong>Unpack and install:</strong> It places the packages into the <code>node_modules</code> directory and caches them.</p>
</li>
<li><p><strong>Update package-lock.json:</strong> It logs the exact versions and dependency tree in this file to ensure consistent installs later.</p>
</li>
</ol>
<h3 id="heading-method-2-you-run-npm-install-without-existing-dependencies"><strong>Method 2</strong> - You Run <code>npm install &lt;package-name&gt;</code> Without Existing Dependencies</h3>
<p>If your <code>package.json</code> doesn't have any dependencies yet and you run something like this in your terminal:</p>
<pre><code class="lang-bash">npm install express
</code></pre>
<p>Then npm will run:</p>
<ol>
<li><p><strong>Resolve the package version:</strong> It fetches the latest version of Express (unless you specify a version manually).</p>
</li>
<li><p><strong>Download and install:</strong> The tarball is downloaded and placed in the <code>node_modules/</code> folder that is automatically generated. A tarball is the zipped-up version of the package (<code>express</code> in this case), containing the package’s JavaScript files, a <code>package.json</code>, a README, and anything else the author included for distribution that npm downloads and extracts onto your machine.</p>
</li>
<li><p><strong>Add to</strong> <code>package.json</code>: npm automatically adds the package to your dependencies list like this:</p>
</li>
</ol>
<pre><code class="lang-json"><span class="hljs-string">"dependencies"</span>: {
  <span class="hljs-attr">"express"</span>: <span class="hljs-string">"^4.18.2"</span>
}
</code></pre>
<ol start="4">
<li><strong>Create a</strong> <code>package-lock.json</code> <strong>(if it doesn't exist):</strong> It writes all the version info to lock things down for future installs.</li>
</ol>
<p>Note: If you run <code>npm install --save-dev jest</code>It’ll add the package under devDependencies instead.</p>
<h2 id="heading-how-to-publish-an-npm-library">How to Publish an npm Library</h2>
<p>To follow through with this guide section, you will need to have the following prerequisites:</p>
<ul>
<li><p>Stable network connection</p>
</li>
<li><p>A code editor with a terminal (like VSCode)</p>
</li>
<li><p>Basic knowledge of JavaScript and Node</p>
</li>
<li><p>A basic understanding of the Markdown Markup language (for README documentation)</p>
</li>
</ul>
<p>Now, let’s dive into publishing an npm library. Assuming you just built and tested a fantastic JavaScript tool that you would like to share with the world, and allow people to use it in their projects and applications, the steps below will guide you from having the tool on your local machine to making it publicly available as an npm package for anyone to install and enjoy:</p>
<h3 id="heading-step-1-create-a-free-npm-account">Step 1: Create a free npm account</h3>
<p>Before publishing, you will need an npm account.</p>
<ul>
<li><p>Go to <a target="_blank" href="https://www.npmjs.com/signup">https://www.npmjs.com/signup</a>.</p>
</li>
<li><p>Fill in the form with your username, email, and password.</p>
</li>
<li><p>Confirm your email address by clicking the link in the verification email.</p>
</li>
</ul>
<h3 id="heading-step-2-log-in-from-the-command-line">Step 2: Log in from the Command Line</h3>
<p>Once you’ve created your account, you need to log in through your terminal. Run:</p>
<pre><code class="lang-bash">npm login
</code></pre>
<p>npm will prompt you for your username, password, and email address. If everything is correct, npm will generate an authentication token and store it locally, so you don’t have to log in every time.</p>
<h3 id="heading-step-3-create-a-packagejson-file">Step 3: Create a <code>package.json</code> file</h3>
<p>If your project doesn't already have a <code>package.json</code> file, create one by typing this command in your terminal:</p>
<pre><code class="lang-bash">npm init
</code></pre>
<p>You will be prompted to fill in the following information:</p>
<ul>
<li><p><strong>name</strong>: Must be unique if it’s public.</p>
</li>
<li><p><strong>version</strong>: Start with 1.0.0 (it represents the first version of your project; subsequently, you will change it accordingly after each update push) </p>
</li>
<li><p><strong>description</strong>: One-line summary of your package.</p>
</li>
<li><p><strong>entry point</strong>: Entry file of your project, usually <code>index.js</code> or <code>dist/index.js</code>.</p>
</li>
<li><p><strong>keywords</strong>: Help others discover your package.</p>
</li>
<li><p><strong>author</strong>: Your name or GitHub handle.</p>
</li>
<li><p><strong>license</strong>: Use MIT, ISC, or another open-source license.</p>
</li>
</ul>
<p>When you’re done, npm will generate a <code>package.json</code> like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"my-awesome-package"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"A demo package for npm publishing"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"keywords"</span>: [<span class="hljs-string">"demo"</span>, <span class="hljs-string">"npm"</span>, <span class="hljs-string">"tutorial"</span>],
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Your Name"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>
}
</code></pre>
<h3 id="heading-step-4-add-a-readmemdhttpreadmemd">Step 4: Add a <a target="_blank" href="http://readme.md">README.md</a></h3>
<p>Explain what your package does and how to use it in a <code>README.md</code> file. The README appears on your package page on <a target="_blank" href="http://npmjs.com">npmjs.com</a>. Example content:</p>
<pre><code class="lang-markdown"><span class="hljs-section"># my-awesome-package</span>

A simple package that says hello.

<span class="hljs-section">## Usage</span>

<span class="hljs-code">```js
const greet = require('my-awesome-package');
console.log(greet('Oliver'));
// Output: Hello, Oliver!</span>
</code></pre>
<h3 id="heading-step-5add-an-npmignore-file">Step 5:Add an .npmignore file</h3>
<p>This will exclude folders like <code>node_modules</code>, <code>dist</code>, or <code>.env</code>, and any other file or folder within the package you don’t wish to publish. Example content:</p>
<pre><code class="lang-bash">node_modules
.env
dist
</code></pre>
<h3 id="heading-step-6-check-if-the-package-name-you-chose-is-available">Step 6: Check if the package name you chose is available</h3>
<p>Before publishing, check if the package name you chose is not already taken. To check, run this command on your terminal:</p>
<pre><code class="lang-bash">npm search my-awesome-package
</code></pre>
<p>Or enter the URL <a target="_blank" href="https://www.npmjs.com/package/my-awesome-package">https://www.npmjs.com/package/my-awesome-package</a> in your browser (replace my-awesome-package with the name you chose). If no package page shows up, the name is not taken.</p>
<p>If the package name has been taken, change it in your <code>package.json</code> and any documentation (<code>README.md</code>), the name is reflected, or publish it under a scope. A scope is like a namespace tied to your npm username or organization. It ensures your package name is unique, even if the base name is common. For example, if my-awesome-package is taken, you can publish under a scope by setting the name section in its <code>package.json</code> like this: </p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"@yourname/my-awesome-package"</span>
}
</code></pre>
<h3 id="heading-step-6-publish-your-package">Step 6: Publish your package</h3>
<p>You are now ready to publish. Run:</p>
<pre><code class="lang-bash">npm publish
</code></pre>
<p>If you used a scoped name, when publishing, you must make it public:</p>
<pre><code class="lang-bash">npm publish --access public
</code></pre>
<p>If everything is valid, npm will publish your package and give you a URL like:</p>
<p><a target="_blank" href="https://www.npmjs.com/package/my-awesome-package">https://www.npmjs.com/package/my-awesome-package</a></p>
<h2 id="heading-how-to-verify-and-install-your-package">How to Verify and Install Your Package</h2>
<p>Visit your project page on npm to see it live. For example: <a target="_blank" href="https://www.npmjs.com/package/my-awesome-package">https://www.npmjs.com/package/my-awesome-package</a>, or search your project name on the npm website search bar.</p>
<p>Now, try installing it in a node project:</p>
<pre><code class="lang-bash">npm install my-awesome-package
</code></pre>
<p>Test out its features and functionalities, depending on what it is built to do.</p>
<h2 id="heading-how-to-update-your-package">How to Update Your Package</h2>
<p>If you make changes or update features in your package, you can publish the update.</p>
<p>After applying your changes, you can update the version in your <code>package.json</code>. Using semantic versioning, you can update it as follows:</p>
<ul>
<li><p>Patch update: 1.0.0 → 1.0.1</p>
</li>
<li><p>Minor update: 1.0.0 → 1.1.0</p>
</li>
<li><p>Major update: 1.0.0 → 2.0.0</p>
</li>
</ul>
<p>Or you can use the CLI:</p>
<pre><code class="lang-bash">npm version &lt;update_type&gt;
</code></pre>
<p>Replace <code>&lt;update_type&gt;</code> with your new semantic version. </p>
<p>Now run:</p>
<pre><code class="lang-bash">npm publish
</code></pre>
<h2 id="heading-notes-and-best-practices">Notes and Best Practices</h2>
<p>Although publishing to npm is straightforward, you still need to follow best practices to maintain a clean, secure, and user-friendly package.</p>
<ul>
<li><p>Exclude Sensitive Files: Never include <code>.env</code>, credentials, or secrets. Use <code>.npmignore</code> or the "files" field in <code>package.json</code> to control what gets published.</p>
</li>
<li><p>Test Before Publishing: Run <code>npm pack</code> to preview the package and install it locally in another project to ensure everything works.</p>
</li>
<li><p>Unpublish Carefully: You can unpublish within 72 hours using <code>npm unpublish --force</code>, but avoid doing this frequently to prevent breaking other projects that rely on your package.</p>
</li>
<li><p>Always Bump the Version: npm won’t let you overwrite a version, so use semantic versioning (npm version patch|minor|major) before publishing updates.</p>
</li>
<li><p>Add Essentials: Include a clear <code>README.md</code> file, a license, and relevant keywords to make your package discoverable and easy to use.</p>
</li>
</ul>
<h2 id="heading-beyond-the-basics">Beyond the Basics</h2>
<p>Now that you've got the hang of the basics, you can put your npm skills to work and level up as a developer. Here are some simple, practical steps you can take:</p>
<h3 id="heading-get-involved-in-open-source">Get Involved in Open Source</h3>
<p>One of the best ways to gain real-world experience is by contributing to existing projects on npm. Check out their repositories on GitHub and GitLab and see how you can contribute to them. This teaches you how to collaborate, handle code reviews, and manage versions. A great way to start is by looking for projects on GitHub with a "good first issue" label.</p>
<h3 id="heading-maintain-your-own-package">Maintain Your Own Package</h3>
<p>Publishing is just the first step. To truly master the process, keep your package up to date. This involves fixing bugs, listening to user feedback, and adding new features. You will quickly learn about versioning, ensuring your package remains compatible with older projects, and effectively manage dependencies.</p>
<h3 id="heading-dig-into-advanced-features">Dig into Advanced Features</h3>
<p>Explore advanced npm topics like semantic versioning, using private packages, creating scoped packages, and automating your releases with CI/CD pipelines. These are essential skills for any professional developer.</p>
<p>Taking these steps will help you transition from merely understanding npm to confidently utilizing it in real-world scenarios.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Congratulations! You have now learned the essentials of npm from the <code>package.json</code> file to publishing and maintaining a JavaScript package with the npm library. With this knowledge, you can share your JavaScript tools with the world, collaborate with other developers, and contribute to the growing ecosystem of open-source libraries.</p>
<p>By following best practices, testing locally, and learning from standard errors, you can confidently create packages that are clean, secure, and useful for other developers. Whether you’re building a small utility or a full-fledged framework, publishing on npm gives your ideas visibility and helps you contribute to the wider JavaScript community.</p>
<p>If you’d like to get hands-on experience collaborating on a real package, I published an npm project called <a target="_blank" href="https://www.npmjs.com/package/route-pilot?activeTab=readme">route-pilot</a>, a powerful CLI tool for testing and analyzing Express.js routes in your Node.js applications. I’m actively seeking contributors who want to enhance the code, add new features, or refine the documentation. It’s a simple way to practice working with npm in a collaborative setting while learning more about open-source development. Head over to the <a target="_blank" href="https://github.com/oliverTwist2/express-route-tester">GitHub repo</a> and join in. We’d love to have you on board!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create an npm Library ]]>
                </title>
                <description>
                    <![CDATA[ In the world of JavaScript development, npm (Node Package Manager) has become an essential tool for managing dependencies and sharing reusable code. Whether you're building a simple website or a complex web application, npm libraries help streamline ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-an-npm-library/</link>
                <guid isPermaLink="false">67a627bfaf028c3f4ba71a05</guid>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Yarn ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ German Cocca ]]>
                </dc:creator>
                <pubDate>Fri, 07 Feb 2025 15:33:19 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1738941301640/7189d889-387d-4bd2-bf5c-2cbcbd17faad.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In the world of JavaScript development, <strong>npm</strong> (Node Package Manager) has become an essential tool for managing dependencies and sharing reusable code. Whether you're building a simple website or a complex web application, npm libraries help streamline development by providing pre-built solutions to common problems.</p>
<p>Another popular package manager is <strong>Yarn</strong>, which offers faster and more reliable dependency management while maintaining compatibility with the npm ecosystem.</p>
<p>In this article, we'll explore what npm libraries are, their benefits, and how they enhance the JavaScript and React ecosystem. We'll also go through a practical step-by-step guide on creating, publishing, and using your own npm library in a React project. We’ll also compare npm and Yarn, showing how you can use either of them effectively in your workflow.</p>
<p>By the end of this tutorial, you'll have a clear understanding of how to package and distribute your own code, making it reusable across multiple projects and even available to the wider developer community.</p>
<h2 id="heading-table-of-contents"><strong>Table of Contents</strong></h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-what-is-npm">What is npm?</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-npm-works">How npm works</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-role-of-packagejson">The role of the <code>package.json</code> file</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-key-npm-commands">Key npm Commands</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-why-use-npm-libraries">Why Use npm Libraries?</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-code-reuse-and-modularization">Code reuse and modularization</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-simplified-dependency-management">Simplified dependency management</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-community-driven-ecosystem">Community-driven ecosystem</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-introducing-yarn-an-alternative-to-npm">Introducing Yarn: An Alternative to npm</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-yarn">What is Yarn?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-differences-between-npm-and-yarn">Differences between npm and Yarn</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-when-to-use-yarn-instead-of-npm">When to use Yarn instead of npm</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-create-your-own-npm-library">How to Create Your Own npm Library</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-setting-up-a-new-package">Setting up a new package</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-writing-modular-and-reusable-code">Writing modular and reusable code</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-adding-dependencies-and-peer-dependencies">Adding dependencies and peer dependencies</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-publish-your-library-to-npm">How to Publish Your Library to npm</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-creating-an-npm-account">Creating an npm account</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-configuring-packagejson-for-publishing">Configuring package.json for Publishing</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-publishing-the-package">Publishing the package</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-your-npm-library-in-a-react-project">How to Use Your npm Library in a React Project</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-installing-your-package">Installing your package</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-importing-and-using-the-library-in-a-react-component">Importing and using the package in a React component</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-handling-package-updates-and-versioning">Handling package updates and versioning</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-best-practices-for-npm-and-yarn-libraries">Best Practices for npm and Yarn Libraries</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-write-meaningful-documentation">Write Meaningful Documentation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-follow-semantic-versioning-semver">Follow Semantic Versioning (SemVer)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-keep-dependencies-up-to-date">Keep Dependencies Up to Date</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-write-unit-tests-for-your-library">Write Unit Tests for Your Library</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-ensure-cross-platform-compatibility">Ensure Cross-Platform Compatibility</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion &amp; Next Steps</a></p>
<ul>
<li><p><a class="post-section-overview" href="#heading-recap-of-key-points">Recap of key points</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-additional-resources">Additional resources for npm and Yarn library development</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-encouragement-to-contribute-to-open-source">Encouragement to contribute to open-source</a></p>
</li>
</ul>
</li>
</ol>
<h2 id="heading-what-is-npm"><strong>What is npm?</strong></h2>
<p>npm (Node Package Manager) is the default package manager for JavaScript and Node.js. It allows developers to install, share, and manage libraries or dependencies that make building applications easier and more efficient.</p>
<p>npm provides access to a vast ecosystem of open-source packages hosted on the <strong>npm registry</strong>, making it one of the largest software repositories in the world.</p>
<p>npm comes bundled with <strong>Node.js</strong>, meaning that once you install Node.js, you automatically have access to npm. You can check if npm is installed by running the following command in your terminal:</p>
<pre><code class="lang-python">npm -v
</code></pre>
<p>This command should return the version of npm installed on your system.</p>
<h3 id="heading-how-npm-works"><strong>How npm Works</strong></h3>
<p>npm operates through three key components:</p>
<ol>
<li><p><strong>The npm Registry</strong> – A public repository that hosts open-source JavaScript packages.</p>
</li>
<li><p><strong>The npm CLI (Command Line Interface)</strong> – A tool that allows developers to install, update, and manage packages from the command line.</p>
</li>
<li><p><strong>The package.json File</strong> – A metadata file that keeps track of dependencies, scripts, and project configurations.</p>
</li>
</ol>
<p>When you install a package using npm, it pulls the package from the registry and saves it in the <code>node_modules</code> folder within your project.</p>
<p>For example, to install <strong>Lodash</strong>, a popular utility library, you would run:</p>
<pre><code class="lang-python">npm install lodash
</code></pre>
<p>This will:</p>
<ul>
<li><p>Download the latest version of <code>lodash</code> from the npm registry</p>
</li>
<li><p>Add it to your <code>node_modules</code> folder</p>
</li>
<li><p>Update the <code>package.json</code> and <code>package-lock.json</code> files to reflect the new dependency</p>
</li>
</ul>
<h3 id="heading-the-role-of-packagejson"><strong>The Role of</strong> <code>package.json</code></h3>
<p>The <code>package.json</code> file is the heart of any npm project. It serves as a blueprint, containing information about the project, including:</p>
<ul>
<li><p><strong>Project metadata</strong> (name, version, description)</p>
</li>
<li><p><strong>Dependencies</strong> (external packages required for the project)</p>
</li>
<li><p><strong>Scripts</strong> (commands to automate tasks like starting a server or running tests)</p>
</li>
<li><p><strong>Versioning information</strong> (ensuring compatibility between different versions of dependencies)</p>
</li>
</ul>
<p>A typical <code>package.json</code> file looks like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"my-awesome-project"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"A sample project demonstrating npm usage"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"start"</span>: <span class="hljs-string">"node index.js"</span>,
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"echo \"No tests specified\" &amp;&amp; exit 0"</span>
  },
  <span class="hljs-attr">"dependencies"</span>: {
    <span class="hljs-attr">"lodash"</span>: <span class="hljs-string">"^4.17.21"</span>
  },
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"eslint"</span>: <span class="hljs-string">"^8.0.0"</span>
  },
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Your Name"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>
}
</code></pre>
<ul>
<li><p><code>dependencies</code> – Lists essential packages required for the application to function.</p>
</li>
<li><p><code>devDependencies</code> – Includes development-only dependencies (for example, testing and linting tools).</p>
</li>
<li><p><code>scripts</code> – Defines CLI commands for automating tasks.</p>
</li>
</ul>
<p>To install all dependencies listed in <code>package.json</code>, simply run:</p>
<pre><code class="lang-python">npm install
</code></pre>
<p>This ensures all required packages are downloaded and ready for use.</p>
<h3 id="heading-key-npm-commands"><strong>Key npm Commands</strong></h3>
<p>Here are some essential npm commands you’ll use frequently:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Command</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td><code>npm init -y</code></td><td>Creates a default <code>package.json</code> file</td></tr>
<tr>
<td><code>npm install &lt;package-name&gt;</code></td><td>Installs a package and adds it to <code>dependencies</code></td></tr>
<tr>
<td><code>npm install &lt;package-name&gt; --save-dev</code></td><td>Installs a package and adds it to <code>devDependencies</code></td></tr>
<tr>
<td><code>npm uninstall &lt;package-name&gt;</code></td><td>Removes a package from the project</td></tr>
<tr>
<td><code>npm update</code></td><td>Updates all installed dependencies</td></tr>
<tr>
<td><code>npm outdated</code></td><td>Checks for outdated dependencies</td></tr>
<tr>
<td><code>npm run &lt;script-name&gt;</code></td><td>Runs a script defined in <code>package.json</code></td></tr>
</tbody>
</table>
</div><h2 id="heading-why-use-npm-libraries"><strong>Why Use npm Libraries?</strong></h2>
<p>As modern web development grows in complexity, using npm libraries has become essential for building scalable and maintainable applications. Instead of writing everything from scratch, you can leverage pre-built, tested, and optimized libraries to speed up development and ensure reliability.</p>
<p>In this section, we’ll explore the key advantages of using npm libraries and why they are crucial in JavaScript and React development.</p>
<h3 id="heading-code-reuse-and-modularization"><strong>Code Reuse and Modularization</strong></h3>
<p>One of the biggest benefits of npm libraries is <strong>code reuse</strong>. Instead of repeatedly writing the same functions or utilities in different projects, developers can:</p>
<ul>
<li><p>✅ Use existing open-source packages for common functionalities (for example, date formatting, HTTP requests, UI components).</p>
</li>
<li><p>✅ Create and publish their own reusable libraries to share across multiple projects.</p>
</li>
</ul>
<p>For example, instead of manually implementing a function to format dates, you can install a well-maintained package like date-fns:</p>
<pre><code class="lang-python">npm install date-fns
</code></pre>
<p>Then, you can use it in your project:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { format } <span class="hljs-keyword">from</span> <span class="hljs-string">"date-fns"</span>;

<span class="hljs-keyword">const</span> formattedDate = format(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(), <span class="hljs-string">"yyyy-MM-dd"</span>);
<span class="hljs-built_in">console</span>.log(formattedDate); <span class="hljs-comment">// Outputs: 2024-02-04 (or the current date)</span>
</code></pre>
<p>This modular approach saves time and ensures consistency across projects.</p>
<h3 id="heading-simplified-dependency-management"><strong>Simplified Dependency Management</strong></h3>
<p>npm makes it easy to manage dependencies in a project. Instead of manually downloading and maintaining different versions of external libraries, npm automates this process through the package.json and package-lock.json files.</p>
<p>Some key features include:</p>
<p>🔹 <strong>Automatic installation</strong> – Run <code>npm install</code>, and all dependencies are set up.<br>🔹 <strong>Version control</strong> – Specify package versions to avoid breaking changes.<br>🔹 <strong>Peer dependencies</strong> – Ensure compatibility between different libraries.</p>
<p>For example, here’s how npm helps manage dependency versions in <code>package.json</code>:</p>
<pre><code class="lang-json"><span class="hljs-string">"dependencies"</span>: {
  <span class="hljs-attr">"react"</span>: <span class="hljs-string">"^18.0.0"</span>,
  <span class="hljs-attr">"axios"</span>: <span class="hljs-string">"^1.5.0"</span>
}
</code></pre>
<ul>
<li><p><code>^18.0.0</code> – Allows minor updates but prevents major breaking changes.</p>
</li>
<li><p><code>axios</code> – Ensures HTTP requests are handled consistently across different projects.</p>
</li>
</ul>
<p>To update all dependencies safely, run:</p>
<pre><code class="lang-python">npm update
</code></pre>
<p>This ensures your project is always running on the latest stable versions.</p>
<h3 id="heading-community-driven-ecosystem"><strong>Community-Driven Ecosystem</strong></h3>
<p>npm has an active and growing community, meaning developers around the world contribute and maintain thousands of useful libraries. This results in:</p>
<p>🌎 <strong>Faster development</strong> – No need to reinvent the wheel.<br>🛠️ <strong>Well-tested solutions</strong> – Many libraries are battle-tested in production environments.<br>📚 <strong>Rich documentation</strong> – Most npm packages come with clear usage instructions and examples.</p>
<p>Popular npm libraries include:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Library</td><td>Purpose</td></tr>
</thead>
<tbody>
<tr>
<td><strong>React</strong> (<code>react</code>)</td><td>UI library for building web applications</td></tr>
<tr>
<td><strong>Axios</strong> (<code>axios</code>)</td><td>HTTP client for making API requests</td></tr>
<tr>
<td><strong>Lodash</strong> (<code>lodash</code>)</td><td>Utility functions for working with arrays, objects, and strings</td></tr>
<tr>
<td><strong>Express</strong> (<code>express</code>)</td><td>Web framework for building backend services</td></tr>
<tr>
<td><strong>Jest</strong> (<code>jest</code>)</td><td>JavaScript testing framework</td></tr>
</tbody>
</table>
</div><p>For example, using <strong>Axios</strong> to make an API request:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">"axios"</span>;

axios.get(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts/1"</span>)
  .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(response.data))
  .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(error));
</code></pre>
<p>This replaces the need for writing complex <code>fetch</code> requests with error handling manually.</p>
<h2 id="heading-introducing-yarn-an-alternative-to-npm"><strong>Introducing Yarn: An Alternative to npm</strong></h2>
<p>While <strong>npm</strong> is the default package manager for Node.js, another powerful alternative exists: <strong>Yarn</strong>. Developed by Facebook in 2016, Yarn was created to improve speed, security, and reliability in dependency management.</p>
<p>In this section, we’ll explore what Yarn is, how it differs from npm, and when you might prefer using Yarn over npm.</p>
<h3 id="heading-what-is-yarn"><strong>What is Yarn?</strong></h3>
<p>Yarn (<strong>Yet Another Resource Negotiator</strong>) is a package manager that works similarly to npm but with a focus on performance, security, and consistency. It offers:</p>
<p>🚀 <strong>Faster dependency installation</strong> thanks to parallel downloads<br>🔐 <strong>More secure package management</strong> using checksum verification<br>📦 <strong>Reliable dependency resolution</strong> with an offline cache</p>
<p>To check if you have Yarn installed, run:</p>
<pre><code class="lang-python">yarn -v
</code></pre>
<p>If you don’t have it yet, you can install it globally using npm:</p>
<pre><code class="lang-python">npm install --<span class="hljs-keyword">global</span> yarn
</code></pre>
<p>Once installed, you can use it just like npm to manage dependencies.</p>
<h3 id="heading-differences-between-npm-and-yarn"><strong>Differences Between npm and Yarn</strong></h3>
<p>Although npm and Yarn serve the same purpose, they have some key differences:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td>npm</td><td>Yarn</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Speed</strong></td><td>Installs packages one at a time</td><td>Installs multiple packages in parallel (faster)</td></tr>
<tr>
<td><strong>Lock File</strong></td><td><code>package-lock.json</code></td><td><code>yarn.lock</code></td></tr>
<tr>
<td><strong>Offline Cache</strong></td><td>Not available (by default)</td><td>Can install packages from local cache</td></tr>
<tr>
<td><strong>Security</strong></td><td>Verifies package integrity but lacks checksum enforcement</td><td>Uses checksum verification for security</td></tr>
<tr>
<td><strong>Monorepo Support</strong></td><td>Supports workspaces but not optimized</td><td>Built-in support for monorepos with <code>workspaces</code></td></tr>
</tbody>
</table>
</div><h4 id="heading-performance-comparison"><strong>Performance Comparison</strong></h4>
<p>When installing dependencies, Yarn is often faster because it downloads packages in parallel, while npm installs them sequentially.</p>
<p>For example, to install all dependencies in a project:</p>
<pre><code class="lang-python"><span class="hljs-comment"># With npm</span>
npm install

<span class="hljs-comment"># With Yarn</span>
yarn install
</code></pre>
<p>Yarn can also install packages from a local cache, meaning it doesn't always need to fetch dependencies from the internet.</p>
<h4 id="heading-common-yarn-commands-vs-npm"><strong>Common Yarn Commands vs. npm</strong></h4>
<p>Many npm commands have an equivalent in Yarn:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Action</td><td>npm Command</td><td>Yarn Command</td></tr>
</thead>
<tbody>
<tr>
<td>Initialize a new project</td><td><code>npm init</code></td><td><code>yarn init</code></td></tr>
<tr>
<td>Install all dependencies</td><td><code>npm install</code></td><td><code>yarn install</code></td></tr>
<tr>
<td>Install a package</td><td><code>npm install package-name</code></td><td><code>yarn add package-name</code></td></tr>
<tr>
<td>Install a dev dependency</td><td><code>npm install package-name --save-dev</code></td><td><code>yarn add package-name --dev</code></td></tr>
<tr>
<td>Remove a package</td><td><code>npm uninstall package-name</code></td><td><code>yarn remove package-name</code></td></tr>
<tr>
<td>Update all packages</td><td><code>npm update</code></td><td><code>yarn upgrade</code></td></tr>
<tr>
<td>Run a script</td><td><code>npm run script-name</code></td><td><code>yarn script-name</code></td></tr>
</tbody>
</table>
</div><p>For example, installing <code>axios</code> using Yarn:</p>
<pre><code class="lang-python">yarn add axios
</code></pre>
<h3 id="heading-when-to-use-yarn-instead-of-npm"><strong>When to Use Yarn Instead of npm</strong></h3>
<p>Yarn is a great choice when:</p>
<ul>
<li><p><strong>You want faster installations</strong> – Yarn installs multiple packages in parallel, making it faster than npm.</p>
</li>
<li><p><strong>You need better dependency consistency</strong> – The <code>yarn.lock</code> file ensures that all developers use the same dependency versions.</p>
</li>
<li><p><strong>You're working with monorepos</strong> – Yarn’s built-in <strong>workspaces</strong> make it easier to manage multiple projects within the same repository.</p>
</li>
<li><p><strong>You want improved security</strong> – Yarn’s checksum verification prevents corrupted packages from being installed.</p>
</li>
</ul>
<p>Still, npm has improved significantly in recent years, especially with npm v7+, making it a viable choice for most projects.</p>
<h4 id="heading-switching-between-npm-and-yarn"><strong>Switching Between npm and Yarn</strong></h4>
<p>If your project was originally set up using npm but you want to switch to Yarn, you can:</p>
<p>1️⃣ <strong>Delete</strong> <code>node_modules</code> and <code>package-lock.json</code></p>
<pre><code class="lang-python">rm -rf node_modules package-lock.json
</code></pre>
<p>2️⃣ <strong>Run Yarn to install dependencies</strong></p>
<pre><code class="lang-python">yarn install
</code></pre>
<p>This will generate a yarn.lock file, ensuring all dependencies are managed by Yarn moving forward.</p>
<p>Both npm and Yarn are powerful tools for package management. Choosing between them depends on your project’s needs:</p>
<p>✔️ Use <strong>npm</strong> if you want the default, widely used package manager that works well with most projects.<br>✔️ Use <strong>Yarn</strong> if you need faster installs, better security, and monorepo support.</p>
<p>Ultimately, both tools allow you to <strong>install, manage, and publish</strong> JavaScript packages efficiently.</p>
<h2 id="heading-how-to-create-your-own-npm-library"><strong>How to Create Your Own npm Library</strong></h2>
<p>Creating your own npm library is a great way to <strong>share reusable code</strong>, contribute to the open-source community, or even streamline development across multiple projects. In this section, we’ll walk through the step-by-step process of setting up, coding, and preparing a library for publishing on npm.</p>
<h3 id="heading-setting-up-a-new-package"><strong>Setting Up a New Package</strong></h3>
<p>Before writing code, you need to set up an npm package. Follow these steps:</p>
<h4 id="heading-step-1-create-a-new-project-folder"><strong>Step 1: Create a New Project Folder</strong></h4>
<pre><code class="lang-python">mkdir my-awesome-library
cd my-awesome-library
</code></pre>
<h4 id="heading-step-2-initialize-npm"><strong>Step 2: Initialize npm</strong></h4>
<p>Run the following command to create a <code>package.json</code> file:</p>
<pre><code class="lang-python">npm init
</code></pre>
<p>You will be prompted to enter details such as:</p>
<ul>
<li><p>Package name</p>
</li>
<li><p>Version</p>
</li>
<li><p>Description</p>
</li>
<li><p>Entry point (default: <code>index.js</code>)</p>
</li>
<li><p>Author</p>
</li>
<li><p>License</p>
</li>
</ul>
<p>💡 To skip the prompts and create a default <code>package.json</code>, use:</p>
<pre><code class="lang-python">npm init -y
</code></pre>
<h3 id="heading-writing-modular-and-reusable-code"><strong>Writing Modular and Reusable Code</strong></h3>
<p>Now, let’s create a simple utility library that provides a function to format dates.</p>
<h4 id="heading-step-3-create-an-indexjs-file"><strong>Step 3: Create an</strong> <code>index.js</code> File</h4>
<p>Inside the project folder, create a file named <code>index.js</code> and add the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">formatDate</span>(<span class="hljs-params">date</span>) </span>{
  <span class="hljs-keyword">if</span> (!(date <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Date</span>)) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Invalid date"</span>);
  }
  <span class="hljs-keyword">return</span> date.toISOString().split(<span class="hljs-string">"T"</span>)[<span class="hljs-number">0</span>];
}

<span class="hljs-built_in">module</span>.exports = { formatDate };
</code></pre>
<h3 id="heading-adding-dependencies-and-peer-dependencies"><strong>Adding Dependencies and Peer Dependencies</strong></h3>
<p>Your library might depend on external packages. For example, let’s use date-fns for better date formatting.</p>
<p>To install it as a dependency, run:</p>
<pre><code class="lang-python">npm install date-fns
</code></pre>
<p>Then, modify <code>index.js</code> to use <code>date-fns</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { format } = <span class="hljs-built_in">require</span>(<span class="hljs-string">"date-fns"</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">formatDate</span>(<span class="hljs-params">date</span>) </span>{
  <span class="hljs-keyword">if</span> (!(date <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Date</span>)) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">"Invalid date"</span>);
  }
  <span class="hljs-keyword">return</span> format(date, <span class="hljs-string">"yyyy-MM-dd"</span>);
}

<span class="hljs-built_in">module</span>.exports = { formatDate };
</code></pre>
<p>If you're creating a React-specific library, you should add React as a peer dependency:</p>
<pre><code class="lang-python">npm install react --save-peer
</code></pre>
<p>This ensures users of your library install React separately, preventing version conflicts.</p>
<p>Before publishing, you should test how your package works when installed as a dependency.</p>
<h4 id="heading-step-4-link-the-package-locally"><strong>Step 4: Link the Package Locally</strong></h4>
<p>Run the following command in your package folder:</p>
<pre><code class="lang-python">npm link
</code></pre>
<p>Then, in another project where you want to use your package, navigate to that project and run:</p>
<pre><code class="lang-python">npm link my-awesome-library
</code></pre>
<p>Now, you can import and use your function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { formatDate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">"my-awesome-library"</span>);

<span class="hljs-built_in">console</span>.log(formatDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>())); <span class="hljs-comment">// Output: 2025-02-04 (or the current date)</span>
</code></pre>
<p>Once you're happy with your package, it's time to <strong>publish it on npm</strong>.</p>
<h2 id="heading-how-to-publish-your-library-to-npm"><strong>How to Publish Your Library to npm</strong></h2>
<p>Now that we’ve created our npm package, the next step is <strong>publishing it to the npm registry</strong> so others can install and use it. In this section, we’ll cover how to publish the package step by step.</p>
<h3 id="heading-creating-an-npm-account"><strong>Creating an npm Account</strong></h3>
<p>Before publishing, you need an npm account.</p>
<h4 id="heading-step-1-sign-up-for-npm"><strong>Step 1: Sign Up for npm</strong></h4>
<ol>
<li><p>Go to <a target="_blank" href="https://www.npmjs.com/signup">https://www.npmjs.com/signup</a> and create an account.</p>
</li>
<li><p>Verify your email address.</p>
</li>
</ol>
<h4 id="heading-step-2-log-in-to-npm-from-the-terminal"><strong>Step 2: Log in to npm from the Terminal</strong></h4>
<p>Run the following command in your terminal:</p>
<pre><code class="lang-python">npm login
</code></pre>
<p>You will be prompted to enter:</p>
<ul>
<li><p>Your npm username</p>
</li>
<li><p>Your password</p>
</li>
<li><p>Your email (associated with your npm account)</p>
</li>
</ul>
<p>If the login is successful, you’ll see a message:</p>
<pre><code class="lang-python">Logged <span class="hljs-keyword">in</span> <span class="hljs-keyword">as</span> your-username on https://registry.npmjs.org/
</code></pre>
<h3 id="heading-configuring-packagejson-for-publishing"><strong>Configuring package.json for Publishing</strong></h3>
<h4 id="heading-step-3-ensure-your-package-name-is-unique"><strong>Step 3: Ensure Your Package Name is Unique</strong></h4>
<p>Every npm package needs a unique name. Run the following command to check if your desired name is available:</p>
<pre><code class="lang-python">npm search my-awesome-library
</code></pre>
<p>If the name is already taken, you’ll need to modify <code>package.json</code> and change the <code>"name"</code> field.</p>
<h4 id="heading-step-4-add-metadata-and-keywords"><strong>Step 4: Add Metadata and Keywords</strong></h4>
<p>Open <code>package.json</code> and ensure it includes useful metadata:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"my-awesome-library"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"A simple npm package for formatting dates"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"repository"</span>: {
    <span class="hljs-attr">"type"</span>: <span class="hljs-string">"git"</span>,
    <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://github.com/yourusername/my-awesome-library.git"</span>
  },
  <span class="hljs-attr">"keywords"</span>: [<span class="hljs-string">"date"</span>, <span class="hljs-string">"formatter"</span>, <span class="hljs-string">"utility"</span>, <span class="hljs-string">"npm package"</span>],
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Your Name &lt;your-email@example.com&gt;"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>
}
</code></pre>
<p>🔹 <strong>repository</strong> – Useful if you plan to host the project on GitHub.<br>🔹 <strong>keywords</strong> – Helps people discover your package on npm.<br>🔹 <strong>license</strong> – Specifies how others can use your package (for example, MIT, GPL, and so on).</p>
<h3 id="heading-publishing-the-package"><strong>Publishing the Package</strong></h3>
<h4 id="heading-step-5-publish-your-package-to-npm"><strong>Step 5: Publish Your Package to npm</strong></h4>
<p>Run the following command inside your project folder:</p>
<pre><code class="lang-python">npm publish
</code></pre>
<p>If successful, you’ll see output similar to:</p>
<pre><code class="lang-python">+ my-awesome-library@<span class="hljs-number">1.0</span><span class="hljs-number">.0</span>
</code></pre>
<p>Your package is now available at:</p>
<p>📌 <a target="_blank" href="https://www.npmjs.com/package/my-awesome-library"><strong>https://www.npmjs.com/package/my-awesome-library</strong></a></p>
<h4 id="heading-step-6-making-changes-and-updating-the-package"><strong>Step 6: Making Changes and Updating the Package</strong></h4>
<p>If you want to release a new version, update the <code>version</code> field in <code>package.json</code>. npm follows Semantic Versioning (SemVer):</p>
<ul>
<li><p><strong>Patch:</strong> Bug fixes (1.0.0 → 1.0.1)</p>
</li>
<li><p><strong>Minor:</strong> New features, backward-compatible (1.0.0 → 1.1.0)</p>
</li>
<li><p><strong>Major:</strong> Breaking changes (1.0.0 → 2.0.0)</p>
</li>
</ul>
<p>Instead of manually updating <code>package.json</code>, use:</p>
<pre><code class="lang-python">npm version patch   <span class="hljs-comment"># 1.0.0 → 1.0.1</span>
npm version minor   <span class="hljs-comment"># 1.0.0 → 1.1.0</span>
npm version major   <span class="hljs-comment"># 1.0.0 → 2.0.0</span>
</code></pre>
<p>Then, publish the new version:</p>
<pre><code class="lang-python">npm publish
</code></pre>
<p>If you accidentally publish a package and need to remove it:</p>
<pre><code class="lang-python">npm unpublish my-awesome-library --force
</code></pre>
<p>⚠️ <strong>Note:</strong> You can only unpublish packages <strong>within 72 hours</strong> of publishing.</p>
<p>🎯 <strong>You’ve successfully published your own npm library!</strong> Now, other developers can install it using:</p>
<pre><code class="lang-python">npm install my-awesome-library
</code></pre>
<p>By following Semantic Versioning, writing clear documentation, and maintaining your package, you contribute to the open-source ecosystem and make your code reusable.</p>
<h2 id="heading-how-to-use-your-npm-library-in-a-react-project"><strong>How to Use Your npm Library in a React Project</strong></h2>
<p>Now that we’ve published our npm package, let’s see how to install, import, and use it inside a React project created with <strong>Vite</strong>. This section will guide you through the process using both npm and Yarn.</p>
<h3 id="heading-installing-your-package"><strong>Installing Your Package</strong></h3>
<h4 id="heading-step-1-create-a-new-react-project-with-vite-if-needed"><strong>Step 1: Create a New React Project with Vite (if needed)</strong></h4>
<p>If you don’t have an existing React project, create one using Vite:</p>
<h4 id="heading-using-npm"><strong>Using npm</strong></h4>
<pre><code class="lang-python">npm create vite@latest my-react-app --template react
cd my-react-app
npm install
</code></pre>
<h4 id="heading-using-yarn"><strong>Using Yarn</strong></h4>
<pre><code class="lang-python">yarn create vite@latest my-react-app --template react
cd my-react-app
yarn install
</code></pre>
<p>Once the installation is complete, you can start the development server:</p>
<pre><code class="lang-python">npm run dev
</code></pre>
<p>or</p>
<pre><code class="lang-python">yarn dev
</code></pre>
<h4 id="heading-step-2-install-your-npm-package"><strong>Step 2: Install Your npm Package</strong></h4>
<p>Now, install the npm library we created earlier (<code>my-awesome-library</code>).</p>
<h4 id="heading-using-npm-1"><strong>Using npm</strong></h4>
<pre><code class="lang-python">npm install my-awesome-library
</code></pre>
<h4 id="heading-using-yarn-1"><strong>Using Yarn</strong></h4>
<pre><code class="lang-python">yarn add my-awesome-library
</code></pre>
<h3 id="heading-importing-and-using-the-library-in-a-react-component"><strong>Importing and Using the Library in a React Component</strong></h3>
<p>Once installed, you can use the library inside a React component.</p>
<p>Open <code>src/App.jsx</code> and modify it as follows:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { formatDate } <span class="hljs-keyword">from</span> <span class="hljs-string">"my-awesome-library"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> today = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Formatted Date<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{formatDate(today)}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>Now, run your Vite React app:</p>
<pre><code class="lang-python">npm run dev
</code></pre>
<p>Or with Yarn:</p>
<pre><code class="lang-python">yarn dev
</code></pre>
<p>This will display a formatted date on the webpage, confirming that our library is working!</p>
<h3 id="heading-handling-package-updates-and-versioning"><strong>Handling Package Updates and Versioning</strong></h3>
<p>To update your npm package in your project:</p>
<h4 id="heading-using-npm-2"><strong>Using npm</strong></h4>
<pre><code class="lang-python">npm update my-awesome-library
</code></pre>
<h4 id="heading-using-yarn-2"><strong>Using Yarn</strong></h4>
<pre><code class="lang-python">yarn upgrade my-awesome-library
</code></pre>
<p>If you want to check outdated dependencies:</p>
<pre><code class="lang-python">npm outdated
</code></pre>
<p>or</p>
<pre><code class="lang-python">yarn outdated
</code></pre>
<h3 id="heading-using-a-local-version-of-your-package-in-development"><strong>Using a Local Version of Your Package in Development</strong></h3>
<p>If you’re still making changes to your npm package and want to test it in your React project <strong>before publishing</strong>, you can use <code>npm link</code> or <code>yarn link</code>.</p>
<h4 id="heading-step-1-link-your-package-locally"><strong>Step 1: Link Your Package Locally</strong></h4>
<p>Go to your package’s project folder:</p>
<pre><code class="lang-python">cd ~/path-to-my-awesome-library
npm link
</code></pre>
<p>or</p>
<pre><code class="lang-python">yarn link
</code></pre>
<h4 id="heading-step-2-use-it-in-your-react-project"><strong>Step 2: Use It in Your React Project</strong></h4>
<p>Navigate to your React app and link the package:</p>
<pre><code class="lang-python">cd ~/path-to-my-react-app
npm link my-awesome-library
</code></pre>
<p>or</p>
<pre><code class="lang-python">yarn link my-awesome-library
</code></pre>
<p>Now, when you import and use <code>my-awesome-library</code>, it will use the local version instead of the published one.</p>
<h3 id="heading-publishing-an-update-to-your-package"><strong>Publishing an Update to Your Package</strong></h3>
<p>If you’ve made changes to your package and want to publish a new version:</p>
<p>1️⃣ <strong>Update the version number</strong> in <code>package.json</code> (use <code>npm version patch</code> for small updates).<br>2️⃣ <strong>Run</strong> <code>npm publish</code> to upload the new version.<br>3️⃣ <strong>Run</strong> <code>npm update my-awesome-library</code> in your React project to get the latest version.</p>
<h3 id="heading-final-thoughts-on-using-npm-libraries-in-react-vite-edition"><strong>Final Thoughts on Using npm Libraries in React (Vite Edition)</strong></h3>
<p>By now, you should have a fully functional npm package and know how to install, use, and update it in a React project using Vite.</p>
<p>✔️ Vite is faster than Create React App and provides better performance for development.<br>✔️ npm and Yarn make dependency management easy.<br>✔️ <code>npm link</code> allows local testing before publishing.<br>✔️ Keeping dependencies updated ensures stability.</p>
<p>This workflow is essential for developers looking to create, maintain, and distribute reusable React components or JavaScript utilities.</p>
<h2 id="heading-best-practices-for-npm-and-yarn-libraries"><strong>Best Practices for npm and Yarn Libraries</strong></h2>
<p>Now that you've created, published, and used your own npm package, it's essential to follow best practices to ensure your package is reliable, maintainable, and easy to use. This section will cover key principles and techniques to make your npm library as professional as possible.</p>
<h3 id="heading-write-meaningful-documentation"><strong>Write Meaningful Documentation</strong></h3>
<p>A well-documented library helps other developers understand how to use it effectively.</p>
<h4 id="heading-what-to-include-in-your-documentation"><strong>What to Include in Your Documentation</strong></h4>
<p>📌 Installation instructions<br>📌 Usage examples<br>📌 API reference (functions, parameters, return values)<br>📌 Versioning and update history<br>📌 Contributions guide (if open-source)</p>
<p>For example, a simple <a target="_blank" href="http://README.md"><code>README.md</code></a> file for my-awesome-library:</p>
<pre><code class="lang-python"><span class="hljs-comment"># my-awesome-library</span>

A simple npm package <span class="hljs-keyword">for</span> formatting dates.

<span class="hljs-comment">## Installation</span>

<span class="hljs-comment">### Using npm</span>
```sh
npm install my-awesome-library
</code></pre>
<h4 id="heading-using-yarn-3">Using Yarn</h4>
<pre><code class="lang-python">yarn add my-awesome-library
</code></pre>
<h4 id="heading-usage">Usage</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { formatDate } <span class="hljs-keyword">from</span> <span class="hljs-string">"my-awesome-library"</span>;

<span class="hljs-built_in">console</span>.log(formatDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>())); <span class="hljs-comment">// Outputs: 2025-02-04</span>
</code></pre>
<h3 id="heading-follow-semantic-versioning-semver"><strong>Follow Semantic Versioning (SemVer)</strong></h3>
<p>Versioning helps maintain compatibility and informs users of changes. npm follows Semantic Versioning (SemVer):</p>
<p>MAJOR.MINOR.PATCH</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Change Type</td><td>Example</td><td>Meaning</td></tr>
</thead>
<tbody>
<tr>
<td><strong>Patch</strong></td><td><code>1.0.0 → 1.0.1</code></td><td>Bug fixes, no breaking changes</td></tr>
<tr>
<td><strong>Minor</strong></td><td><code>1.0.0 → 1.1.0</code></td><td>New features, no breaking changes</td></tr>
<tr>
<td><strong>Major</strong></td><td><code>1.0.0 → 2.0.0</code></td><td>Breaking changes</td></tr>
</tbody>
</table>
</div><p>💡 To bump versions automatically, use:</p>
<pre><code class="lang-javascript"><span class="hljs-string">``</span><span class="hljs-string">`sh
npm version patch   # Small bug fix
npm version minor   # New feature added
npm version major   # Breaking changes</span>
</code></pre>
<p>Then, publish the new version:</p>
<pre><code class="lang-javascript">npm publish
</code></pre>
<p>👉 Use proper versioning to prevent breaking projects that depend on your library.</p>
<h3 id="heading-keep-dependencies-up-to-date"><strong>Keep Dependencies Up to Date</strong></h3>
<p>Regularly updating dependencies improves security, performance, and compatibility.</p>
<h4 id="heading-check-for-outdated-dependencies"><strong>Check for outdated dependencies:</strong></h4>
<pre><code class="lang-javascript">npm outdated
</code></pre>
<p>or</p>
<pre><code class="lang-javascript">yarn outdated
</code></pre>
<h4 id="heading-update-dependencies"><strong>Update dependencies:</strong></h4>
<pre><code class="lang-javascript">npm update
</code></pre>
<p>or</p>
<pre><code class="lang-javascript">yarn upgrade
</code></pre>
<h3 id="heading-write-unit-tests-for-your-library"><strong>Write Unit Tests for Your Library</strong></h3>
<p>Testing ensures your package works correctly before publishing updates.</p>
<h4 id="heading-install-a-testing-framework-jest"><strong>Install a Testing Framework (Jest)</strong></h4>
<pre><code class="lang-javascript">npm install --save-dev jest
</code></pre>
<h4 id="heading-create-a-test-file-indextestjs"><strong>Create a Test File (</strong><code>index.test.js</code>)</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { formatDate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./index"</span>);

test(<span class="hljs-string">"formats a date correctly"</span>, <span class="hljs-function">() =&gt;</span> {
  expect(formatDate(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>(<span class="hljs-string">"2025-02-04"</span>))).toBe(<span class="hljs-string">"2025-02-04"</span>);
});

test(<span class="hljs-string">"throws an error if input is not a date"</span>, <span class="hljs-function">() =&gt;</span> {
  expect(<span class="hljs-function">() =&gt;</span> formatDate(<span class="hljs-string">"not a date"</span>)).toThrow(<span class="hljs-string">"Invalid date"</span>);
});
</code></pre>
<h4 id="heading-run-tests"><strong>Run Tests</strong></h4>
<pre><code class="lang-javascript">shCopyEditnpm test
</code></pre>
<p>👉 You can use CI/CD (for example, GitHub Actions) to run tests automatically on every push.</p>
<h3 id="heading-using-cicd-for-automated-publishing"><strong>Using CI/CD for Automated Publishing</strong></h3>
<h4 id="heading-automate-publishing-with-github-actions"><strong>Automate Publishing with GitHub Actions</strong></h4>
<p>Create a <code>.github/workflows/publish.yml</code> file:</p>
<pre><code class="lang-javascript">ymlCopyEditname: Publish to npm
<span class="hljs-attr">on</span>:
  push:
    branches:
      - main

<span class="hljs-attr">jobs</span>:
  publish:
    runs-on: ubuntu-latest
    <span class="hljs-attr">steps</span>:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        <span class="hljs-attr">with</span>:
          node-version: <span class="hljs-number">18</span>
          registry-url: <span class="hljs-string">"https://registry.npmjs.org/"</span>
      - run: npm install
      - run: npm test
      - run: npm publish
        <span class="hljs-attr">env</span>:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
</code></pre>
<p>1️⃣ <strong>Create an npm token</strong>:<br>Run:</p>
<pre><code class="lang-javascript">shCopyEditnpm token create
</code></pre>
<p>Copy the token and add it to GitHub Secrets (<code>NPM_TOKEN</code>).</p>
<p>2️⃣ <strong>Push code to GitHub</strong> → Auto-publish on npm!</p>
<p>👉 Automating publishing prevents human errors and ensures quality control.</p>
<h3 id="heading-ensure-cross-platform-compatibility"><strong>Ensure Cross-Platform Compatibility</strong></h3>
<ul>
<li><p>Use <strong>ES modules (</strong><code>import/export</code>) for modern compatibility.</p>
</li>
<li><p>Include <strong>CommonJS (</strong><code>require/module.exports</code>) support for older environments.</p>
</li>
<li><p>Test with different <strong>Node.js versions</strong> using CI/CD.</p>
</li>
</ul>
<p>Example <code>package.json</code> for dual compatibility:</p>
<pre><code class="lang-javascript">jsonCopyEdit<span class="hljs-string">"type"</span>: <span class="hljs-string">"module"</span>,
<span class="hljs-string">"main"</span>: <span class="hljs-string">"index.cjs"</span>,
<span class="hljs-string">"exports"</span>: {
  <span class="hljs-string">"import"</span>: <span class="hljs-string">"./index.mjs"</span>,
  <span class="hljs-string">"require"</span>: <span class="hljs-string">"./index.cjs"</span>
}
</code></pre>
<p>👉 This ensures your package works everywhere (Node.js, React, Next.js, and so on).</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Congratulations! 🎉 You’ve successfully learned how to create, publish, and use your own npm package, while also understanding the benefits of both <strong>npm</strong> and <strong>Yarn</strong> for package management.</p>
<p>Throughout this guide, we covered:</p>
<p>✔️ What npm is and why it’s important<br>✔️ How to use npm and Yarn to manage dependencies<br>✔️ How to create a reusable npm package<br>✔️ How to publish and update your package on npm<br>✔️ How to integrate your package into a React project with Vite<br>✔️ Best practices for writing, testing, and maintaining your library</p>
<p>By following these steps, you've taken an important step toward open-source development and modular programming, making your code reusable for both yourself and the developer community.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ When to Use NPM Packages – A Guide for Developers ]]>
                </title>
                <description>
                    <![CDATA[ You know when you hit a roadblock while coding and think, "Hey, someone has probably done this before"? That's where npm (Node Package Manager) comes in handy. This huge collection of ready-made code modules created by other developers allows you to ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/when-to-use-npm-packages/</link>
                <guid isPermaLink="false">66bb8925b0d3ac3d7acde3df</guid>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Jaja ]]>
                </dc:creator>
                <pubDate>Mon, 24 Jun 2024 15:59:46 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/06/Article-Image.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You know when you hit a roadblock while coding and think, "Hey, someone has probably done this before"? That's where npm (Node Package Manager) comes in handy. This huge collection of ready-made code modules created by other developers allows you to plug them into your projects and take advantage of these solutions.</p>
<p>In this article, we’ll talk about npm packages along with their benefits and pitfalls. I'll equip you with the tools and knowledge to help you decide when to use npm packages.</p>
<h2 id="heading-what-well-cover"><strong>What We'll Cover:</strong></h2>
<ol>
<li><a class="post-section-overview" href="#heading-whats-the-deal-with-npm-and-npm-packages">What's the Deal with NPM and NPM Packages?</a></li>
<li><a class="post-section-overview" href="#heading-advantages-of-using-npm-packages">Advantages of using NPM Packages</a></li>
<li><a class="post-section-overview" href="#heading-the-danger-of-over-reliance-on-npm-packages">The Danger of Over-reliance on NPM Packages</a></li>
<li><a class="post-section-overview" href="#heading-striking-a-balance-with-npm-packages">Striking a Balance with NPM Packages</a> </li>
<li><a class="post-section-overview" href="#heading-what-to-consider-when-choosing-an-npm-package">What to Consider When Choosing an NPM Package</a></li>
<li><a class="post-section-overview" href="#heading-efficient-approach-to-using-npm-packages">Efficient Approach to Using npm Packages</a>                                                                   – <a class="post-section-overview" href="#heading-when-to-use-npm-packages">When to Use npm Packages</a><br>– <a class="post-section-overview" href="#heading-when-using-a-package-may-not-be-necessary">When Using a Package May Not Be Necessary</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ol>
<h2 id="heading-whats-the-deal-with-npm-and-npm-packages">What's the Deal with Npm and Npm Packages?</h2>
<p>Npm, short for <a target="_blank" href="https://www.npmjs.com/">Node Package Manager</a>, plays a crucial role in the JavaScript community. It serves as both a repository and a manager for open-source Node.js packages. </p>
<p>Through npm, developers gain access to many tools, enabling them to install, share, and manage dependencies within their projects.</p>
<p>Think of npm packages as building blocks for coding. They range from simple utilities to elaborate frameworks, streamlining development efforts. Instead of starting from scratch for every challenge, you can leverage these components from your digital toolkit to help accelerate your projects.</p>
<h2 id="heading-advantages-of-using-npm-packages">Advantages of Using NPM Packages</h2>
<p>NPM brings with it a large number of advantages such as:</p>
<p><strong>Efficiency Boost</strong>: Rather than spending hours figuring out a solution from scratch, npm packages offer a shortcut. They're like little time savers that allow you to implement features and functionalities with just a few lines of code.</p>
<p><strong>Wide Range of Options</strong>: From basic utilities to advanced frameworks, there's a package for almost anything. Need to add a slick carousel to your website? There's a package for that. Tired of the peace that code-formatters like Prettier bring and want to embrace violence? You guessed it—there's <a target="_blank" href="https://www.npmjs.com/package/shittier">a package for that</a> too! Heck, <a target="_blank" href="https://www.npmjs.com/package/is-thirteen?activeTab=readme">need to check if a value (number) equals 13</a> 😂? NPM can help. </p>
<p>With npm, the possibilities are virtually endless, giving you access to a world of pre-built solutions at your fingertips.</p>
<p><strong>Community Collaboration</strong>: One of the great things about npm is the strong sense of community it creates. Developers from around the globe actively add to the npm ecosystem by creating and sharing packages. This means you're not just depending on your skills – you're benefiting from the collective knowledge and experience of many developers. </p>
<p>If you run into a problem with a package, you can simply check out forums or GitHub repositories where someone else may have encountered the same issue and can help out. It's like having a team of experienced experts ready to lend a hand whenever you need it.</p>
<p><strong>Modularity and Code Reusability</strong>: NPM packages encourage a modular approach to development, which is like organising your code into neat little building blocks. This makes your codebase more maintainable, scalable, and easier to debug. Plus, it promotes code reusability, allowing you to leverage existing packages in multiple projects.</p>
<p><strong>Streamlined Development Workflow</strong>: With npm packages, you can streamline your development workflow and focus on what you do best—building awesome stuff. </p>
<p>Instead of getting bogged down in the nitty-gritty details of implementation, you can quickly integrate existing solutions and move on to the next task. This allows you to iterate faster, meet deadlines more efficiently, and ultimately deliver better results to your clients or users.</p>
<h2 id="heading-the-danger-of-over-reliance-on-npm-packages">The Danger of Over-reliance on NPM Packages</h2>
<p>So, here's the deal: while npm packages can be like a magical fix for coding difficulties, they also come with some downsides. Let me share a story from my time learning with my mentor, let's call him Hihi.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/wink-meme.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Wink meme</em></p>
<p>Hihi was all about building strong coding skills from the ground up. He often said, "<em>Packages are handy shortcuts, but if you rely on them too much, you might end up stuck.</em>" </p>
<p>I remember how he'd challenge me to build UI components like dropdown menus, sliders, animations and so on without using any packages. It was tough, but it taught me a lot about how things work behind the scenes.</p>
<p>Hihi had seen firsthand how relying too much on packages could trip up developers. He'd tell me about people he knew who struggled to implement features when they didn't have their trusty packages to lean on. It wasn't just about being a coding genius—it was about understanding the fundamentals and learning to think outside the box.</p>
<p>And you know what? Hihi was right. Building stuff from scratch pushed me to understand the code and solve my problems when things got tricky.</p>
<h3 id="heading-why-you-shouldnt-overuse-npm-packages">Why You Shouldn't Overuse NPM Packages</h3>
<p><strong>Dependency Overload</strong>: Adding each npm package to your project also means adding its dependencies. This can lead to a mess of dependencies that become tricky to handle and keep up to date. </p>
<p>Plus, updating a package can sometimes cause unexpected issues, potentially breaking your application's functionality.</p>
<p><strong>Security Risks</strong>: Not all npm packages are created equal. Some may contain vulnerabilities or even malicious code (often seen in the terminal right after installing a package), putting your projects at risk. Relying too heavily on packages without vetting them properly can leave your projects vulnerable to attacks.</p>
<p><strong>Maintenance Burden</strong>: When you rely on npm packages for essential functionality, you're at the mercy of the package maintainers. If a package gets deprecated or stops receiving updates, you're left scrambling to find a replacement or fix the issue yourself.</p>
<p><strong>Loss of Creativity</strong>: Over-reliance on npm packages can stifle our creativity as developers. Instead of thinking critically and problem-solving independently, you may default to using packages as a crutch. This can hinder your growth and development as a programmer, limiting your ability to tackle new challenges and innovate.</p>
<p><strong>Increased Bundle Size</strong>: Integrating each npm package into your project adds to the overall bundle size, and depending on various factors, some packages (Looking at you, <a target="_blank" href="https://www.npmjs.com/package/styled-components/v/6.1.8">styled-components</a> 😒😂) can bulk up significantly. </p>
<p>This can pose a notable challenge, particularly if speed and performance are important for your application. Larger bundles may result in sluggish load times, which negatively affect user experience and even your search engine rankings.</p>
<p><strong>Overhead of Unused Code</strong>: NPM packages often come with a lot of extra code that you may not need for your project. This unused code adds unnecessary overhead to your application, bloating the file size and slowing down performance.</p>
<p><strong>Compatibility Concerns</strong>: Mixing and matching npm packages can sometimes lead to compatibility issues, especially when different packages are written in different programming languages or use conflicting versions of dependencies. </p>
<p>For example, if one package is written in JavaScript and another in TypeScript, they may play poorly together, leading to errors and unexpected behaviour.</p>
<h2 id="heading-striking-a-balance-with-npm-packages">Striking a Balance with NPM Packages</h2>
<p>Now hold on a minute, I don’t want you to think that NPM is all bad. NPM packages can be super helpful and you shouldn't fear them.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/woah-family-guy.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Rather, I want you to cultivate the rationale behind your decision-making when choosing npm packages.</p>
<p>Choosing the right npm packages can be a bit like navigating a maze—you want to find the shortest, most efficient path to your destination without getting lost along the way. </p>
<p>So how do you balance leveraging the benefits of npm packages and avoiding the potential pitfalls? Let's break it down.</p>
<h3 id="heading-what-to-consider-when-choosing-an-npm-package">What to Consider When Choosing an NPM Package</h3>
<ul>
<li><strong>How Much of the Package You'll Use</strong>: Consider how much of the package you'll use in your project. If you only need one or two features, building those features yourself may be more efficient.</li>
<li><strong>Package Size</strong>: Larger packages contribute to increased bundle size, which can impact performance. Opt for smaller, more lightweight packages whenever possible.</li>
<li><strong>Complexity of Functionality</strong>: Evaluate whether the functionality provided by the package is fairly complex to implement from scratch. If so, using the package may save time and effort.</li>
<li><strong>Project Time Estimate</strong>: Consider the timeline of your project. If you're working on a tight deadline, using npm packages can speed up development and meet project milestones more quickly.</li>
<li><strong>Maintenance and Support</strong>: Prioritize packages with ongoing maintenance and strong community support. Opt for those with dedicated maintainers and an active community, as they are more likely to receive prompt updates and assistance when needed.</li>
<li><strong>Compatibility</strong>: Ensure that the package is compatible with your project's tech stack, including programming languages, frameworks, and dependencies.</li>
</ul>
<p>To better guide you, here’s a flowchart of how your decision-making process can go.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/Flowchart-of-decision-making.png" alt="Image" width="600" height="400" loading="lazy">
<em>How to decide which npm packages to use - and whether to use them.</em></p>
<p>As seen in the graph above, each step is built on the next, aiding you in making a sound choice when picking a package.</p>
<h2 id="heading-efficient-approach-to-using-npm-packages">Efficient Approach to Using npm Packages</h2>
<p>While packages can be incredibly useful for streamlining development and adding advanced features, it's important to consider when you should use them and when it might not be necessary.</p>
<h3 id="heading-when-to-use-npm-packages">When to Use npm Packages</h3>
<ul>
<li><strong>Routing</strong>: If your web app needs complex navigation, grabbing a battle-tested routing library like React Router can make your life a lot easier. These packages handle dynamic route matching, nested routes, and more, saving you time and headaches.</li>
<li><strong>Form Validation</strong>: Save yourself the headache of reinventing the validation wheel. To streamline your form validation process, utilize specialized form validation libraries like <a target="_blank" href="https://formik.org/">Formik</a> for React or <a target="_blank" href="https://vee-validate.logaretm.com/v4/">VeeValidate</a> for Vue.js. These libraries ensure consistent validation behavior across your forms, without the hassle of building it from scratch.</li>
<li><strong>Animations</strong>: Want to dazzle your users with smooth and engaging animations? You can use animation libraries like <a target="_blank" href="https://www.framer.com/motion/">Framer Motion</a> or <a target="_blank" href="https://gsap.com/">GreenSock (GSAP)</a>. These libraries provide an array of tools to help you achieve complex animations with minimal effort, whether it's animating components, transitions, or scroll-based effects.</li>
<li><strong>Styling</strong>: When it comes to styling, CSS-in-JS libraries like <a target="_blank" href="https://styled-components.com/">styled-components</a> or <a target="_blank" href="https://emotion.sh/docs/introduction">Emotion</a> can be powerful allies. While they may not be necessary for simple styling needs, they shine in projects requiring dynamic styling, theming, or responsive design. These libraries offer powerful styling capabilities and component-level encapsulation, making styling a breeze.</li>
<li><strong>UI Components</strong>: Building custom UI components from scratch can be time-consuming, especially for complex components like date pickers or data tables. Integrating well-designed UI component libraries such as <a target="_blank" href="https://ui.shadcn.com/">ShadCN</a>, <a target="_blank" href="https://ant.design/">Ant Design</a>, or <a target="_blank" href="https://tailwindcss.com/">Tailwind CSS</a> can accelerate development and ensure a consistent look and feel across your application.</li>
</ul>
<h3 id="heading-when-using-a-package-may-not-be-necessary">When Using a Package May Not Be Necessary</h3>
<ul>
<li><strong>Basic Utility Functions</strong>: For simple utility functions or helper methods, writing them yourself may be more efficient rather than adding an additional dependency. It also keeps your codebase lightweight and avoids unnecessary dependencies.</li>
<li><strong>Custom Business Logic</strong>: If your project requires highly specialized or domain-specific logic, relying on generic npm packages may not be suitable. Building custom solutions tailored to your project's unique requirements can offer greater flexibility and control over functionality.</li>
<li><strong>Performance Optimization</strong>: While npm packages can provide convenient solutions, they may also introduce overhead and impact performance. For performance-critical aspects of your application, consider optimizing code internally rather than relying on external dependencies.</li>
<li><strong>Learning and Skill Development</strong>: Building features from scratch offers valuable opportunities for learning and skill development. Consider tackling certain challenges without relying on packages to deepen your understanding of underlying concepts and enhance your problem-solving abilities.</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Remember, npm packages are like tools in a toolbox—useful when needed, but not always necessary. </p>
<p>Before reaching for a package, consider whether it aligns with your project's goals and whether you could achieve the same result with a custom solution. </p>
<p>So next time you're tempted to grab a package, pause and ask yourself: "Do I need it? Do I really need it? Do I…?" You get the idea.</p>
<h3 id="heading-like-my-articles">Like my articles?</h3>
<p>Feel free to <a target="_blank" href="https://www.buymeacoffee.com/JajaDavid">buy me a coffee here</a>, to keep my brain chugging and provide more articles like this.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/06/coffee-tom.gif" alt="coffee-tom" width="600" height="400" loading="lazy">
<em>Coffee Tom</em></p>
<h3 id="heading-contact-information"><strong>Contact Information</strong></h3>
<p>Want to connect or contact me? Feel free to hit me up on the following:</p>
<ul>
<li>Twitter / X: <a target="_blank" href="https://twitter.com/JajaDavid8">@jajadavid8</a></li>
<li>LinkedIn: <a target="_blank" href="https://www.linkedin.com/in/david-jaja-8084251b4/">David Jaja</a></li>
<li>Email: Jajadavidjid@gmail.com</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use pnpm – Installation and Common Commands ]]>
                </title>
                <description>
                    <![CDATA[ pnpm is like npm, but it's way faster and more efficient. After all, the starting p stands for _p_erformant. According to its creator, Zoltan Kochan, pnpm "allows you to save gigabytes of disk space." Many popular projects including Next.js, Vite, Sv... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-use-pnpm/</link>
                <guid isPermaLink="false">66c5f749bae1f0a10743aece</guid>
                
                    <category>
                        <![CDATA[ node js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ performance ]]>
                    </category>
                
                    <category>
                        <![CDATA[ pnpm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ashutosh Biswas ]]>
                </dc:creator>
                <pubDate>Tue, 09 Jan 2024 15:31:52 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/cover-pnpm-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><em>pnpm</em> is like npm, but it's way faster and more efficient. After all, the starting <em>p</em> stands for _p_erformant.</p>
<p>According to its creator, Zoltan Kochan, pnpm "allows you to save gigabytes of disk space."</p>
<p>Many popular projects including Next.js, Vite, Svelte, and even freeCodeCamp use pnpm. So now is a great time try out this tool if you haven't yet. I'm sure your time will not be wasted.</p>
<p>In this article, I won't go into details of why pnpm is faster and more efficient than npm. You can check out the <a target="_blank" href="https://pnpm.io/motivation">official documentation</a> if you want to know more about that.</p>
<p>The goal of this article is to quickly get you started with pnpm so you can perform your day to day tasks that you previously did with npm or yarn. Grab your favorite cup of tea or coffee ☕️, and let's dive right in! 🚀</p>
<h2 id="heading-how-to-install-pnpm">How to Install pnpm</h2>
<p>I assume you already have a modern version of Node.js installed on your machine. These modern versions come with a command called <code>corepack</code>. It let's you manage your Node package managers. </p>
<p>Yes you read that right! It's an experimental feature of Node but it works pretty well. </p>
<p>So to start using it, you first need to enable it by entering the following command from your terminal, which has the effect of installing pnpm (and also yarn) on your system:</p>
<pre><code class="lang-zsh">corepack <span class="hljs-built_in">enable</span>
</code></pre>
<p>It's that simple. Now if you run <code>pnpm --version</code> you will see the version you have just installed. But this might not be the latest version of pnpm. If this is the case, you can install the latest version of pnpm using this command:</p>
<pre><code class="lang-zsh">corepack prepare pnpm@latest --activate
</code></pre>
<p>Keep in mind that there are many ways to install pnpm on your system, and you can read about all of them in the <a target="_blank" href="https://pnpm.io/installation">installation docs</a>. My favorite is the <code>corepack</code> formula I've shown above.</p>
<h2 id="heading-how-to-configure-your-shell-for-efficiency-optional">How to Configure your Shell for Efficiency (Optional)</h2>
<p>Well, you now have pnpm installed. But the default command line experience can be improved to save you some effort. </p>
<p>Note that this section is optional. If you want you can skip to the next section. But if you are serious about setting it up so that the CLI experience is pleasant, let's learn how to do it.</p>
<h3 id="heading-pnpm-is-hard-to-type-so-set-up-an-alias"><code>pnpm</code> is Hard to Type – So Set Up an Alias</h3>
<p>If you find <code>pnpm</code> hard to type like I do, you can set up an alias to to save you some effort. If you're on Linux or MacOS, just put the following in your shell config (<code>.bashrc</code>, <code>.zshrc</code>, or <code>config.fish</code>):</p>
<pre><code>alias pn=pnpm
</code></pre><p>If you want to set up your alias in Powershell (Windows) you can <a target="_blank" href="https://pnpm.io/installation#adding-a-permanent-alias-in-powershell-windows">see this doc</a>.</p>
<h3 id="heading-how-to-setup-tab-completion">How to Setup Tab-Completion</h3>
<p>There are two ways you can do this in pnpm. Both have their pros and cons.</p>
<p>First I will share with you my favorite method. It's a shell plugin called <code>pnpm-shell-completion</code> and is available for zsh, fish shell, and Powershell core. It only covers the most commonly used commands. If you are Arch Linux and zsh user, you can install it with any AUR helper. For example, if you use <code>yay</code>, run the following command to install it:</p>
<pre><code class="lang-zsh">yay -S pnpm-shell-completion
</code></pre>
<p>Then add the following line in your <code>.zshrc</code> file to load it:</p>
<pre><code class="lang-zsh"><span class="hljs-built_in">source</span> /usr/share/zsh/plugins/pnpm-shell-completion/pnpm-shell-completion.zsh
</code></pre>
<p>Now it should work. If you use any other supported shell, follow the plugin's <a target="_blank" href="https://github.com/g-plane/pnpm-shell-completion">doc</a> to learn how to install it.</p>
<p>The second method comes built-in with pnpm. To setup this style of auto-completion, run the following command:</p>
<pre><code class="lang-shell">pnpm install-completion
</code></pre>
<p>And then follow the steps it gives you. This method covers more commands than the first approach. But it has some limitations – for example it can't auto-complete your <code>package.json</code> scripts. It also, for example, can't auto complete any dependency name that you want to uninstall.</p>
<h2 id="heading-how-to-use-pnpm">How to Use <code>pnpm</code></h2>
<p>Now, you should have pnpm installed with an alias and tab-completion. No more delay – let's see how to use pnpm.</p>
<h3 id="heading-how-to-initialize-a-new-project-using-pnpm">How to Initialize a New Project using <code>pnpm</code></h3>
<p>To get the default <code>package.json</code> in the current directory, run the following command:</p>
<pre><code class="lang-zsh">pnpm init
</code></pre>
<p>Unlike npm, it will not create it interactively and you don't need to specify the <code>-y</code> flag for this.</p>
<h3 id="heading-how-to-install-a-package">How to Install a Package</h3>
<p>To install a package as a dependency, the syntax is:</p>
<pre><code>pnpm add &lt;pkg&gt;
</code></pre><p>To install a package as a dev dependency, you have pass the <code>-D</code> (or <code>--save-dev</code>) flag:</p>
<pre><code>pnpm add -D &lt;pkg&gt;
</code></pre><p>To install a package globally, use the <code>-g</code> flag:</p>
<pre><code>pnpm add -g &lt;pkg&gt;
</code></pre><h3 id="heading-how-to-install-all-dependencies">How to Install All Dependencies</h3>
<p>Suppose you cloned a project from GitHub. It does have a <code>package.json</code> file but no <code>node_modules</code> (you should not track <code>node_modules</code> with Git). Now to install all the dependencies in that <code>package.json</code>, the command is very similar to <code>npm</code>:</p>
<pre><code>pnpm install
</code></pre><p>or</p>
<pre><code>pnpm i
</code></pre><h3 id="heading-how-to-run-a-packagejson-script">How to Run a <code>package.json</code> Script</h3>
<p>This process is also very similar to <code>npm</code>. The explicit way to do it is to use the <code>run</code> command. If you have a script named <code>build</code>, you can execute it with this command:</p>
<pre><code>pnpm run build
</code></pre><p>You can also use <code>pnpm build</code> to do the same thing. This is a shorthand format that can do other things as well. We'll learn more about shorthand very soon in this article.</p>
<h3 id="heading-how-to-run-commands-that-come-with-dependencies">How to Run Commands that Come with Dependencies</h3>
<p>You can run commands that come with dependencies using <code>pnpm exec</code>.</p>
<p>When you install a package, if it has commands specified by the <code>bin</code> field in its <code>package.json</code>, you will get an executable of the same name in your <code>node_modules/.bin</code> directory. Its purpose to execute the corresponding file.</p>
<p><code>pnpm exec</code> prepends <code>./node_modules/.bin</code> to the <code>PATH</code> (that is, <code>PATH=./node_modules/.bin:$PATH</code>) and then executes the given command.</p>
<p>The following is an example that shows installing <code>typescript</code> as a dev dependency and then running the <code>tsc</code> command to create a <code>tsconfig.json</code> file:</p>
<pre><code>pnpm add -D typescript
pnpm exec tsc --init
</code></pre><p>Similar to the <code>pnpm run</code> command, you can also omit <code>exec</code> and just use <code>pnpm tsc</code> to do the same thing. This works when you don't have a conflicting <code>tsc</code> script in your <code>package.json</code>. In the next section we will take a close look at this shorthand syntax.</p>
<p>Note that since <code>pnpm exec</code> has access to all commands resolved by the paths specified in <code>PATH</code>, you may have access to many system commands for example <code>rm</code>, <code>ls</code>, and so on.</p>
<h3 id="heading-how-pnpm-works">How <code>pnpm &lt;command&gt;</code> Works</h3>
<p><code>pnpm &lt;command&gt;</code> works like this:</p>
<ul>
<li>If <code>&lt;command&gt;</code> is a pnpm command (that is <code>add</code>, <code>install</code> and so on), execute that command.</li>
<li>Else if <code>&lt;command&gt;</code> is a script found in <code>package.json</code>, execute <code>pnpm run &lt;command&gt;</code>.</li>
<li>Else execute <code>pnpm exec &lt;command&gt;</code>.</li>
</ul>
<p>So <code>pnpm &lt;command&gt;</code> serves as a convenient shortcut where <code>pnpm exec</code> and <code>pnpm run</code> are explicit commands without fallback.</p>
<h3 id="heading-how-to-update-packages">How to Update Packages</h3>
<p>To update packages to their latest versions based on the specified range in <code>package.json</code>, run this command:</p>
<pre><code>pnpm up
</code></pre><p>To update all dependencies to their latest versions, ignoring ranges specified in <code>package.json</code>, run this:</p>
<pre><code>pnpm up --latest
</code></pre><h3 id="heading-how-to-remove-a-package">How to Remove a Package</h3>
<p>To remove a package from both <code>node_modules</code> and your <code>package.json</code>, you can use <code>pnpm rm</code>. For example if you installed <code>express</code>, you can remove it using:</p>
<pre><code>pnpm rm express
</code></pre><p>To remove a globally installed package, use the <code>-g</code> flag. Below is an example of removing the globally installed package <code>nodemon</code>:</p>
<pre><code>pnpm rm -g nodemon
</code></pre><h2 id="heading-is-there-an-npx-alternative">Is There an <code>npx</code> Alternative?</h2>
<p>Yes – it's the <code>pnpm dlx</code> command. It's very similar to npx. It downloads the specified package from the registry without installing it as a dependency and then runs whatever default command binary it exposes.</p>
<p>For example, you can run the command that <code>cowsay</code> package exposes like below to print ASCII art of a cow saying a string that you pass:</p>
<pre><code>pnpm dlx cowsay hi freeCodeCamp
</code></pre><p>Now you might be wondering, if a package exposes multiple command binaries, what command <code>pnpm dlx</code> chooses as the default? Or how can you explicitly specify a command binary?</p>
<p>Let's see how the default command binary is determined first:</p>
<ul>
<li>If the <code>bin</code> field of <code>package.json</code> has only one entry, then that is used.</li>
<li>Else if there is a command name in the <code>bin</code> field of <code>package.json</code> that matches the package name, ignoring the scope part if any, then that command is used.</li>
<li>Else pnpm can't determine the default command and throws an error with a helpful error message that most likely will answer the second question.</li>
</ul>
<p>To explicitly specify a particular command, you will need to install the package first using the <code>--package</code> option and specify that command after <code>dlx</code>. </p>
<p>For example the package <code>typescript</code> exposes to command binaries <code>tsc</code> and <code>tsserver</code>. Now if you want to run <code>tsc --init</code> to create a <code>tsconfig.json</code> file without having <code>typescript</code> in your <code>node_modules</code> or <code>package.json</code>, you can use <code>pnpm dlx</code> like below:</p>
<pre><code>pnpm --package=typescript dlx tsc --init
</code></pre><h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, you've learned what pnpm is and how to install it. We've also covered several common pnpm commands that you will most likely need on a daily basis. </p>
<p>I hope this article helped you get up and running with pnpm. Check out the <a target="_blank" href="https://pnpm.io/motivation">documentation of pnpm</a> to learn more about it.</p>
<p>If you want you can follow me on <a target="_blank" href="https://www.linkedin.com/in/ashutosh-biswas/">LinkedIn</a> and <a target="_blank" href="https://twitter.com/ashutoshbw">Twitter</a> where I share useful coding related things.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create and Publish an NPM Package – a Step-by-Step Guide ]]>
                </title>
                <description>
                    <![CDATA[ NPM is the largest software registry on the internet. There are over a million packages in the NPM Library. Developers publish packages on NPM to share their code with others. And organisations also use NPM to share code internally. In this article, ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-and-publish-your-first-npm-package/</link>
                <guid isPermaLink="false">66d45ddaa3a4f04fb2dd2e33</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ modules ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Benjamin Semah ]]>
                </dc:creator>
                <pubDate>Wed, 01 Feb 2023 21:36:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/npm-package-article-image.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>NPM is the largest software registry on the internet. There are over a million packages in the NPM Library.</p>
<p>Developers publish packages on NPM to share their code with others. And organisations also use NPM to share code internally.</p>
<p>In this article, you will learn how to create a package. And you will also learn how to publish your package on NPM so others can download and use it.</p>
<p>Let's get started!</p>
<h2 id="heading-how-to-choose-a-name-for-your-npm-package">How to Choose a Name For Your NPM Package</h2>
<p>The first thing you need to do before creating your package is to choose a name. This is important because your package needs to have a unique name. You should choose a name that has not been used already.</p>
<p>When you decide on a name, go to the <a target="_blank" href="https://www.npmjs.com/">NPM registry</a> and run a search. Be sure there's no exact match to the name you chose (or a match that is too similar).</p>
<p>For example, if there's a package called <code>hellonpmpackage</code> and you decide to call yours <code>hello-npm-package</code>, NPM will throw an error when you attempt to publish it.</p>
<p>If there's already a package in the NPM registry with the same you want to use, then you have two options.</p>
<ol>
<li><p>You can choose a different name.</p>
</li>
<li><p>You can publish your package as a scoped package (see the section "Publishing scoped packages" below).</p>
</li>
</ol>
<h2 id="heading-how-to-create-a-npm-package">How to Create a NPM Package</h2>
<p>Follow the steps below to create your package.</p>
<h3 id="heading-1-install-node">1. Install Node</h3>
<p>If you do not already have Node installed, you should go ahead and install it. You can visit the official website to <a target="_blank" href="https://nodejs.org/en/download/">download and install Node.js</a>. NPM comes pre-installed with Node.</p>
<h3 id="heading-2-initialize-a-git-repository">2. Initialize a Git Repository</h3>
<p>Create a new project folder for your package and navigate into the folder. Then, run the following command in your terminal:</p>
<pre><code class="lang-json">git init
</code></pre>
<p>This will help you track the changes you make to your package. Also, make sure you have a remote version of your repository on GitHub (or your preferred hosting service).</p>
<h3 id="heading-3-initialize-npm-in-your-project">3. Initialize NPM in Your Project</h3>
<p>To do this, navigate to the root directory of your project and run the following command:</p>
<pre><code class="lang-json">npm init
</code></pre>
<p>This command will create a <code>package.json</code> file. You will get prompts to provide the following information:</p>
<ul>
<li><p><code>package-name</code>: As you learned earlier in this tutorial, the name of your package must be unique. Also it must be lowercase. It may include hyphens.</p>
</li>
<li><p><code>version</code>: The initial value is 1.0.0. You update the number when you update your package using <a target="_blank" href="https://www.freecodecamp.org/news/semantic-versioning-1fd6f57749f7/">semantic versioning</a>.</p>
</li>
<li><p><code>description</code>: You can provide a description of your package here. Indicate what your package does and how to use it.</p>
</li>
<li><p><code>entry point</code>: The entry file for your code. The default value is <code>index.js</code>.</p>
</li>
<li><p><code>test command</code>: Here, you can add the command you want to run when a user runs <code>npm run test</code>.</p>
</li>
<li><p><code>git repository</code>: The link to your remote repository on GitHub.</p>
</li>
<li><p><code>keywords</code>: Add relevant keywords that will help others find your package on the NPM registry.</p>
</li>
<li><p><code>author</code>: Add your name.</p>
</li>
<li><p><code>license</code>: You can add a license or use the default license (Internet Systems Consortium (ISC) License).</p>
</li>
</ul>
<p>See the screenshot below for an example of how to answer the prompt questions:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/npm-image1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Creating a package.json file</em></p>
<p><em>Note: I left the</em> <code>test command</code> blank because there is no test command for the package in this tutorial.</p>
<h3 id="heading-4-add-your-code">4. Add Your Code</h3>
<p>Now, you can go ahead and add the code for your package.</p>
<p>First, you need to create the file that will be loaded when your module is required by another application. For this tutorial, that will be the <code>index.js</code> file.</p>
<p>Inside the <code>index.js</code> file, add the code for your package.</p>
<p>For this tutorial, I will be creating a simple package called <code>first-hello-npm</code>. This package returns the string <code>"Hello NPM!"</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//index.js</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">helloNpm</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">"hello NPM"</span>
}

<span class="hljs-built_in">module</span>.exports = helloNpm
</code></pre>
<p>After creating your function, you should export it like in the example above. That way, anyone who downloads your package can load and use it in their code.</p>
<p>If you have been following along, you should now have your package created. But before you publish, you need to test your package. Testing your package reduces the chances of publishing bugs to the NPM registry.</p>
<h2 id="heading-how-to-test-your-npm-package">How to Test Your NPM Package</h2>
<p>Testing ensures that your NPM package works as expected. There are many ways to test your package. In this tutorial, you will learn one of the simplest ways of testing.</p>
<p>First, navigate to the root of your <code>package</code> project. Then, run the following command:</p>
<pre><code class="lang-json">npm link
</code></pre>
<p>This will make your package available globally. And you can require the package in a different project to test it out.</p>
<p>Create a <code>test</code> folder. And inside that test folder, add a <code>script.js</code> file.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/npm-image2.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Example of a test folder with a script.js file</em></p>
<p>In the example above, the test folder contains only the <code>script.js</code> file. It does not yet contain the package. To add the package you created to your test folder, run the command below:</p>
<pre><code class="lang-json">npm link &lt;name-of-package&gt;
</code></pre>
<p>In the case of the test folder for this tutorial, I will run the following command:</p>
<pre><code class="lang-json">npm link first-hello-npm
</code></pre>
<p>This will create a <code>node-modules</code> folder. And it'll add all the files and folders from your package – see the screenshot below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/npm-image3.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the <code>script.js</code> file, you can now require your package and use it for the test.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// test/script.js</span>

<span class="hljs-keyword">const</span> helloNpm = <span class="hljs-built_in">require</span>(<span class="hljs-string">'first-hello-npm'</span>)

<span class="hljs-built_in">console</span>.log(helloNpm())
</code></pre>
<p>The <code>first-hello-npm</code> package is expected to return the string <code>"hello NPM!"</code>. As you can see from the screenshot below, the package works as expected when I run the script.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/npm-image4.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Test result for first-hello-npm package</em></p>
<p>After testing your package and ensuring it works as expected, you can now publish it on the NPM registry.</p>
<h2 id="heading-how-to-publish-your-npm-package">How to Publish Your NPM Package</h2>
<p>To publish your package on the NPM registry, you need to have an account. If you don't have an account, visit the <a target="_blank" href="https://www.npmjs.com/signup">NPM sign up page</a> to create one.</p>
<p>After creating the account, open your terminal and run the following command in the root of your package:</p>
<pre><code class="lang-json">npm login
</code></pre>
<p>You will get a prompt to enter your <code>username</code> and <code>password</code>. If login is successful, you should see a message like this:</p>
<p><code>Logged in as &lt;your-username&gt; on https://registry.npmjs.org/.</code></p>
<p>You can now run the following command to publish your package on the NPM registry:</p>
<pre><code class="lang-json">npm publish
</code></pre>
<p>If all goes well, you should get results similar to the screenshot below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/npm-image7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Notice indicating that the package is published successfully.</em></p>
<p>If you have been following along, then congratulations! You just published your first NPM package.</p>
<p>You can visit the <a target="_blank" href="https://www.npmjs.com/">NPM website</a> and run a search for your package. You should see your package show up in the search results.</p>
<p>For example, from the screenshot below, you can see the <code>first-hello-npm</code> package is now available on NPM.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/package-available.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The first-hello-npm package is now available on NPM</em></p>
<h2 id="heading-how-to-publish-scoped-npm-packages">How to Publish Scoped NPM Packages</h2>
<p>If an existing package has the same name you would like to use, the workaround is to publish a scoped package.</p>
<p>When you publish a scoped package, you have the option to make it public or private. If it's private, you can choose who you want to share the package with.</p>
<h3 id="heading-how-to-create-a-scoped-npm-package">How to Create a Scoped NPM Package</h3>
<p>To create a scoped package, first navigate to the root of your package directory.</p>
<p>Then, run the <code>npm init</code> command and pass your <code>username</code> as the value to the <code>scope</code> flag:</p>
<pre><code class="lang-json">npm init --scope=@your-username
</code></pre>
<p>Respond to the prompts to create a <code>package.json</code> file. For your package name, the format should be <code>@your-username/package-name</code>.</p>
<p>For example <code>@benjaminsemah/first-hello-npm</code>.</p>
<p>You can now add the code for your package and test it. The process is the same as already explained above.</p>
<p>Then, to publish your scoped package, run the following command in your terminal.</p>
<pre><code class="lang-json">npm publish --access public
</code></pre>
<p>You can change from <code>public</code> to <code>private</code> if you don't want to make the package available for public use.</p>
<p>You should see a response similar to this.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/scoped-package-published.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Scoped package published successfully.</em></p>
<p>Congratulations if you followed along. You've published your scoped package. You should see your scoped package on NPM if you search for it. For example in the screenshot below, you can see the scoped package I created in this tutorial.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/scoped-package-available.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Scoped package is now available on NPM</em></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Packages helps developers work faster. And they also improve collaboration. When you figure out a smarter way of doing things, one way you can share with the community is to create and publish your solution as a package.</p>
<p>In this article, you learned what packages are and why they are useful. You also learned how to create and publish packages on the NPM registry. The developer community awaits all the beautiful packages you will create and share.</p>
<p>Thanks for reading. And happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What Are Node Modules and How Do You Use Them? ]]>
                </title>
                <description>
                    <![CDATA[ Every Node.js application has modules. These modules form part of the building blocks of the application. They help developers work faster and write more structured code. In this tutorial, you will learn what node modules are. You will also learn abo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-are-node-modules/</link>
                <guid isPermaLink="false">66d45df0182810487e0ce11d</guid>
                
                    <category>
                        <![CDATA[ Express ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ modules ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Benjamin Semah ]]>
                </dc:creator>
                <pubDate>Tue, 06 Dec 2022 17:54:33 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/11/stock.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Every Node.js application has modules. These modules form part of the building blocks of the application. They help developers work faster and write more structured code.</p>
<p>In this tutorial, you will learn what node modules are. You will also learn about the three types of node modules. And we'll go over the right way to use them in your applications.</p>
<h2 id="heading-what-is-a-module-in-javascript">What is a Module in JavaScript?</h2>
<p>In simple terms, a module is a piece of reusable JavaScript code. It could be a <code>.js</code> file or a directory containing <code>.js</code> files. You can export the content of these files and use them in other files.</p>
<p>Modules help developers adhere to the DRY (Don't Repeat Yourself) principle in programming. They also help to break down complex logic into small, simple, and manageable chunks.</p>
<h2 id="heading-types-of-node-modules">Types of Node Modules</h2>
<p>There are three main types of Node modules that you will work with as a Node.js developer. They include the following.</p>
<ul>
<li><p>Built-in modules</p>
</li>
<li><p>Local modules</p>
</li>
<li><p>Third-party modules</p>
</li>
</ul>
<h3 id="heading-built-in-modules">Built-in Modules</h3>
<p>Node.js comes with some modules out of the box. These modules are available for use when you install Node.js. Some common examples of built-in Node modules are the following:</p>
<ul>
<li><p>http</p>
</li>
<li><p>url</p>
</li>
<li><p>path</p>
</li>
<li><p>fs</p>
</li>
<li><p>os</p>
</li>
</ul>
<p>You can use the built-in modules with the syntax below.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> someVariable = <span class="hljs-built_in">require</span>(<span class="hljs-string">'nameOfModule'</span>)
</code></pre>
<p>You load the module with the <code>require</code> function. You need to pass the name of the module you're loading as an argument to the <code>require</code> function.</p>
<p><strong>Note:</strong> The name of the module must be in quotation marks. Also, using <code>const</code> to declare the variable ensures that you do not overwrite the value when calling it.</p>
<p>You also need to save the returned value from the <code>require</code> function in <code>someVariable</code>. You can name that variable anything you want. But often, you will see programmers give the same to the variable as the name of the module (see example below).</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">'http'</span>) 

server = http.createServer(<span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> { 
    res.writeHead(<span class="hljs-number">200</span>, {<span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'text/plain'</span>}) 
    res.end(<span class="hljs-string">'Hello World!'</span>)
})

server.listen(<span class="hljs-number">3000</span>)
</code></pre>
<p>You use the <code>require</code> function to load the built-in <code>http</code> module. Then, you save the returned value in a variable named <code>http</code>.</p>
<p>The returned value from the <code>http</code> module is an object. Since you've loaded it using the <code>require</code> function, you can now use it in your code. For example, call the <code>.createServer</code> property to create a server.</p>
<h3 id="heading-local-modules">Local Modules</h3>
<p>When you work with Node.js, you create local modules that you load and use in your program. Let's see how to do that.</p>
<p>Create a simple <code>sayHello</code> module. It takes a <code>userName</code> as a parameter and prints "hello" and the user's name.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">userName</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Hello <span class="hljs-subst">${userName}</span>!`</span>)
}

<span class="hljs-built_in">module</span>.exports = sayHello
</code></pre>
<p>First, you need to create the function. Then you export it using the syntax <code>module.exports</code>. It doesn't have to be a function, though. Your module can export an object, array, or any data type.</p>
<h4 id="heading-how-to-load-your-local-modules">How to load your local modules</h4>
<p>You can load your local modules and use them in other files. To do so, you use the <code>require</code> function as you did for the built-in modules.</p>
<p>But with your custom functions, you need to provide the path of the file as an argument. In this case, the path is <code>'./sayHello</code>' (which is referencing the <code>sayHello.js</code> file).</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> sayHello = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./sayHello'</span>)
sayHello(<span class="hljs-string">"Maria"</span>) <span class="hljs-comment">// Hello Maria!</span>
</code></pre>
<p>Once you've loaded your module, you can make a reference to it in your code.</p>
<h3 id="heading-third-party-modules">Third-Party Modules</h3>
<p>A cool thing about using modules in Node.js is that you can share them with others. The Node Package Manager (NPM) makes that possible. When you install Node.js, NPM comes along with it.</p>
<p>With NPM, you can share your modules as packages via <a target="_blank" href="https://www.npmjs.com/">the NPM registry.</a> And you can also use packages others have shared.</p>
<h4 id="heading-how-to-use-third-party-packages">How to use third-party packages</h4>
<p>To use a third-party package in your application, you first need to install it. You can run the command below to install a package.</p>
<pre><code class="lang-javascript">npm install &lt;name-<span class="hljs-keyword">of</span>-package&gt;
</code></pre>
<p>For example, there's a package called <code>capitalize</code>. It performs functions like capitalizing the first letter of a word.</p>
<p>Running the command below will install the capitalize package:</p>
<pre><code class="lang-javascript">npm install capitalize
</code></pre>
<p>To use the installed package, you need to load it with the <code>require</code> function.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> capitalize = <span class="hljs-built_in">require</span>(<span class="hljs-string">'capitalize)</span>
</code></pre>
<p>And then you can use it in your code, like this for example:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> capitalize = <span class="hljs-built_in">require</span>(<span class="hljs-string">'capitalize'</span>)
<span class="hljs-built_in">console</span>.log(capitalize(<span class="hljs-string">"hello"</span>)) <span class="hljs-comment">// Hello</span>
</code></pre>
<p>This is a simple example. But there are packages that perform more complex tasks and can save you loads of time.</p>
<p>For example, you can use the Express.js package which is a Node.js framework. It makes building apps faster and simple. To learn more about NPM, read this <a target="_blank" href="https://www.freecodecamp.org/news/what-is-npm-a-node-package-manager-tutorial-for-beginners/">freeCodeCamp article on the Node Package Manager</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned about what Node modules are and the three types of node modules. You also learned about how to use the different types in your application.</p>
<p>Thanks for reading. And happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What Exactly is Node.js? Explained for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ Node.js allows developers to create both front-end and back-end applications using JavaScript. It was released in 2009 by Ryan Dahl. In this article, you will learn about Node.js. You will learn the following: What is Node.js? How the Node.js envir... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-node-js/</link>
                <guid isPermaLink="false">66d45df255db48792eed3f4f</guid>
                
                    <category>
                        <![CDATA[ Express ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Benjamin Semah ]]>
                </dc:creator>
                <pubDate>Mon, 05 Dec 2022 15:18:12 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/11/What-is.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Node.js allows developers to create both front-end and back-end applications using JavaScript. It was released in 2009 by Ryan Dahl.</p>
<p>In this article, you will learn about Node.js. You will learn the following:</p>
<ul>
<li><p>What is Node.js?</p>
</li>
<li><p>How the Node.js environment differs from the browser.</p>
</li>
<li><p>Why you should learn Node.js.</p>
</li>
<li><p>How to get started with Node.js.</p>
</li>
<li><p>Resources to help you learn Node.js.</p>
</li>
</ul>
<h2 id="heading-what-is-nodejs">What is Node.js?</h2>
<blockquote>
<p>"Node.js is an open-source and cross-platform JavaScript runtime environment." - <a target="_blank" href="https://nodejs.dev/en/learn/introduction-to-nodejs/">Nodejs.dev Docs</a></p>
</blockquote>
<p>This sounds like a cool, straightforward answer. But for a beginner, this definition might raise further questions. So let's break it down and understand what it means.</p>
<p><strong>Node.js is open-source:</strong> This means that the source code for Node.js is publicly available. And it's maintained by contributors from all over the world. The <a target="_blank" href="https://nodejs.org/en/get-involved/contribute/">Node.js contribution guide</a> shows you how to contribute.</p>
<p><strong>Node.js is cross-platform:</strong> Node.js is not dependent on any operating system software. It can work on Linux, macOS, or Windows.</p>
<p><strong>Node.js is a JavaScript runtime environment:</strong> When you write JavaScript code in your text editor, that code cannot perform any task unless you execute (or run) it. And to run your code, you need a runtime environment.</p>
<p>Browsers like Chrome and Firefox have runtime environments. That is why they can run JavaScript code. Before Node.js was created, JavaScript could only run in a browser. And it was used to build only front-end applications.</p>
<p>Node.js provides a runtime environment outside of the browser. It's also built on the <a target="_blank" href="https://www.freecodecamp.org/news/javascript-under-the-hood-v8/">Chrome V8 JavaScript engine</a>. This makes it possible to build back-end applications using the same JavaScript programming language you may be familiar with.</p>
<h2 id="heading-differences-between-the-browser-and-nodejs-runtime-environments">Differences Between the Browser and Node.js Runtime Environments</h2>
<p>Both the browser and Node.js are capable of executing JavaScript programs. But there are some key differences that you need to know. They include the following.</p>
<h3 id="heading-access-to-the-dom-apis">Access to the DOM APIs</h3>
<p>With the browser runtime, you can access the Document Object Model (DOM). And you can perform all the DOM operations. But Node.js does not have access to the DOM.</p>
<p>Node.js exposes almost all the system resources to your programs. This means you can interact with the operating system, access the file systems, and read and write to the files. But, you do not have access to operating systems and file systems from the browser.</p>
<h3 id="heading-window-vs-global-object">Window vs Global object</h3>
<p>JavaScript has a built-in global object. The JavaScript global object for the browser is called the <code>window</code> object. In Node.js, the global object goes by the name <code>global</code>.</p>
<p>The <code>window</code> object contains methods and properties available only in the browser environment.</p>
<h3 id="heading-control-over-runtime-versions">Control over runtime versions</h3>
<p>With Node.js, you can choose which version to run your server-side application on. As a result, you can use modern JavaScript features without worrying about any version-specific inconsistencies.</p>
<p>Contrast this to the browser runtime environment. As a developer, you have no control over the version of browsers your clients use to access your app.</p>
<h3 id="heading-loading-modules-import-vs-require-keywords">Loading modules (<code>import</code> vs <code>require</code> keywords)</h3>
<p>Node.js offers out-of-the-box support for CommonJS and ES modules. You can load modules using the <code>require</code> keyword (CommonJS syntax) and the <code>import</code> keyword (ES syntax).</p>
<p>Some modern browsers support ES modules. This means you can use <code>import</code> ES modules. But you will still need to create bundles to cater to older browsers that do not support ES modules.</p>
<h2 id="heading-how-much-javascript-do-you-need-to-get-started-with-node">How Much JavaScript Do You Need to Get Started with Node?</h2>
<p>If you are an absolute beginner to JavaScript, I recommend that you start with the basics.</p>
<p>Become familiar with basic JavaScript concepts first. Then, you can move on to learning to build server-side applications with Node.js.</p>
<p>There's no way you'll ever exhaust all there is to learn about JavaScript. So, how to determine when you know enough JavaScript to get started with Node.js?</p>
<p>The Node.js documentation provides a <a target="_blank" href="https://nodejs.org/en/learn/getting-started/how-much-javascript-do-you-need-to-know-to-use-nodejs">list of JavaScript topics to learn</a> before diving deep with Node.js.</p>
<p>Once you have a grasp of JavaScript basics, then you can get started with Node.js</p>
<h2 id="heading-how-to-get-started-with-nodejs">How to Get Started with Node.js</h2>
<p>Let's see how you can create your first Node.js application. This section will show you how to run Node.js scripts from the command line.</p>
<h3 id="heading-how-to-download-and-install-nodejs">How to download and install Node.js</h3>
<p>First, you need to download and install Node.js. There are different ways you can do that. If you are a beginner, I would suggest that you <a target="_blank" href="https://nodejs.org/en/download/">download Node.js from the official website</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/node1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Screenshot of the official Node.js website</em></p>
<p>Official packages are available on the website for all major platforms (Windows, macOS, and Linux). Download and install the appropriate package for your system.</p>
<h3 id="heading-how-to-check-the-nodejs-version">How to check the Node.js version</h3>
<p>To check the Node.js version, run the command <code>node --version</code> in your terminal.<br>If the installation was successful, you will see the version of Node.js you installed. You should get a response like the screenshot below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/node2.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-run-nodejs-from-the-command-line">How to run Node.js from the command line</h3>
<p>Let's build a simple <code>Hello World</code> app.</p>
<p>Create a new project folder. You can call it <code>my-project.</code> Open the project in your code editor. Inside the folder, create an <code>app.js</code> file.</p>
<p>Add the following code to <code>app.js</code></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/node3.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>As you can see, this is JavaScript code.</p>
<p>You can run the script in the command line by running the command <code>node &lt;fileName&gt;</code>. In this case, the file name is <code>app.js</code>.</p>
<p>Run the following command in your terminal to execute the <code>Hello world.</code> program:</p>
<pre><code class="lang-bash">node app.js
</code></pre>
<p>You should see the string "Hello world." logged in your terminal like so.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/node4.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Congratulations! You just ran your first Node.js application.</p>
<h2 id="heading-should-you-learn-nodejs">Should You Learn Node.js?</h2>
<p>Here are some reasons why you should consider learning Node.js</p>
<h3 id="heading-nodejs-allows-you-to-write-javascript-on-both-client-and-server">Node.js allows you to write JavaScript on both client and server.</h3>
<p>One of the advantages of Node.js is that it allows you to work on both the front-end and back-end of your application. And you use one programming language – JavaScript – to do so.</p>
<p>This is good news for front-end developers who work with JavaScript. If you want to start working on the server side, it's easier compared to learning a new back-end language from scratch.</p>
<h3 id="heading-node-has-a-vibrant-community">Node has a vibrant community.</h3>
<p>As I mentioned earlier in the article, Node.js is open-sourced. It is actively maintained by developers from all over the world.</p>
<p>There is a vibrant community surrounding Node.js. You can find excellent tutorials and solutions to problems when you get stuck.</p>
<h3 id="heading-node-is-built-on-top-of-google-chromes-v8-engine">Node is built on top of Google Chrome's V8 engine.</h3>
<p>Node.js is built on top of the Chrome V8 JavaScript engine. This is significant because the V8 engine powers some of Google's in-browser applications like Gmail. As such, Google invests heavily to ensure it offers high performance.</p>
<h3 id="heading-demand-in-the-market">Demand in the market</h3>
<p>Many big names like Netflix, Uber, Paypal, and LinkedIn, and others use Node.js. Apart from the big names, many startups also use Node.js in developing their applications.</p>
<p>Learning to work with Node.js will make you a desirable candidate in the job market.</p>
<h3 id="heading-the-npm-library">The NPM library</h3>
<p>The NPM library is one of the excellent resources that comes with Node.js.<br>The library contains a registry of over a million packages. A package is a reusable piece of code.</p>
<p>You can create a package for a recurring task or problem and share the code with others via the registry.</p>
<p>You can also download packages that others have shared. For many tasks that developers perform regularly, there are packages available for that.</p>
<h2 id="heading-resources-to-learn-node">Resources to Learn Node</h2>
<p>If you are curious about learning how to build Node.js applications, I recommend the following resources.</p>
<ul>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=Oe421EPjeBE">8-Hour Node.js and Express.js Course on freeCodeCamp YouTube Channel</a>.</p>
</li>
<li><p><a target="_blank" href="https://www.freecodecamp.org/learn/back-end-development-and-apis/">The freeCodeCamp Backend Development and APIs curriculum</a></p>
</li>
<li><p><a target="_blank" href="https://nodejs.dev/en/learn">Nodejs.dev Documentation</a></p>
</li>
</ul>
<p>Also, below is a link to a video of Ryan Dahl when he first presented Node.js.</p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=ztspvPYybIY">Ryan Dahl: Original Node.js presentation at JSConf 2009</a></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>A single blog post like this is not enough to learn all there is to know about Node.js. The purpose of this article was to give you an overview of what Node.js is.</p>
<p>If you were not sure what Node.js is, I hope this article addressed your concerns and cleared your confusion.</p>
<p>Thanks for reading. And happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Validate URLs in JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ A Uniform Resource Locator (URL) is what leads you to a page or file on the internet. URLs serve as the addresses of things on the internet. All valid URLs follow certain patterns. So if you know those patterns, you can determine whether a URL is val... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-validate-urls-in-javascript/</link>
                <guid isPermaLink="false">66d45dde51f567b42d9f8447</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Regex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ url ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Benjamin Semah ]]>
                </dc:creator>
                <pubDate>Tue, 22 Nov 2022 04:12:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/08/jase-bloor-oCZHIa1D4EU-unsplash--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A Uniform Resource Locator (URL) is what leads you to a page or file on the internet. URLs serve as the addresses of things on the internet.</p>
<p>All valid URLs follow certain patterns. So if you know those patterns, you can determine whether a URL is valid or not in your program and give feedback, throw an error, and so on.</p>
<p>In this tutorial, you will learn three methods to check if a string in JavaScript is a valid URL:</p>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-to-use-the-url-constructor-to-validate-urls">How to Use the <code>URL</code> Constructor to Validate URLs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-npm-packages-to-validate-urls">How to Use npm Packages to Validate URLs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-regex-to-validate-urls">How to Use Regex to Validate URLs</a></p>
</li>
</ul>
<h2 id="heading-how-to-use-the-url-constructor-to-validate-urls">How to Use the <code>URL</code> Constructor to Validate URLs</h2>
<p>When you pass a string to the <code>URL</code> constructor, it returns a new <code>URL</code> object if a string is a valid URL. Otherwise, it returns an error:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fccUrl = <span class="hljs-keyword">new</span> URL(<span class="hljs-string">"https://www.freecodecamp.org/"</span>);
<span class="hljs-built_in">console</span>.log(fccUrl);
</code></pre>
<p>The following is what you get when you log <code>fccUrl</code> to the console:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/validURL.PNG" alt="A URL object in JavaScript" width="600" height="400" loading="lazy"></p>
<p><em>A</em> <code>URL</code> object in JavaScript</p>
<p>This object means that the string you passed to the <code>URL</code> constructor was a valid URL.</p>
<p>Now let's see what you get when you pass an invalid URL string:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fccUrl = <span class="hljs-keyword">new</span> URL(<span class="hljs-string">'freecodecamp'</span>);
<span class="hljs-built_in">console</span>.log(fccUrl);
</code></pre>
<p>The string <code>'freecodecamp'</code> is not a valid URL. Thus, you get the following <code>TypeError</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/invalidURL.PNG" alt="A TypeError after passing an invalid URL to the URL constructor" width="600" height="400" loading="lazy"></p>
<p><em>Invalid URL</em></p>
<p>To recap:</p>
<ol>
<li><p>When you pass a valid URL string to the <code>URL</code> constructor, it returns a new <code>URL</code> object.</p>
</li>
<li><p>When you pass an invalid URL string to the <code>URL</code> constructor, it returns a <code>TypeError</code>.</p>
</li>
</ol>
<p>With this knowledge, you can create a custom function to check the validity of a given URL string.</p>
<h3 id="heading-how-to-create-a-url-validator-function-with-the-url-constructor">How to Create a URL Validator Function with the <code>URL</code> Constructor</h3>
<p>By using the <code>URL</code> constructor and a <code>try...catch</code> statement, you can create a custom <code>isValidUrl</code> function:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isValidUrl</span>(<span class="hljs-params">string</span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">new</span> URL(string);
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
  } <span class="hljs-keyword">catch</span> (err) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }
}
</code></pre>
<p>The <code>isValidUrl</code> function returns <code>true</code> when the string you pass as an argument is a valid URL. Otherwise, it returns <code>false</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'https://www.freecodecamp.org/'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'mailto://mail@freecodecamp.org'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'freecodecamp'</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="heading-how-to-validate-only-http-urls-with-the-url-constructor">How to Validate Only HTTP URLs with the <code>URL</code> Constructor</h3>
<p>Sometimes, you may want to check if the string is a valid HTTP URL, and reject other valid URLs like <code>'mailto://mail@freecodecamp.org'</code>.</p>
<p>If you look closely at the <code>URL</code> object, one of its properties is <code>protocol</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/protocol.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The</em> <code>URL</code> object has a protocol property.</p>
<p>In the example above, the value of the protocol property is <code>'https:'</code>.</p>
<p>To check if a string is a valid HTTP URL, you can use the protocol property of the URL object:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isValidHttpUrl</span>(<span class="hljs-params">string</span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> newUrl = <span class="hljs-keyword">new</span> URL(string);
    <span class="hljs-keyword">return</span> newUrl.protocol === <span class="hljs-string">'http:'</span> || newUrl.protocol === <span class="hljs-string">'https:'</span>;
  } <span class="hljs-keyword">catch</span> (err) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }
}

<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'https://www.freecodecamp.org/'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'mailto://mail@freecodecamp.org'</span>)); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'freecodecamp'</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<p>The difference here is that you're not returning <code>true</code> after the new <code>URL</code> object is created. Instead, you're checking if the <code>protocol</code> property has a value equal to <code>'http:'</code> or <code>'https:'</code> and returning <code>true</code> if it is and <code>false</code> if not.</p>
<h2 id="heading-how-to-use-npm-packages-to-validate-urls">How to Use npm Packages to Validate URLs</h2>
<p>There are two NPM packages you can use: <code>is-url</code> and <code>is-url-http</code>.</p>
<p>These packages are the simplest way to check if a string is a valid URL. All you need to do is pass in a string as a parameter, and they will return <code>true</code> or <code>false</code>.</p>
<p>Let's see how both of these packages work.</p>
<h3 id="heading-how-to-validate-urls-with-the-is-url-package">How to Validate URLs with the <code>is-url</code> Package</h3>
<p>You can use the <code>is-url</code> package to check if a string is a valid URL. This package does not check the protocol of the URL passed to it.</p>
<p>To use <code>is-url</code>, first install it using the command below:</p>
<pre><code class="lang-javascript">npm install is-url
</code></pre>
<p>Then import it and pass your URL string to it as an argument:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> isUrl <span class="hljs-keyword">from</span> <span class="hljs-string">'is-url'</span>;

<span class="hljs-keyword">const</span> firstCheck = isUrl(<span class="hljs-string">'https://www.freecodecamp.org/'</span>);
<span class="hljs-keyword">const</span> secondCheck = isUrl(<span class="hljs-string">'mailto://mail@freecodecamp.org'</span>);
<span class="hljs-keyword">const</span> thirdCheck = isUrl(<span class="hljs-string">'freeCodeCamp'</span>);

<span class="hljs-built_in">console</span>.log(firstCheck); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(secondCheck); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(thirdCheck); <span class="hljs-comment">// false</span>
</code></pre>
<p>The <code>is-url</code> package returns <code>true</code> for strings that have valid URL formats and <code>false</code> for strings that have invalid URL formats.</p>
<p>In the example, both <code>firstCheck</code> (with the <code>https:</code> protocol) and <code>secondCheck</code> (with the <code>mailto:</code> protocol) return <code>true</code>.</p>
<h3 id="heading-how-to-validate-http-urls-with-the-is-http-url-package">How to Validate HTTP URLs with the <code>is-http-url</code> Package</h3>
<p>You can use the <code>is-url-http</code> package to check if a string is a valid HTTP URL.</p>
<p>Install the package with the following command:</p>
<pre><code class="lang-javascript">npm install is-url-http
</code></pre>
<p>Then import it and pass the URL string to it like so:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> isUrlHttp <span class="hljs-keyword">from</span> <span class="hljs-string">'is-url-http'</span>;

<span class="hljs-keyword">const</span> firstCheck = isUrlHttp(<span class="hljs-string">'https://www.freecodecamp.org/'</span>);
<span class="hljs-keyword">const</span> secondCheck = isUrlHttp(<span class="hljs-string">'mailto://freecodecamp@mail.org'</span>);
<span class="hljs-keyword">const</span> thirdCheck = isUrlHttp(<span class="hljs-string">'freeCodeCamp'</span>);

<span class="hljs-built_in">console</span>.log(firstCheck); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(secondCheck); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(thirdCheck); <span class="hljs-comment">// false</span>
</code></pre>
<p>In this example, only <code>firstCheck</code> returns <code>true</code>. The <code>is-url-http</code> package is not only checks that the string is a valid URL, it also checks if it's a valid HTTP URL. That is why it returns <code>false</code> for <code>secondCheck</code>, which is not a valid HTTP URL.</p>
<h2 id="heading-how-to-use-regex-to-validate-urls">How to Use Regex to Validate URLs</h2>
<p>You can also use regex, or a regular expression, to check if a string is a valid URL or not.</p>
<p>All valid URLs follow a particular pattern. They have three main parts, which are:</p>
<ul>
<li><p>Protocol</p>
</li>
<li><p>Domain name (or IP address)</p>
</li>
<li><p>Port and path</p>
</li>
</ul>
<p>Sometimes a query string or fragment locator follows the path.</p>
<p>You can learn more about URL patterns from this <a target="_blank" href="https://www.freecodecamp.org/news/what-happens-when-you-hit-url-in-your-browser/">freeCodeCamp article on the structure of URLs</a>.</p>
<p>Knowing the pattern URLs are made of, you can use regex to check for the existence of such patterns in a string. If the patterns exist, then the string passes the regex test. Otherwise, it fails.</p>
<p>Also, using regex, you can check for all valid URLs, or only check for valid HTTP URLs.</p>
<h3 id="heading-how-to-validate-urls-with-regex">How to Validate URLs with Regex</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isValidUrl</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> pattern = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(
    <span class="hljs-string">'^([a-zA-Z]+:\\/\\/)?'</span> + <span class="hljs-comment">// protocol</span>
      <span class="hljs-string">'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'</span> + <span class="hljs-comment">// domain name</span>
      <span class="hljs-string">'((\\d{1,3}\\.){3}\\d{1,3}))'</span> + <span class="hljs-comment">// OR IP (v4) address</span>
      <span class="hljs-string">'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'</span> + <span class="hljs-comment">// port and path</span>
      <span class="hljs-string">'(\\?[;&amp;a-z\\d%_.~+=-]*)?'</span> + <span class="hljs-comment">// query string</span>
      <span class="hljs-string">'(\\#[-a-z\\d_]*)?$'</span>, <span class="hljs-comment">// fragment locator</span>
    <span class="hljs-string">'i'</span>
  );
  <span class="hljs-keyword">return</span> pattern.test(str);
}

<span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'https://www.freecodecamp.org/'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'mailto://freecodecamp.org'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidUrl(<span class="hljs-string">'freeCodeCamp'</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<p>The regex in the <code>isValidUrl</code> function above checks if a string is a valid URL. The protocol check <code>^([a-zA-Z]+:\\/\\/)?</code> is not limited to just <code>https:</code>.</p>
<p>This is why the second example with the <code>mailto:</code> protocol returns <code>true</code>.</p>
<h3 id="heading-how-to-validate-http-urls-with-regex">How to Validate HTTP URLs with Regex</h3>
<p>To use regex to check if a string is a valid HTTP URL, you need to edit the protocol check.</p>
<p>Instead of <code>^([a-zA-Z]+:\\/\\/)?</code>, you should use <code>'^(https?:\\/\\/)?'</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isValidHttpUrl</span>(<span class="hljs-params">str</span>) </span>{
  <span class="hljs-keyword">const</span> pattern = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(
    <span class="hljs-string">'^(https?:\\/\\/)?'</span> + <span class="hljs-comment">// protocol</span>
      <span class="hljs-string">'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'</span> + <span class="hljs-comment">// domain name</span>
      <span class="hljs-string">'((\\d{1,3}\\.){3}\\d{1,3}))'</span> + <span class="hljs-comment">// OR ip (v4) address</span>
      <span class="hljs-string">'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'</span> + <span class="hljs-comment">// port and path</span>
      <span class="hljs-string">'(\\?[;&amp;a-z\\d%_.~+=-]*)?'</span> + <span class="hljs-comment">// query string</span>
      <span class="hljs-string">'(\\#[-a-z\\d_]*)?$'</span>, <span class="hljs-comment">// fragment locator</span>
    <span class="hljs-string">'i'</span>
  );
  <span class="hljs-keyword">return</span> pattern.test(str);
}

<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'https://www.freecodecamp.org/'</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'mailto://freecodecamp.org'</span>)); <span class="hljs-comment">// false</span>
<span class="hljs-built_in">console</span>.log(isValidHttpUrl(<span class="hljs-string">'freeCodeCamp'</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<p>Now only the first example which has a valid <code>https:</code> protocol returns <code>true</code>. Note that URL strings with <code>http:</code> work, too.</p>
<h2 id="heading-wrapping-up">Wrapping up!</h2>
<p>In this article, you learned how to check the validity of URLs in JavaScript. You now know the following three methods for doing so.</p>
<ul>
<li><p>How to Use the <code>URL</code> Constructor to Validate URLs</p>
</li>
<li><p>How to Use npm Packages to Validate URLs (<code>is-url</code> and <code>is-http-url</code>)</p>
</li>
<li><p>How to Use Regex to Validate URLs</p>
</li>
</ul>
<p>It's up to you to choose which method you're comfortable working with.</p>
<p>Thanks for reading. And happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Error: cannot find module [Node npm Error Solved] ]]>
                </title>
                <description>
                    <![CDATA[ If you’re a developer that works with Node JS and JavaScript libraries and frameworks like React, Vue, and Angular, then you might have encountered the "Error: cannot find module" error. In this article, I’m going to show you how to fix the error. Wh... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/error-cannot-find-module-node-npm-error-solved/</link>
                <guid isPermaLink="false">66adf0c46778e7bd69427bf3</guid>
                
                    <category>
                        <![CDATA[ error ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Wed, 09 Nov 2022 15:37:57 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/11/factory-4757647_1280.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you’re a developer that works with Node JS and JavaScript libraries and frameworks like React, Vue, and Angular, then you might have encountered the "Error: cannot find module" error.</p>
<p>In this article, I’m going to show you how to fix the error.</p>
<h2 id="heading-why-the-error-cannot-find-module-occurs">Why the "Error: cannot find module" Occurs</h2>
<p>This error occurs because of the following reasons: </p>
<ul>
<li>you’re trying to import an item from a module you don’t have installed in your project directory</li>
<li>you’re importing some things from an outdated package</li>
<li>you’re pointing to a file that does not exist</li>
</ul>
<p>In the screenshot below, you can see that I’m getting the error:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/ss1.png" alt="ss1" width="600" height="400" loading="lazy"></p>
<p>I’m getting the error because I’m trying to import the freeCodeCamp icon from the react-icons package, which I don’t have installed.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { FaFreeCodeCamp } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-icons/fa"</span>;
</code></pre>
<h2 id="heading-how-to-fix-the-cannot-find-module-error">How to Fix the "cannot find module" Error</h2>
<p>If you get this error, the solution is always in the error. The module (package) not found is always specified in the format "Module not found: Error: Can't resolve 'package name' in 'project directory". </p>
<p>In my case, I got it like this "Module not found: Error: Can't resolve 'react-icons/fa' in 'C:\Users\user\Desktop\Projects\Address Locator\address-locator\src'".</p>
<p>To fix the error, you need to install the package that is absent in your project directory – <code>npm install package-name</code> or <code>yarn add package-name</code>.</p>
<p>In my case, I need to install the <code>react-icons</code> package so the freeCodeCamp icon can be resolved. I’ll do that by running <code>yarn add react-icons</code>.</p>
<p>Once I install the package and run the app, everything should successfully compile:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/11/ss2.png" alt="ss2" width="600" height="400" loading="lazy"> </p>
<p>If you install the package but you still get the error, then follow the steps below:</p>
<ul>
<li>delete the node modules folder by running <code>rm -rf node_modules</code></li>
<li>delete package.lock.json file by running <code>rm -f package-lock.json</code></li>
<li>clean up the NPM cache by running <code>npm cache clean --force</code></li>
<li>install all packages again by running <code>npm install</code></li>
</ul>
<p>That should fix the error for you.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>When you get the “cannot find module” error, or “module not found”, it means you’ve not installed the package you’re trying to use. </p>
<p>If the error occurs even if you have the package installed, then the fixes suggested in this article can help you out.</p>
<p>Thank you for reading.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ NVM for Windows – How to Download and Install Node Version Manager in Windows 10 ]]>
                </title>
                <description>
                    <![CDATA[ Different software development tools might require specific versions of Node.js and NPM (Node Package Manager). NPM is a tool for managing packages installed from the NPM registry. In addition, if you are making an NPM package, you might need to test... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/nvm-for-windows-how-to-download-and-install-node-version-manager-in-windows-10/</link>
                <guid isPermaLink="false">66adf1abf452caf50fb1fe07</guid>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Thu, 11 Aug 2022 16:00:37 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/nvmWindows.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Different software development tools might require specific versions of Node.js and NPM (Node Package Manager). NPM is a tool for managing packages installed from the NPM registry.</p>
<p>In addition, if you are making an NPM package, you might need to test it with different versions of Node.js. This is why you should have NVM installed.</p>
<p>NVM, short for Node Version Manager, is a command line tool for managing and switching to different versions of Node.js.</p>
<p>In this article, I will show you how to download and install NVM on Windows 10 – even though there’s no “NVM” for Windows.</p>
<p>I will also show you how to set up and use different versions of Node.js and NPM on your Windows computer.</p>
<h2 id="heading-what-well-cover">What We'll Cover</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-download-and-install-node-version-manager-in-windows-10">How to Download and Install Node Version Manager in Windows 10</a><ul>
<li><a class="post-section-overview" href="#heading-follow-the-steps-below-to-download-nvm-windows">Follow the steps below to download nvm-windows</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-how-to-use-nvm-on-windows-10">How to Use NVM on Windows 10</a><ul>
<li><a class="post-section-overview" href="#heading-how-to-install-different-versions-of-nodejs-and-npm-with-nvm">How to Install Different Versions of Node.js and NPM with NVM</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-recap">Recap</a></li>
</ul>
<h2 id="heading-how-to-download-and-install-node-version-manager-in-windows-10">How to Download and Install Node Version Manager in Windows 10</h2>
<p>As I mentioned earlier, there’s no “NVM” for Windows, as NVM is only supported on Linux and Mac. </p>
<p>What you will be using on your Windows machine is “nvm-windows”. nvm-windows is similar to NVM, but not identical to it.</p>
<p><strong>N.B.</strong>: If you have Node.js installed already, you need to uninstall it so it doesn’t lead to errors when using different versions of Node and installing packages from the NPM registry. </p>
<p>Restart your PC after that, open the command prompt or PowerShell, and run <code>node -v</code> to confirm Node has been uninstalled.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/ss1-2.png" alt="ss1-2" width="600" height="400" loading="lazy"></p>
<p>In addition, if you have yarn installed, uninstall it and reinstall it after installing NVM. You don’t want to get weird errors while installing and using packages from the NPM registry.</p>
<h3 id="heading-follow-the-steps-below-to-download-nvm-windows">Follow the steps below to download nvm-windows</h3>
<ul>
<li><strong>Step 1</strong>: Head over to the <a target="_blank" href="https://github.com/coreybutler/nvm-windows#installation--upgrades">nvm-windows repository</a> and click on Download Now!”
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss2-2.png" alt="ss2-2" width="600" height="400" loading="lazy"></li>
</ul>
<p>You’ll be taken to a page containing different versions of nvm-windows.</p>
<ul>
<li><p><strong>Step 2</strong>: Click on the latest version to download it. For now, it is the April 28, 2022 version.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss3-2.png" alt="ss3-2" width="600" height="400" loading="lazy"></p>
</li>
<li><p><strong>Step 3</strong>: Locate the installer on your computer and open it. Follow the installation wizard to install it.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss4-2.png" alt="ss4-2" width="600" height="400" loading="lazy">
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss5-3.png" alt="ss5-3" width="600" height="400" loading="lazy"></p>
</li>
<li><p><strong>Step 4</strong>: Open up PowerShell or Command Prompt and run <code>nvm -v</code> to confirm the installation.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss6-2.png" alt="ss6-2" width="600" height="400" loading="lazy"></p>
</li>
</ul>
<p>If you get the same message I got above, then nvm-windows has been successfully installed. Congrats!</p>
<h2 id="heading-how-to-use-nvm-on-windows-10">How to Use NVM on Windows 10</h2>
<p>To use NVM, you need to open PowerShell or Command Prompt as an admin. You can also use Git bash.</p>
<ul>
<li><p>To open PowerShell as admin, right-click on Start and select “PowerShell (Admin)”.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/powershell-admin.png" alt="powershell-admin" width="600" height="400" loading="lazy"></p>
</li>
<li><p>To open Command Prompt as admin, search for “cmd” and select “Open as Administrator” on the right.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/cmd-admin.png" alt="cmd-admin" width="600" height="400" loading="lazy"></p>
</li>
</ul>
<h3 id="heading-how-to-install-different-versions-of-nodejs-and-npm-with-nvm">How to Install Different Versions of Node.js and NPM with NVM</h3>
<p>The superpower NVM gives you is the ability to have multiple versions of Node.js installed on your machine. </p>
<p>To install the latest version of Node, run <code>nvm install latest</code>.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss7-1.png" alt="ss7-1" width="600" height="400" loading="lazy"></p>
<p>It is always better to install the long-term support (LTS) version of Node because it is less buggy.</p>
<p>To install the LTS version of Node, run <code>nvm install lts</code>.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss8-1.png" alt="ss8-1" width="600" height="400" loading="lazy"></p>
<p>To install a specific version of Node, you need to run <code>nvm list available</code> first so you can see the versions of Node that are available.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss9-1.png" alt="ss9-1" width="600" height="400" loading="lazy"></p>
<p>To install that specific version, run <code>nvm install node-version-number</code>. For example, <code>nvm install 14.20.0</code>.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss10-1.png" alt="ss10-1" width="600" height="400" loading="lazy"></p>
<p><strong>N.B.</strong>: Once you install a version of Node, the corresponding version of NPM is installed for you. So you don’t need to install NPM separately. </p>
<p>If the version of NPM you want to use is not available, run <code>npm install @npm version-number -g</code> to install it.</p>
<p>Now, to see the list of Node versions you have installed on your Windows machine, run <code>nvm list</code>.
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss11-1.png" alt="ss11-1" width="600" height="400" loading="lazy"></p>
<p>To use a specific version of Node, run: </p>
<ul>
<li><code>nvm use latest</code> to use the latest version </li>
<li><code>nvm use lts</code> to use the long-term support version</li>
<li><code>nvm use version-number</code> to use any other version you have installed
<img src="https://www.freecodecamp.org/news/content/images/2022/08/ss12-1.png" alt="ss12-1" width="600" height="400" loading="lazy"></li>
</ul>
<h2 id="heading-recap">Recap</h2>
<p>This article showed you how to install NVM on Windows 10 (nvm-windows) and how to use it to install and manage different versions of Node.</p>
<p>As a reminder, here are the common commands you’ll be using with nvm-windows:</p>
<ul>
<li><code>nvm install node-version</code> – install a version of Node </li>
<li><code>nvm list</code> – see the versions of Node you have installed on your machine</li>
<li><code>nvm use node-version</code> – use a specific version of Node</li>
</ul>
<p>Thank you for reading and keep coding :)</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Update NPM Dependencies ]]>
                </title>
                <description>
                    <![CDATA[ The Node Package Manager (npm) provides various features to help you install and maintain your project's dependencies. Dependencies can become outdated over time due to bug fixes, new features, and other updates. The more project dependencies you hav... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-update-npm-dependencies/</link>
                <guid isPermaLink="false">66bc4cae73c9920bb20c0e01</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Natalie Pina ]]>
                </dc:creator>
                <pubDate>Tue, 05 Jul 2022 22:55:20 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/07/deps.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The Node Package Manager (npm) provides various features to help you install and maintain your project's dependencies.</p>
<p>Dependencies can become outdated over time due to bug fixes, new features, and other updates. The more project dependencies you have, the harder it is to keep up with these updates. </p>
<p>Outdated packages can pose a threat to security and can have negative effects on performance. Up-to-date packages prevent vulnerabilities. This means that periodic dependency checks and updates are important.</p>
<h2 id="heading-how-to-keep-dependencies-up-to-date">How to Keep Dependencies Up-To-Date</h2>
<p>Now, you could go through each individual package in package.json one by one to change the version and run <code>npm install &lt;package&gt;@latest</code> to get the latest version. But this isn't going to be the most efficient method. </p>
<p>Imagine if you had 20 or more packages that could use a version bump. Instead, you should develop a workflow to periodically check for new versions before the number of outdated dependencies grows and it becomes increasingly harder to upgrade.</p>
<p>Here's a workflow that helps me stay on top of updates: first, discover which packages need to be updated and how far behind the versions are. Next, choose to update packages individually or together in a batch. Always test out the updates to ensure breaking changes haven't occurred.</p>
<p>I prefer to perform major version updates individually. With major updates, you're likely to encounter breaking changes. It's much easier to undo or address code changes in relation to one package compared to many.</p>
<p>In this article, I will go over methods to inspect and upgrade dependencies in detail.</p>
<h2 id="heading-how-to-use-the-npm-outdated-command">How to Use the <code>npm outdated</code> Command</h2>
<pre><code>npm outdated
</code></pre><p>This command will check every installed dependency and compare the current version with the latest version in the <a target="_blank" href="https://www.npmjs.com/">npm registry</a>. It is printed out into a table outlining available versions.</p>
<p>It is built into npm so there are no additional packages required to download. <code>npm outdated</code> is a good place to start for an overview of the number of dependency updates required.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/07/Screen-Shot-2022-07-03-at-1.14.41-PM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<ul>
<li>Current is the current version installed.</li>
<li>Wanted is the max version of the package according the <a target="_blank" href="https://docs.npmjs.com/misc/semver">semver</a> range.</li>
<li>Latest is the version of the package tagged as latest in the npm registry.</li>
</ul>
<p>With this method, to install updates for every package, you just need to run:</p>
<pre><code>npm update
</code></pre><p>Keep in mind that with <code>npm update</code> it will never update to a major breaking-changes version. It updates the dependencies in package.json and package-lock.json. It will use the "wanted" version. </p>
<p>To obtain the "latest" version append <code>@latest</code>to individual installs, for example <code>npm install react@latest</code>. </p>
<h2 id="heading-how-to-use-npm-check-updates">How to Use <code>npm-check-updates</code></h2>
<p>For an advanced and customizable upgrading experience, I'd recommend <a target="_blank" href="https://www.npmjs.com/package/npm-check-updates"><code>npm-check-updates</code></a>. This package can do everything <code>npm outdated</code> and <code>npm upgrade</code> can do with some added customization options. It does require a package installation, however.</p>
<p>To get started, install the <a target="_blank" href="https://www.npmjs.com/package/npm-check-updates"><code>npm-check-updates</code></a> package globally:</p>
<pre><code>npm install -g npm-check-updates
</code></pre><p>Then, run <code>ncu</code> to display packages to upgrade. Similar to <code>npm outdated</code> it will not apply any changes.</p>
<pre><code>ncu
Checking package.json
[====================] <span class="hljs-number">12</span>/<span class="hljs-number">12</span> <span class="hljs-number">100</span>%

 @testing-library/user-event    ^<span class="hljs-number">13.5</span><span class="hljs-number">.0</span>  →  ^<span class="hljs-number">14.2</span><span class="hljs-number">.1</span>
 @types/jest                    ^<span class="hljs-number">27.5</span><span class="hljs-number">.2</span>  →  ^<span class="hljs-number">28.1</span><span class="hljs-number">.4</span>
 @types/node                  ^<span class="hljs-number">16.11</span><span class="hljs-number">.42</span>  →  ^<span class="hljs-number">18.0</span><span class="hljs-number">.1</span>

Run ncu -u to upgrade package.json
</code></pre><p>To upgrade dependencies, you just need to run:</p>
<pre><code>ncu --upgrade

<span class="hljs-comment">// or </span>
ncu -u
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2022/07/screenshot.png" alt="Image" width="600" height="400" loading="lazy">
<em>Resource: <a target="_blank" href="https://www.npmjs.com/package/npm-check-updates">npm-check-updates</a></em></p>
<ul>
<li>Red = major</li>
<li>Cyan = minor</li>
<li>Green = patch</li>
</ul>
<p>This updates dependencies in only the package.json file and will select the latest version even if it includes a breaking change. With this method, <code>npm install</code> is not run automatically so be sure to run that afterward to update package-lock.json.</p>
<p>To choose your preferred version type, run <code>ncu --target [patch, minor, latest, newest, greatest]</code>. </p>
<h3 id="heading-how-to-use-interactive-mode-with-npm-check-updates">How to Use Interactive Mode with <code>npm-check-updates</code></h3>
<pre><code>ncu --interactive

<span class="hljs-comment">// or </span>
ncu -i
</code></pre><p>Interactive mode allows you to select specific packages to update. By default, all packages are selected. </p>
<p>Navigate down through each package and use space to deselect, and enter when you are ready to upgrade all of the selected packages. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/07/175337598-cdbb2c46-64f8-44f5-b54e-4ad74d7b52b4.png" alt="Image" width="600" height="400" loading="lazy">
<em>Resource: <a target="_blank" href="https://www.npmjs.com/package/npm-check-updates">npm-check-updates</a></em></p>
<p>There are several ways to elevate the interactive <code>npm-check-updates</code> experience. </p>
<pre><code class="lang-ncu">ncu --interactive --format group
</code></pre>
<p>This command groups and organizes packages into major, minor and patch releases. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/07/175336533-539261e4-5cf1-458f-9fbb-a7be2b477ebb--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Resource: <a target="_blank" href="https://www.npmjs.com/package/npm-check-updates">npm-check-updates</a></em></p>
<p><code>npm-check-updates</code> provides other useful tools such as <a target="_blank" href="https://github.com/raineorshine/npm-check-updates#doctor-mode">doctor mode</a> which installs upgrades and runs tests to check for breaking changes. </p>
<p>I highly suggest taking a look at the <a target="_blank" href="https://github.com/raineorshine/npm-check-updates">documentation</a> overall to learn more about all this package has to offer. The project is well-maintained along with a climbing weekly download rate of ~<a target="_blank" href="https://www.npmjs.com/package/npm-check-updates">294,467</a> at the time of writing this article.</p>
<h2 id="heading-summary">Summary</h2>
<p>Getting in the habit of regularly updating your dependencies will help your apps' security and performance. </p>
<p>Both <code>npm outdated</code> and <code>npm-check-updates</code> are useful tools to check for packages that could use a version bump. </p>
<p>I suggest trying both of these out to see which provides a better developer experience.</p>
<p>I hope these methods help along the path of updating!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Update Node and NPM to the Latest Version ]]>
                </title>
                <description>
                    <![CDATA[ By Dillion Megida Node is a runtime environment that allows developers to execute JavaScript code outside the browser, on the server-side.  NPM, on the other hand, is a package manager for publishing JavaScript packages (also known as Node modules) t... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-update-node-and-npm-to-the-latest-version/</link>
                <guid isPermaLink="false">66d84f214540581f645440ef</guid>
                
                    <category>
                        <![CDATA[ node js ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ update  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 12 Apr 2022 20:28:40 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/pexels-markus-winkler-4052195.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Dillion Megida</p>
<p>Node is a runtime environment that allows developers to execute JavaScript code outside the browser, on the server-side. </p>
<p>NPM, on the other hand, is a package manager for publishing JavaScript packages (also known as Node modules) to the <a target="_blank" href="https://www.npmjs.com/">npm registry</a>. You can also use it to install packages to your applications.</p>
<p>To install Node, you have to go to the <a target="_blank" href="https://nodejs.org/en/">Nodejs website</a> to download the installer. After downloading, you can run the installer, follow the steps, agree to the terms and conditions, and have the installer on your device.</p>
<p>When you install Node, you also get the <code>npm</code> CLI which you can use to manage packages in your applications.</p>
<p>However, Node and NPM can be updated separately to their latest versions, and in the rest of this article, I'll show you how.</p>
<h2 id="heading-how-to-update-node">How to Update Node</h2>
<h3 id="heading-1-use-npm-to-update-your-node-version">1. Use NPM to Update Your Node Version</h3>
<p>To update Node with NPM, you will install the <a target="_blank" href="https://www.npmjs.com/package/n">n</a> package, which will be used to interactively manage node versions on your device.</p>
<p>Here are the steps:</p>
<h4 id="heading-clear-the-npm-cache">Clear the NPM cache</h4>
<p>When you install dependencies, some modules are cached to improve the speed of installation in subsequent downloads. So first, you want to clear the NPM cache.</p>
<h4 id="heading-install-n">Install n</h4>
<pre><code class="lang-shell">npm install -g n
</code></pre>
<p>You'll need to install this package globally as it manages the Node versions at the root.</p>
<h4 id="heading-install-a-new-version-of-node">Install a new version of Node</h4>
<pre><code class="lang-shell">n lts
n latest
</code></pre>
<p>The two commands above install the long-term support and latest versions of Node.</p>
<h4 id="heading-remove-previously-installed-versions">Remove previously installed versions</h4>
<pre><code class="lang-shell">n prune
</code></pre>
<p>This command removes the cached versions of the previously installed versions and only keeps the latest installed version.</p>
<h3 id="heading-2-use-nvm-to-update-your-node-version">2. Use NVM to Update Your Node Version</h3>
<p>NVM stands for Node Version Manager, and as the name implies, it helps you manage your Node Versions. With NVM, you can install Node versions and specify the version of Node that a project uses.</p>
<p>NVM makes it easy to test projects across various Node versions.</p>
<p>To update a Node Version with NVM, you have to install NVM first.</p>
<p>Here is the <a target="_blank" href="https://github.com/nvm-sh/nvm#installing-and-updating">installation guide</a> for NVM.</p>
<p>When installed, you can install packages with:</p>
<pre><code class="lang-shell">nvm install [version]
</code></pre>
<p>You can install the latest version with:</p>
<pre><code class="lang-shell">nvm install node
</code></pre>
<p>And uninstall other versions with:</p>
<pre><code class="lang-shell">nvm uninstall [version]
</code></pre>
<p>With many versions installed, you may also want to specify the version to use at a particular time. One way to do this is by setting a default alias like this:</p>
<pre><code class="lang-shell">nvm alias default [version]
</code></pre>
<p>This way, Node executions will run with the specified version.</p>
<h3 id="heading-3-download-updated-node-binaries">3. Download Updated Node Binaries</h3>
<p>And you can also get the latest versions from the <a target="_blank" href="https://nodejs.org/en/">Node.js</a> website. On it, you can find the latest and long-term support versions for your device.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/04/image-7.png" alt="Image" width="600" height="400" loading="lazy">
<em>Node.js downloads page</em></p>
<p>Downloading the latest version also gives you the latest version of NPM.</p>
<h2 id="heading-how-to-update-npm">How to Update NPM</h2>
<p>Just as you use NPM to update packages, you can use NPM to update itself. Here's the command to achieve this:</p>
<pre><code class="lang-shell">npm install -g npm@latest
</code></pre>
<p>This command will install the latest version of NPM globally.</p>
<p>On Mac, you may have to pass the <code>sudo</code> command before NPM, as this installs NPM at the root of your device, and you need privileges to do that.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we've seen how to update Node and NPM to their latest versions.</p>
<p>To reiterate, when you install Node, you automatically get NPM. If you also update Node by installing the binaries from the website, you get an updated NPM.</p>
<p>We also saw other ways to update Node and NPM globally on your device.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Package Manager – Complete Guide to NPM and Yarn ]]>
                </title>
                <description>
                    <![CDATA[ A package manager is a tool developers use to automate finding, downloading, installing, configuring, upgrading, and removing a system's packages. This article will show you all you need to get started with package managers like NPM and Yarn. But why... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-package-manager-npm-and-yarn/</link>
                <guid isPermaLink="false">66ba0dfd7fb82b484b253a2b</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ software development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Yarn ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Oluwatobi Sofela ]]>
                </dc:creator>
                <pubDate>Tue, 05 Apr 2022 00:08:08 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/package-manager-npm-and-yarn-explained-curology-pDsmoI5j3B8-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A <strong>package manager</strong> is a tool developers use to automate finding, downloading, installing, configuring, upgrading, and removing a system's packages.</p>
<p>This article will show you all you need to get started with package managers like NPM and Yarn.</p>
<p>But why exactly do we need a package manager in our development workflow? Let's find out.</p>
<h2 id="heading-why-do-you-need-a-package-manager">Why Do You Need a Package Manager?</h2>
<p>Suppose there were no package managers. In that case, you would have to do the following manually:</p>
<ul>
<li>Find all the correct packages for your project</li>
<li>Verify that the packages don't have any known vulnerabilities</li>
<li>Download the packages</li>
<li>Install them at the appropriate location</li>
<li>Keep track of new updates for all your packages</li>
<li>Upgrade each package whenever there is a new release</li>
<li>Remove the packages you no longer need</li>
</ul>
<p>Manually managing tens or hundreds of packages is a tiresome and time-consuming endeavor.</p>
<p>Therefore, package managers—such as <a target="_blank" href="https://www.npmjs.com/">NPM</a>, <a target="_blank" href="https://pnpm.io/">pNPM</a>, <a target="_blank" href="https://bower.io/">Bower</a>, and <a target="_blank" href="https://yarnpkg.com/">Yarn</a>—help automate and eliminate the tedious process of managing all your packages manually.</p>
<p>Keep in mind that a package manager is not the same as a package registry. So, let's find out the main difference.</p>
<h2 id="heading-package-manager-vs-package-registry-whats-the-difference">Package Manager vs. Package Registry – What's the Difference?</h2>
<p>A <strong>package manager</strong> is a tool developers use to automatically find, download, install, configure, upgrade, and uninstall a computer's packages.</p>
<p>NPM (Node Package Manager) and Yarn (Yet Another Resource Negotiator) are two popularly used package managers.</p>
<p>A <strong>package registry</strong> is a database (storage) for thousands of packages (libraries, plugins, frameworks, or tools).</p>
<p>In other words, a package registry is the place packages get published to and installed from.</p>
<p><a target="_blank" href="https://www.npmjs.com/">NPM registry</a> and <a target="_blank" href="https://github.com/features/packages">GitHub Packages</a> are two popularly used package registries.</p>
<p>So, now that we know what a package manager is and why it is needed, we can discuss how to use the two popular ones—NPM and Yarn.</p>
<p>Note that there are numerous NPM vs. Yarn debates out there – so we will avoid them here because the best package manager is the one that works best for you.</p>
<p>Therefore, this article will show you how NPM and Yarn work rather than tell you which package manager is best. It is then up to you to decide which you prefer.</p>
<p>Alternatively, you can choose to use NPM for a specific project and Yarn for another—depending on which manager you believe is best suited for the job.</p>
<p>So, without any further ado, let's begin by learning how to install the two managers.</p>
<h2 id="heading-how-to-install-node-package-manager-npm">How to Install Node Package Manager (NPM)</h2>
<p>NPM gets installed automatically while installing Node.</p>
<p>Therefore, to get NPM installed on your system, go to the <a target="_blank" href="https://nodejs.org/en/">NodeJS</a> website and get Node's <a target="_blank" href="https://tamalweb.com/which-nodejs-version">latest LTS or the current version</a>.</p>
<h2 id="heading-how-to-install-yarn">How to Install Yarn</h2>
<p>It is best to install Yarn through NPM. So, first, install NPM from the <a target="_blank" href="https://nodejs.org/en/">Node.js</a> website.</p>
<p>Once you've installed NPM, proceed to install Yarn like so:</p>
<pre><code class="lang-bash">npm install -g yarn
</code></pre>
<h2 id="heading-how-to-check-the-installed-node-version">How to Check the Installed Node Version</h2>
<p>To check the Node.js version installed on your system, run:</p>
<pre><code class="lang-bash">node -v
</code></pre>
<p>The <code>-v</code> flag in the snippet above is a shorthand for <code>--version</code>.</p>
<h2 id="heading-how-to-check-the-installed-npm-version">How to Check the Installed NPM Version</h2>
<p>To check the NPM version installed on your system, run:</p>
<pre><code class="lang-bash">npm -v
</code></pre>
<h2 id="heading-how-to-check-the-installed-yarn-version">How to Check the Installed Yarn Version</h2>
<p>To check the Yarn version installed on your system, run:</p>
<pre><code class="lang-bash">yarn -v
</code></pre>
<h2 id="heading-how-to-upgrade-node-package-manager">How to Upgrade Node Package Manager</h2>
<p>Update to the latest NPM version by running:</p>
<pre><code class="lang-bash">npm install npm@latest -g
</code></pre>
<h2 id="heading-how-to-upgrade-nodejs">How to Upgrade NodeJS</h2>
<p>Suppose you wish to upgrade your Node.js installation. In that case, you have two options:</p>
<h3 id="heading-option-1-upgrade-via-the-nodejs-website">Option 1: Upgrade via the NodeJS website</h3>
<p>One way to upgrade your NodeJS installation is to manually download and install the latest version from the <a target="_blank" href="https://nodejs.org/en/">Node.js website</a>.</p>
<h3 id="heading-option-2-upgrade-via-a-version-management-tool">Option 2: Upgrade via a version management tool</h3>
<p>Another way to upgrade your NodeJS installation is to use a <a target="_blank" href="https://nodejs.org/en/download/package-manager/">version manager</a> such as <a target="_blank" href="https://github.com/nvm-sh/nvm">NVM</a>, <a target="_blank" href="https://github.com/tj/n">n</a>, or <a target="_blank" href="https://github.com/jasongin/nvs">nvs</a>.</p>
<h2 id="heading-how-to-upgrade-yarn">How to Upgrade Yarn</h2>
<p>Update to the latest Yarn version by running:</p>
<pre><code class="lang-bash">yarn <span class="hljs-built_in">set</span> version latest
</code></pre>
<p>So, now that we have NPM (or Yarn) on our computer, we can start using the installed manager to find, install, configure, and remove our project's packages.</p>
<p>But what exactly is a package? Let's find out.</p>
<h2 id="heading-what-exactly-is-a-package">What Exactly Is a Package?</h2>
<p>A <strong>package</strong> is a <a target="_blank" href="https://www.codesweetly.com/git-basic-introduction/#h-working-directory">directory</a> (or project) that has a <code>package.json</code> file used to record information about it.</p>
<p><strong>Note:</strong> You can only publish packages (a project described by a <code>package.json</code> file) to the <a target="_blank" href="https://docs.npmjs.com/cli/v6/using-npm/registry">NPM registry</a>.</p>
<h2 id="heading-how-to-install-packages">How to Install Packages</h2>
<p>There are two ways to install a package: locally or globally.</p>
<h3 id="heading-local-package-installation">Local package installation</h3>
<p>A locally installed package is one that you can use only in the project in which you've installed it.</p>
<p>To install a package locally, do the following:</p>
<ol>
<li>Navigate to the <a target="_blank" href="https://www.codesweetly.com/web-tech-glossary/#h-root-directory">root directory</a> of your project from the command line.</li>
<li>Install your package using the NPM or Yarn installation command below (depending on the package manager you've chosen to use for your project).</li>
</ol>
<p><strong>Note:</strong> You must have Node and NPM installed on your system for the NPM (and Yarn) installation commands below to work. You can get both by installing the latest LTS or the current version from the Node.js website.</p>
<h4 id="heading-npm-installation-command">NPM installation command</h4>
<pre><code class="lang-bash">npm install package-name --save
</code></pre>
<p>Note that the <code>--save</code> command above instructs NPM to save <code>package-name</code> in the <code>package.json</code> file as one of the packages on which the project depends.</p>
<p>Suppose you wish to install an exact version of a package. In such a case, add a <code>@[version-number]</code> after the package's name like so:</p>
<pre><code class="lang-bash">npm install package-name@4.14.1 --save
</code></pre>
<p>Alternatively, if the package you are installing is for development and testing purposes, use:</p>
<pre><code class="lang-bash">npm install package-name --save-dev
</code></pre>
<p>The commands above will cause NPM to download three items into your project's root directory: a <code>node_modules</code> folder, a <code>package.json</code> file, and a <code>package-lock.json</code> file. We will discuss these items in detail later on in this article.</p>
<h4 id="heading-yarn-installation-command">Yarn installation command</h4>
<pre><code class="lang-bash">yarn add package-name
</code></pre>
<p>Suppose you wish to install an exact version of a package. In such a case, add a <code>@[version-number]</code> after the package's name like so:</p>
<pre><code class="lang-bash">yarn add package-name@4.14.1
</code></pre>
<p>Alternatively, if the package you are installing is for development and testing purposes, use:</p>
<pre><code class="lang-bash">yarn add package-name --dev
</code></pre>
<p>The commands above will cause Yarn to download three items into your project's root directory: a <code>node_modules</code> folder, a <code>package.json</code> file, and a <code>yarn.lock</code> file. We will discuss these items in detail later on in this article.</p>
<p>So, now that we know how to install a package locally, we can discuss the global package installation.</p>
<h3 id="heading-global-package-installation">Global package installation</h3>
<p>A globally installed package is a package that you can use anywhere on your system.</p>
<p>To install a package globally, run the code below on your terminal:</p>
<pre><code class="lang-bash">npm install package-name -g
</code></pre>
<p>Alternatively, you can use Yarn like so:</p>
<pre><code class="lang-bash">yarn global add package-name
</code></pre>
<p>Note that you can run the commands above from any location on your system.</p>
<h3 id="heading-local-vs-global-package-installation">Local vs. global package installation</h3>
<p>Generally, it is better to install a package locally. Below are some of the differences between a local and global installation.</p>
<h4 id="heading-difference-1-installation-location">Difference 1: Installation location</h4>
<p>A locally installed package gets installed in the directory where you executed the <code>npm install package-name</code> (or <code>yarn add package-name</code>) command.</p>
<p>Specifically, you will find a project's locally installed packages in its <code>node_module</code> directory.</p>
<p>In contrast, a globally installed package gets installed in a single location on your system. The exact location depends on your system's configuration.</p>
<h4 id="heading-difference-2-package-versions">Difference 2: Package versions</h4>
<p>Suppose you installed your package locally. Then, you can use different versions of the same package for multiple app development.</p>
<p>However, you are forced to use the same package version for all your apps when you install globally.</p>
<h4 id="heading-difference-3-updates">Difference 3: Updates</h4>
<p>A local installation allows you to choose the project's packages you wish to upgrade to the latest version. This makes it easier to manage upgrades that break compatibility with other packages.</p>
<p>However, upgrading a globally installed package updates the package for all projects—which can cause maintenance nightmares if the upgrade breaks compatibility with other packages.</p>
<h4 id="heading-difference-4-usage-recommendation">Difference 4: Usage recommendation</h4>
<p>Global installation is best for packages you intend to use only on your command line—especially when they provide executable commands reusable across projects.</p>
<p>However, local installation is best for packages you intend to use in your program—through the <code>import</code> statement or <code>require()</code> function.</p>
<h4 id="heading-difference-5-examples">Difference 5: Examples</h4>
<p><a target="_blank" href="https://www.npmjs.com/">NPM</a>, <a target="_blank" href="https://reactnative.dev/docs/environment-setup">React Native CLI</a>, <a target="_blank" href="https://www.gatsbyjs.com/docs/reference/gatsby-cli/">Gatsby CLI</a>, <a target="_blank" href="https://gruntjs.com/getting-started">Grunt CLI</a>, and <a target="_blank" href="https://cli.vuejs.org/">Vue CLI</a> are well-known examples of global packages.</p>
<p>Common examples of local packages are <a target="_blank" href="https://webpack.js.org/">Webpack</a>, <a target="_blank" href="https://lodash.com/">Lodash</a>, <a target="_blank" href="https://jestjs.io/">Jest</a>, and <a target="_blank" href="https://momentjs.com/">MomentJS</a>.</p>
<p><strong>Note:</strong></p>
<ul>
<li>You can <a target="_blank" href="https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/#when-you-can-t-choose">do both local and global installation</a> of packages you intend to use both on the command line and in your project. Typical examples of such packages are <a target="_blank" href="https://expressjs.com/">ExpressJS</a> and <a target="_blank" href="https://coffeescript.org/">CoffeeScript</a>.</li>
<li>Your package manager does not execute an installed package. NPM (and Yarn) only install packages to the <code>node_modules</code> directory. And if you had specified the <code>--save</code> command, your manager would add details about the package to the <code>package.json</code> file.</li>
<li>To execute (run) any <a target="_blank" href="https://helpdeskgeek.com/how-to/what-is-an-executable-file-how-to-create-one/">executable</a> package, you must explicitly do so yourself. We will discuss how in a later section of this article.</li>
</ul>
<p>But what exactly are the <code>node_modules</code> folder, <code>package.json</code> file, <code>package-lock.json</code> file, and <code>yarn.lock</code> file? Let's find out.</p>
<h2 id="heading-what-is-a-nodemodules-folder">What Is a <code>node_modules</code> Folder?</h2>
<p>The <strong>node_modules</strong> directory is the folder where NPM places all the packages it downloads locally for your project.</p>
<h2 id="heading-what-is-a-packagejson-file">What Is a <code>package.json</code> File?</h2>
<p>A <strong>package.json</strong> file is a JSON document that package managers—like NPM and Yarn—use to store information about a specific project.</p>
<p>In other words, a <code>package.json</code> file is a project's metadata file.</p>
<h3 id="heading-advantages-of-a-packagejson-file">Advantages of a <code>package.json</code> File</h3>
<p>A <code>package.json</code> file:</p>
<ul>
<li>makes it possible to publish your project to the NPM registry</li>
<li>makes it easy for others to manage and install your package</li>
<li>helps NPM manage a <a target="_blank" href="https://www.codesweetly.com/javascript-modules-tutorial/">module</a>'s dependencies easily</li>
<li>makes your package reproducible and shareable with other developers</li>
</ul>
<h3 id="heading-how-to-create-a-packagejson-file">How to Create a <code>package.json</code> File</h3>
<p>Go to your project's root directory and initialize the creation of a <code>package.json</code> file by running:</p>
<pre><code class="lang-bash">npm init
</code></pre>
<p>Or, if your package manager is Yarn, run:</p>
<pre><code class="lang-bash">yarn init
</code></pre>
<p>Once you've executed the initialization command above, your package manager will walk you through creating the <code>package.json</code> file by asking a few questions about your project.</p>
<p>If you wish to skip the questionnaire, you can create a default <code>package.json</code> file. Let's see how.</p>
<h3 id="heading-how-to-create-a-default-packagejson-file">How to Create a Default <code>package.json</code> File</h3>
<p>Suppose you prefer to skip the questionnaire prompted by the <code>npm init</code> (or <code>yarn init</code>) command. In such a case, go to your project's <a target="_blank" href="https://www.codesweetly.com/web-tech-glossary/#h-root-directory">root directory</a> and run:</p>
<pre><code class="lang-bash">npm init -y
</code></pre>
<p>Or, if your package manager is Yarn, run:</p>
<pre><code class="lang-bash">yarn init -y
</code></pre>
<p>The command above will use <a target="_blank" href="https://docs.npmjs.com/creating-a-package-json-file#default-values-extracted-from-the-current-directory">default values extracted from the current directory</a> to create your project's <code>package.json</code> file.</p>
<p><strong>Note:</strong> The <code>-y</code> flag is a shorthand for <code>--yes</code>.</p>
<p>Once your package manager finishes its initialization process, your project's <code>package.json</code> file will contain an object with a set of properties.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"codesweetly-project"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>
}
</code></pre>
<p>You can see that the <code>package.json</code> file above contains the <code>name</code>, <code>version</code>, and <code>main</code> fields. Let's learn more about these properties below.</p>
<h3 id="heading-the-packagejsons-fields">The <code>package.json</code>'s Fields</h3>
<p>The <code>package.json</code>'s properties make your project usable to package managers and end-users.</p>
<p>Suppose you wish to publish your package to the NPM registry. In that case, your <code>package.json</code> file must have the <code>"name"</code> and <code>"version"</code> fields.</p>
<p>However, if you do not intend to publish your package, in that case, all fields—including the <code>"name"</code> and <code>"version"</code> properties—are optional.</p>
<p>Let's learn more about the commonly used fields in a <code>package.json</code> file.</p>
<h4 id="heading-name">name</h4>
<p>The <code>"name"</code> field is a property used to record a project's name.</p>
<p>The <code>"name"</code> property's value must be:</p>
<ul>
<li>a single word</li>
<li>lowercase lettering</li>
<li>and less than or equal to 214 characters</li>
</ul>
<p>Note that you can join words together with hyphens and underscores.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"code_sweetly-project"</span>
}
</code></pre>
<h4 id="heading-version">version</h4>
<p>The <code>"version"</code> field indicates a project's current version number.</p>
<p>The <code>"version"</code> property must be in the form of a <code>major.minor.patch</code> format. It must also follow the <a target="_blank" href="https://docs.npmjs.com/about-semantic-versioning">semantic versioning guidelines</a>.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>
}
</code></pre>
<h4 id="heading-description">description</h4>
<p>The <code>"description"</code> field is a property containing a brief description of a project's purpose.</p>
<p>NPM recommends having a <code>"description"</code> property to make your package easier to find on the NPM website.</p>
<p>Your description will be one of the things that's shown when people run the <code>npm search</code> command.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"A brief description about this package (project)"</span>
}
</code></pre>
<h4 id="heading-main">main</h4>
<p>The <code>"main"</code> field indicates a project's <a target="_blank" href="https://www.codesweetly.com/web-tech-glossary/#entry-point">entry point</a>.</p>
<p>In other words, when someone runs the <code>require()</code> function, Node will resolve the invocation to <code>require(&lt;package.json:main&gt;)</code>.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"./src/index.js"</span>
}
</code></pre>
<h4 id="heading-private">private</h4>
<p>The <code>"private"</code> field lets package managers know whether they should publish your project to the NPM registry.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>
}
</code></pre>
<p>If you set your package.json's <code>"private"</code> property to <code>true</code>, package managers will not publish your project.</p>
<p>Therefore, setting the property is an excellent way to prevent accidental publication of your package.</p>
<h4 id="heading-scripts">scripts</h4>
<p>The <code>"scripts"</code> field defines the script commands you want to run at various times in your project's lifecycle.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"jest"</span>,
    <span class="hljs-attr">"dev"</span>: <span class="hljs-string">"webpack --mode development"</span>,
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"webpack --mode production"</span>,
    <span class="hljs-attr">"predeploy"</span>: <span class="hljs-string">"npm run build"</span>,
    <span class="hljs-attr">"deploy"</span>: <span class="hljs-string">"gh-pages -d build"</span> 
  }
}
</code></pre>
<p>The <code>"scripts"</code> field above contains five properties whose values are the commands we want our package manager to run whenever we invoke the property's key.</p>
<p>So, for instance, running <code>npm run dev</code> will execute the <code>"webpack --mode development"</code> command.</p>
<h4 id="heading-keywords">keywords</h4>
<p>The <code>"keywords"</code> field specifies an array of keywords that can help people discover your package.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"keywords"</span>: [
    <span class="hljs-string">"drag"</span>,
    <span class="hljs-string">"drop"</span>,
    <span class="hljs-string">"drag and drop"</span>,
    <span class="hljs-string">"dragndrop"</span>,
    <span class="hljs-string">"draggable"</span> 
  ]
}
</code></pre>
<p>The <code>"keywords"</code> property is part of the information shown when people run the <code>npm search</code> command.</p>
<h4 id="heading-author">author</h4>
<p>The <code>"author"</code> field lists a project's author's details.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Oluwatobi Sofela &lt;oluwatobiss@codesweetly.com&gt; (https://www.codesweetly.com)"</span>
}
</code></pre>
<p>You can also write the snippet above as:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"author"</span>: {
    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Oluwatobi Sofela"</span>,
    <span class="hljs-attr">"email"</span>: <span class="hljs-string">"oluwatobiss@codesweetly.com"</span>,
    <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://www.codesweetly.com"</span>
  }
}
</code></pre>
<p>Note that the <code>"email"</code> and <code>"url"</code> properties are optional.</p>
<h4 id="heading-dependencies">dependencies</h4>
<p>The <code>"dependencies"</code> field lists all the packages a project depends on in production.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"dependencies"</span>: {
    <span class="hljs-attr">"first-package"</span>: <span class="hljs-string">"^1.0.4"</span>,
    <span class="hljs-attr">"second-package"</span>: <span class="hljs-string">"~2.1.3"</span>
  }
}
</code></pre>
<p>So, whenever a user installs your project from the NPM registry, the dependencies property ensures package managers can automatically find and install the packages listed.</p>
<p>Note that you can add a package to the <code>"dependencies"</code> field through either of the following ways:</p>
<ul>
<li>Manually add the name and the <a target="_blank" href="https://docs.npmjs.com/about-semantic-versioning">semantic version</a> of each package your project depends on in production.</li>
<li>Run the <code>npm install package-name --save-prod</code> command on your terminal. Or <code>yarn add package-name</code> if Yarn is your package manager.</li>
</ul>
<h4 id="heading-devdependencies">devDependencies</h4>
<p>The <code>"devDependencies"</code> field lists all the packages a project does not need in production—but requires for its local development and testing purposes.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"devDependencies"</span>: {
    <span class="hljs-attr">"first-dev-package"</span>: <span class="hljs-string">"^5.8.1"</span>,
    <span class="hljs-attr">"second-dev-package"</span>: <span class="hljs-string">"3.2.2—4.0.0"</span>
  }
}
</code></pre>
<p>Note that the packages listed in the <code>"devDependencies"</code> field will be available in the project's development environment but not on its production server.</p>
<p>Suppose a user installs the project through the <code>npm install</code> (or <code>yarn add</code>) command. In such a case, the package manager will find and download all the listed <code>devDependencies</code> to the project's <code>node_modules</code> directory.</p>
<p>Keep in mind that you can add a package to the <code>"devDependencies"</code> field through either of the following ways:</p>
<ul>
<li>Manually add the name and the semantic version of each package on which your project depends for its development and testing purposes.</li>
<li>Run the <code>npm install package-name --save-dev</code> command on your terminal. Or <code>yarn add package-name --dev</code> if Yarn is your package manager.</li>
</ul>
<h4 id="heading-homepage">homepage</h4>
<p>The <code>"homepage"</code> field specifies the URL to your project's homepage.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"homepage"</span>: <span class="hljs-string">"https://codesweetly.com/package-json-file-explained"</span>
}
</code></pre>
<p>So, now that we know what a <code>package.json</code> file is, we can discuss <code>package-lock.json</code>.</p>
<h2 id="heading-what-is-a-package-lockjson-file">What Is a <code>package-lock.json</code> File?</h2>
<p>The <strong>package-lock.json</strong> file is a <a target="_blank" href="https://www.codesweetly.com/document-vs-data-vs-code/#h-what-is-a-document">document</a> NPM uses to record the exact version of all the packages you've installed locally to your project's <code>node_modules</code> directory.</p>
<p>A <code>package-lock.json</code> file makes an app 100% reproducible in the exact way you published it to the NPM registry.</p>
<p>So, suppose a user clones your app and runs the <code>npm install</code> command. In such a case, <code>package-lock.json</code> ensures that the user downloads the exact version of the packages you used to develop the application.</p>
<p>For instance, let's say a user cloned your app containing <em>no</em> <code>package-lock.json</code> file, and a dependency used in the app has a newer version.</p>
<p>Suppose the dependency's version number in the <code>package.json</code> file has a caret sign (for example, <code>^2.6.2</code>). In that case, NPM will install the latest minor version of the dependency—which might cause the app to produce erroneous results.</p>
<p>However, suppose the user cloned your app containing a <code>package-lock.json</code> file. In that case, NPM will install the exact version of the dependency as recorded in the <code>package-lock.json</code> file—regardless of whether a newer version exists.</p>
<p>Therefore, users will always get your app the precise way you published it to the NPM registry.</p>
<p>In other words, NPM uses the <code>package-lock.json</code> file to lock your package's dependencies to the specific version numbers you used for the project's development.</p>
<p><strong>Note:</strong> NPM will update the packages recorded in the <code>package-lock.json</code> file whenever you run the <code>npm update</code> command.</p>
<h2 id="heading-what-is-a-yarnlock-file">What Is a <code>yarn.lock</code> File?</h2>
<p>The <code>yarn.lock</code> file is a document Yarn uses to record the exact version of all the packages you've installed locally to your project's <code>node_modules</code> directory.</p>
<p>The <code>yarn.lock</code> file is comparable to NPM's <a class="post-section-overview" href="#heading-what-is-a-package-lockjson-file">package-lock.json</a> lockfile.</p>
<p>We earlier mentioned that your package manager does not execute an installed package—you must explicitly do so yourself. Let's discuss how.</p>
<h2 id="heading-how-to-run-an-executable-package">How to Run an Executable Package</h2>
<p>There are several ways to run an executable package. Below are the standard techniques.</p>
<h3 id="heading-manually-locate-and-execute-the-package">Manually locate and execute the package</h3>
<p>One way to run an executable package is to type its local path on your command line like so:</p>
<pre><code class="lang-bash">./node_modules/.bin/package-name
</code></pre>
<h3 id="heading-add-the-package-to-the-packagejsons-scripts-field">Add the package to the package.json's <code>scripts</code> field</h3>
<p>An alternate way to execute a package is to first add it to the <code>"scripts"</code> field of your project's package.json file like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"your_package"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"desired-name"</span>: <span class="hljs-string">"name-of-package-to-execute"</span>
  }
}
</code></pre>
<p>Afterward, you can run the package like so:</p>
<pre><code class="lang-bash">npm run desired-name
</code></pre>
<p>Note that the command above is shorthand for <code>npm run-script desired-name</code>.</p>
<p>Alternatively, you can execute the package with Yarn like so:</p>
<pre><code class="lang-bash">yarn run desired-name
</code></pre>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"codesweetly-app"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"webpack"</span>,
  }
}
</code></pre>
<p>The snippet above added <a target="_blank" href="https://www.codesweetly.com/javascript-module-bundler/">webpack</a> to your <code>package.json</code>'s <code>"scripts"</code> field. So, we can now execute <code>webpack</code> on the command line like this:</p>
<pre><code class="lang-bash">npm run build
</code></pre>
<p>Or, if your package manager is Yarn, you can run webpack like this:</p>
<pre><code class="lang-bash">yarn run build
</code></pre>
<h3 id="heading-use-npx">Use NPX</h3>
<p>A faster way to run an executable package is to use NPX like so:</p>
<pre><code class="lang-bash">npx package-name
</code></pre>
<p>With NPX, you no longer need to add your package to the <code>"scripts"</code> field of your project's <code>package.json</code> file.</p>
<p>NPX (Node Package Execute) is a <a target="_blank" href="https://nodejs.dev/learn/the-npx-nodejs-package-runner">Node package runner</a> that automatically finds and executes a specified package.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-bash">npx webpack
</code></pre>
<p>The command above will automatically find and execute <a target="_blank" href="https://www.codesweetly.com/javascript-module-bundler/">webpack</a>. So, we do not need to add the <code>"build": "webpack"</code> property to the <code>"scripts"</code> field of our <code>package.json</code> file.</p>
<p><strong>Note:</strong> NPX automatically gets installed when you install Node 8.2/NPM 5.2.0 or higher.</p>
<p>You can also run some code using your preferred Node.js version. Let's find out how.</p>
<h2 id="heading-how-to-run-code-using-your-preferred-nodejs-version">How to Run Code Using Your Preferred Node.js Version</h2>
<p>You can use the <code>@</code> character and the <a target="_blank" href="https://www.npmjs.com/package/node">node npm package</a> to specify the Node.js version you wish to use to execute your code.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-bash">npx node@7 index.js
</code></pre>
<p>The snippet above tells NPX to run <code>index.js</code> with the latest version of Node from version 7 major.</p>
<p>Using the <code>node@</code> command is a helpful way to avoid using Node.js version management tools like <a target="_blank" href="https://github.com/nvm-sh/nvm">nvm</a> to switch between Node versions.</p>
<p>Suppose you wish to confirm the Node version NPX will use to run your code. In that case, run:</p>
<pre><code class="lang-bash">npx node@7 -v
</code></pre>
<p>The snippet above will display the latest Node version from version 7 major that NPX will use to run your code—for example, <code>v7.10.1</code>.</p>
<h2 id="heading-how-to-check-for-outdated-local-packages">How to Check for Outdated Local Packages</h2>
<p>To determine if any of your project's packages are outdated, run:</p>
<pre><code class="lang-bash">npm outdated
</code></pre>
<p>If the command outputs nothing, it means all your project's packages are up to date.</p>
<p>Otherwise, see this <a target="_blank" href="https://docs.npmjs.com/cli/v6/commands/npm-outdated">npm-outdated article</a> for a detailed explanation of the command's output.</p>
<p>Alternatively, you can use Yarn like so:</p>
<pre><code class="lang-bash">yarn outdated
</code></pre>
<p><strong>Note:</strong> To check a specific package's outdated status, add the package's name after the <code>outdated</code> keyword—for example, <code>npm outdated lodash</code>.</p>
<h2 id="heading-how-to-check-for-outdated-global-packages">How to Check for Outdated Global Packages</h2>
<p>To confirm which global package is outdated, run:</p>
<pre><code class="lang-bash">npm outdated -g --depth=0
</code></pre>
<h2 id="heading-how-to-check-for-locally-installed-packages">How to Check for Locally Installed Packages</h2>
<p>Here are three ways to check for locally installed packages:</p>
<h3 id="heading-locally-installed-packages-and-their-dependencies">Locally installed packages and their dependencies</h3>
<pre><code class="lang-bash">npm list
</code></pre>
<p>Or use Yarn like so:</p>
<pre><code class="lang-bash">yarn list
</code></pre>
<h3 id="heading-locally-installed-packageswithout-their-dependencies">Locally installed packages—without their dependencies</h3>
<pre><code class="lang-bash">npm list --depth=0
</code></pre>
<p>Or,</p>
<pre><code class="lang-bash">yarn list --depth=0
</code></pre>
<h3 id="heading-check-if-a-specific-package-got-installed-locally">Check if a specific package got installed locally</h3>
<pre><code class="lang-bash">npm list package-name
</code></pre>
<h2 id="heading-how-to-check-for-globally-installed-packages">How to Check for Globally Installed Packages</h2>
<p>Here are three ways to check for globally installed packages:</p>
<h3 id="heading-globally-installed-packages-and-their-dependencies">Globally installed packages and their dependencies</h3>
<pre><code class="lang-bash">npm list -g
</code></pre>
<p>Or use Yarn like so:</p>
<pre><code class="lang-bash">yarn list -g
</code></pre>
<h3 id="heading-globally-installed-packageswithout-their-dependencies">Globally installed packages—without their dependencies</h3>
<pre><code class="lang-bash">npm list -g --depth=0
</code></pre>
<p>Or,</p>
<pre><code class="lang-bash">yarn list -g --depth=0
</code></pre>
<h3 id="heading-check-if-a-specific-package-got-installed-globally">Check if a specific package got installed globally</h3>
<pre><code class="lang-bash">npm list -g package-name
</code></pre>
<h2 id="heading-how-to-update-packages">How to Update Packages</h2>
<p>Here's how to update packages with NPM and Yarn:</p>
<h3 id="heading-how-to-update-a-specific-package-to-its-latest-version">How to update a specific package to its latest version</h3>
<pre><code class="lang-bash">npm update package-name
</code></pre>
<p>Or, for projects managed with Yarn, run:</p>
<pre><code class="lang-bash">yarn upgrade package-name
</code></pre>
<h3 id="heading-how-to-update-all-of-a-projects-locally-installed-packages">How to update all of a project's locally installed packages</h3>
<pre><code class="lang-bash">npm update
</code></pre>
<p>Or,</p>
<pre><code class="lang-bash">yarn upgrade
</code></pre>
<h3 id="heading-how-to-update-a-specific-globally-installed-package">How to update a specific globally installed package</h3>
<p>You can update a globally installed package like this:</p>
<pre><code class="lang-bash">npm update package-name -g
</code></pre>
<h3 id="heading-how-to-update-all-your-systems-globally-installed-packages">How to update all your system's globally installed packages</h3>
<pre><code class="lang-bash">npm update -g
</code></pre>
<h2 id="heading-how-to-uninstall-packages">How to Uninstall Packages</h2>
<p>Here's how to uninstall packages with NPM and Yarn:</p>
<h3 id="heading-how-to-uninstall-a-package-from-a-specific-project">How to uninstall a package from a specific project</h3>
<p>First, navigate to the project's <a target="_blank" href="https://www.codesweetly.com/web-tech-glossary/#h-root-directory">root directory</a> from the command line and run:</p>
<pre><code class="lang-bash">npm uninstall package-name
</code></pre>
<p><strong>Note:</strong></p>
<ul>
<li>Add the <code>-S</code> (or <code>--save</code>) flag to remove references to the package in the <code>dependencies</code> field of the project's <code>package.json</code> file.</li>
<li>Add the <code>-D</code> (or <code>--save-dev</code>) flag to remove references to the package in the <code>devDependencies</code> field of the project's <code>package.json</code> file.</li>
</ul>
<p>For projects managed with Yarn, run:</p>
<pre><code class="lang-bash">yarn remove package-name
</code></pre>
<p><strong>Note:</strong> The <code>yarn remove</code> command will automatically update the project's <code>package.json</code> and <code>yarn.lock</code> files.</p>
<h3 id="heading-how-to-uninstall-a-global-package">How to uninstall a global package</h3>
<pre><code class="lang-bash">npm uninstall package-name -g
</code></pre>
<p>Note that it is best practice not to remove packages manually from the <code>node_modules</code> folder as such action can affect other <em>modules</em> depending on it.</p>
<p>But what exactly is a module in NodeJS? Let's find out below.</p>
<h2 id="heading-what-exactly-is-a-module-in-nodejs">What Exactly Is a Module in NodeJS?</h2>
<p>A <strong>module</strong> in NodeJS is any file in the <code>node_modules</code> folder that the computer can load through Node's <code>require()</code> function.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> myModule = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./codesweetly.js"</span>);
</code></pre>
<p>Suppose the computer successfully used the <code>require()</code> function to load the <code>codesweetly.js</code> file. In such a case, it means <code>codesweetly.js</code> is a module—assigned to the <code>myModule</code> variable.</p>
<p>Keep in mind that a module may also be a package—but not always.</p>
<p>A module is <em>not</em> a package if it does <em>not</em> have a <code>package.json</code> file used to record information about it.</p>
<p>Also, note that for a module to be loadable by the <code>require()</code> function, the module must be one of the following:</p>
<ul>
<li>A package—whose <code>package.json</code> file contains a <code>"main"</code> field.</li>
<li>A JavaScript file.</li>
</ul>
<h2 id="heading-how-to-publish-your-project-to-the-npm-registry">How to Publish Your Project to the NPM Registry</h2>
<p>NPM is a free registry for <a target="_blank" href="https://www.npmjs.com/products">public package authors</a>.</p>
<p>So, you can use it to publish any project (folder) from your computer that has a <code>package.json</code> file.</p>
<p>Below are the steps required to share your package with the world.</p>
<h3 id="heading-step-1-sign-in-or-sign-up">Step 1: Sign in or sign up</h3>
<p>Go to the <a target="_blank" href="https://www.npmjs.com/">NPM website</a> and sign in (or sign up if you do not yet have an account).</p>
<p><strong>Note:</strong> make sure that you verify your email after creating a new account. Otherwise, you will get a <code>403 Forbidden</code> error while publishing your package.</p>
<h3 id="heading-step-2-log-in">Step 2: Log in</h3>
<p>Login to your NPM account from the command line like so:</p>
<pre><code class="lang-bash">npm login
</code></pre>
<p><strong>Note:</strong> You can use the <code>npm whoami</code> command to check if you are currently logged in.</p>
<h3 id="heading-step-3-publish-your-package">Step 3: Publish your package!</h3>
<p>Go to your project's root directory and publish it like so:</p>
<pre><code class="lang-bash">npm publish
</code></pre>
<p>Make sure that your package's name does not currently exist on NPM. Otherwise, you will get an error while publishing.</p>
<p>You can use the <code>npm search</code> command (or the <a target="_blank" href="https://www.npmjs.com/">NPM website</a>'s search bar) to search if the name you wish to use already exists on NPM.</p>
<p>Suppose all the suitable names for your package are already taken. In that case, NPM allows you to publish your project as a scope.</p>
<p>In other words, you can publish your package as a sub-section of your username. Let's see how below.</p>
<h3 id="heading-how-to-publish-your-package-as-a-scope-of-your-username">How to publish your package as a scope of your username</h3>
<p>Open your <code>package.json</code> file and prefix your package's name with your username.</p>
<p><strong>Here's an example:</strong></p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"@username/package-name"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>
}
</code></pre>
<p>NPM's default setting assumes that a scoped name package is a private project. So, you will get an error if you use the <code>npm publish</code> command to share a scoped name package.</p>
<p>Therefore, to publish your package as a scope of your username, add the <code>--access=public</code> flag to the <code>npm publish</code> command:</p>
<pre><code class="lang-bash">npm publish --access=public
</code></pre>
<p><strong>Note:</strong> You can make your project a scoped package during the initialization process by using the <code>npm init --scope=username</code> command instead of <code>npm init</code>.</p>
<h2 id="heading-overview">Overview</h2>
<p>This article discussed what a package manager is. We also looked at how two popular package managers (NPM and Yarn) work.</p>
<p>Thanks for reading!</p>
<h3 id="heading-and-heres-a-useful-reactjs-resource"><strong>And here's a useful ReactJS resource:</strong></h3>
<p>I wrote a book about React!</p>
<ul>
<li>It's beginners friendly ✔</li>
<li>It has live code snippets ✔</li>
<li>It contains scalable projects ✔</li>
<li>It has plenty of easy-to-grasp examples ✔</li>
</ul>
<p>The <a target="_blank" href="https://amzn.to/30iVPIG">React Explained Clearly</a> book is all you need to understand ReactJS.</p>
<p><a target="_blank" href="https://amzn.to/30iVPIG"><img src="https://www.freecodecamp.org/news/content/images/2022/01/Twitter-React_Explained_Clearly-CodeSweetly-Oluwatobi_Sofela.jpg" alt="React Explained Clearly Book Now Available at Amazon" width="600" height="400" loading="lazy"></a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Install Node.js and npm on Windows ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you'll learn how to work with JavaScript in the backend using Node on Windows. When you start working with JavaScript and discover that you can not only work with it in the frontend but also in the backend, a new world of possibiliti... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-install-node-js-and-npm-on-windows-2/</link>
                <guid isPermaLink="false">66b0a7b5b27f151789723018</guid>
                
                    <category>
                        <![CDATA[ node ]]>
                    </category>
                
                    <category>
                        <![CDATA[ npm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Daniel Rosa ]]>
                </dc:creator>
                <pubDate>Wed, 02 Mar 2022 00:18:51 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/03/pexels-digital-buggu-171198--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you'll learn how to work with JavaScript in the backend using Node on Windows.</p>
<p>When you start working with JavaScript and discover that you can not only work with it in the frontend but also in the backend, a new world of possibilities seems to open up before you.</p>
<p>To begin with, you realize that you don't need to learn another language to have the backend of your applications up and running. Second, Node.js is simple to install and works in all development platforms we are used to: Mac, Linux, and Windows.</p>
<p>In this article, I'll show you how to install Node on Windows with a step-by-step guide so you're ready to use it.</p>
<p>You will also be happy to know that package management is made even easier, as npm (the Node Package Manager) comes with the installation of Node.</p>
<p>With it, you will be able to have access to an almost unending number of community-made dependencies. You can simply install these in your app so you don't have to reinvent the wheel time and again.</p>
<p>So let’s install Node on Windows and start playing with it a bit.</p>
<h2 id="heading-how-to-install-node-on-windows">How to Install Node on Windows</h2>
<p>The first thing to do is to access <a target="_blank" href="https://nodejs.org/">Node’s official site</a>.</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_site.PNG" alt="Image" width="1920" height="968" loading="lazy">
<em>Node site front page</em></p>
<p>The website is intelligent enough to detect the system you are using, so if you are on Windows, you will most likely get a page like the one above. Right in the middle of it, two buttons show you the most common possibilities of download – also the latest ones. </p>
<p>If you are curious about all the most recent features Node has to offer, go with the button on the right. For most people, however, the site itself recommends using the Long-Term Support version, which leads you to the button on the left. </p>
<p>At the moment of writing this article, the LTS version is version 16.14.0.</p>
<p>When you click on any of them, an .msi file gets downloaded to your computer. The next step is to click on it and the installation will begin. The wizard opens and the following window appears:</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_install1.PNG" alt="Image" width="491" height="384" loading="lazy">
<em>Node installation wizard's initial page</em></p>
<p>Click Next. On the following window, you'll read (you do read it, right?) Node’s EULA, accept its terms, and click Next again. The next window is the one where you select the destination folder for Node.</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_install2.PNG" alt="Image" width="493" height="383" loading="lazy"></p>
<p>Windows normally recommends that the programs be installed in the Program Files folder, in a folder of their own (in our case, we are installing Node.js, so the nodejs folder is our go-to place). </p>
<p>For the sake of simplicity, let’s follow the wizard’s suggestions and use <code>C:\Program Files\nodejs\</code> as the destination folder.</p>
<p>The following window is the one where you can customize your installation. Unless you have disk space problems or have a clear idea as to what you are doing, I recommend keeping the options as they are and just pressing Next again.</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_install3.PNG" alt="Image" width="492" height="383" loading="lazy"></p>
<p>One thing I would like to point out on this window is the third option you see. That’s the option that allows you to have npm installed along with Node on your computer. This way, if you still intend to change the setup in this page somehow, keep that option as is and npm will be installed for you at the end of the process.</p>
<p>The next window deals with the automatic installation of “Tools for Native Modules”. Again, unless you are sure you need them, I recommend keeping this checkbox unmarked and just pressing Next once more.</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_install4.PNG" alt="Image" width="488" height="383" loading="lazy"></p>
<p>We’ve reached the final pre-install window. As it says, from here, you just have to click Install to begin the installation, so let’s do it. </p>
<p>Notice the shield beside the word Install? That means Windows will ask you to confirm if you really want to go through the installation process as soon as you click that button. Assuming this is the reason why you are reading this article, just click Yes and let the installer do its thing.</p>
<p><img src="https://www.freecodecamp.org/portuguese/news/content/images/2022/03/node_install5.PNG" alt="Image" width="492" height="383" loading="lazy"></p>
<p>We finally got to the window we were hoping for, telling us that Node has successfully been installed on our Windows computer. Click Finish and let’s check if everything is ok.</p>
<h2 id="heading-how-to-check-your-node-installation">How to Check Your Node Installation</h2>
<p>In order to check if Node (and npm) were properly installed on your computer, you can choose to open either Windows Powershell or the Command Prompt. </p>
<p>We’ll go with the first. Click on the search bar beside the Start Menu button and type <code>powershell</code>. Click Enter and Windows Powershell will open up in a window for you.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/03/node_install10.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In any folder (like <code>C:\Users</code>, for instance), you can type <code>node -v</code> to check for the version of Node you are using. As I mentioned above, the latest version as I write this article is version 16.14.0 and that’s exactly what we see on Powershell above.</p>
<p>As a side note, you may be asking yourself why we can check this in any folder. One of the options in the custom setup (that we left as is) was to add Node to PATH. By doing so, we are able to access it from anywhere while navigating through the folders.</p>
<p>It is also possible to check for the npm version. To do so, type <code>npm -v</code> and press Enter. In our case, latest version is version 8.3.1, so we can pretty much say we are up to date.</p>
<h2 id="heading-how-to-use-npm">How to Use npm</h2>
<p>Ok, but you did not go all this way reading just to finish here after installing Node and npm, right? You want to see both in action. Let’s do it, then.</p>
<p>To learn how to start a project with Node and install packages with npm, we’ll use Visual Studio Code.</p>
<p>We'll create a folder named Node_Test, where we’ll put both Node and npm to work a little. </p>
<p>Let’s start simple. Inside the Node_Test folder, right click inside the folder and click Open with Visual Studio Code. This will make VS Code open in this empty folder automatically. </p>
<p>Inside VS Code, if you haven’t yet, open a new terminal by pressing <code>Ctrl+Shift+'</code> (single quote).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/03/node_install11.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Click on the terminal and, on the command line, type <code>npm init -y</code>. This will start a Node project automatically for us without us needing to worry about the initial configuration (the <code>-y</code> flag will do that on its own). This creates a package.json file within the Node_Test folder. </p>
<p>Next, let’s install Express as a dependency. You can find it and a list of other possible dependencies of npm on <a target="_blank" href="https://www.npmjs.com/">https://www.npmjs.com/</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/03/node_install12.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Another side note: every time you open npm’s web site, on the top left, you will see what appears to be a meaningless combination of three words. If you look at the initials, though, you will see that it is a brand-new sequence with the acronym npm. </p>
<p>Right, now let’s install Express with this Nifty Purring Manticore. Back on VS Code and the terminal, type <code>npm i express</code> and press Enter. Express will be installed. You can do the same with any other dependency you can think about.</p>
<p>To make sure that Express is installed, open package.json. Scroll up to the list of dependencies and you will see Express there.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/03/node_install13.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>That’s pretty much it. In this article, you saw how to install Node and npm on Windows. </p>
<p>I hope this has been useful to you. For more tutorials like this, check out <a target="_blank" href="https://freecodecamp.org/news">freecodecamp.org/news</a> and browse for the topic you would like to learn about.</p>
<p>Happy coding! 😊</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
