<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Roberto Hernández</title>
    <description>The latest articles on Forem by Roberto Hernández (@robertoheci).</description>
    <link>https://forem.com/robertoheci</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F823527%2F2679f020-0a7b-45fd-9ce0-48ac8f3ee5a2.jpeg</url>
      <title>Forem: Roberto Hernández</title>
      <link>https://forem.com/robertoheci</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/robertoheci"/>
    <language>en</language>
    <item>
      <title>Different approaches to testing your own packages locally: Verdaccio</title>
      <dc:creator>Roberto Hernández</dc:creator>
      <pubDate>Mon, 20 Feb 2023 18:34:09 +0000</pubDate>
      <link>https://forem.com/one-beyond/different-approaches-to-testing-your-own-packages-locally-verdaccio-5hd8</link>
      <guid>https://forem.com/one-beyond/different-approaches-to-testing-your-own-packages-locally-verdaccio-5hd8</guid>
      <description>&lt;p&gt;This is part of a series of articles:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-1kdg"&gt;Linking local files&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-npm-link-4hoj"&gt;npm link&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-npm-yalc-3l97"&gt;npm yalc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Verdaccio&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-relative-deps-2c17"&gt;Relative deps&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;p&gt;In this series, we are going to see how we can use Verdaccio to test your packages locally. &lt;/p&gt;

&lt;h1&gt;
  
  
  1. What is Verdaccio?
&lt;/h1&gt;

&lt;p&gt;Verdaccio is, apart from a green color popular in late medieval Italy for fresco painting (method of painting water-based pigments on freshly applied plaster, usually on wall surfaces), a lightweight proxy and private packages registry. &lt;/p&gt;

&lt;p&gt;It can be configured as required and comes out of the box with its own tiny database and introduces caching the downloaded modules along the way, so you don't need your own one to get started.&lt;/p&gt;

&lt;p&gt;In addition, it allows you to host private Node Packages and is compatible with all client package managers (such npm, yarn and pnpm). &lt;/p&gt;

&lt;p&gt;For those who are looking for extending their storage capabilities, Verdaccio supports various community-made plugins to hook into services such as Amazon's S3, Google Cloud Storage or create your own plugin and also provides Docker and Kubernetes support. &lt;/p&gt;

&lt;h1&gt;
  
  
  2. How to use it locally?
&lt;/h1&gt;

&lt;p&gt;Let’s say that you have a React application and a components library and you want to test that fancy component library in the React application. On the previous post related to this series, we have seen how to develop your components library using &lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-1kdg"&gt;Linking local files&lt;/a&gt;, &lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-npm-link-4hoj"&gt;Npm link&lt;/a&gt; and &lt;a href="https://dev.to/one-beyond/different-approaches-to-testing-your-own-packages-locally-npm-yalc-3l97"&gt;Npm Yalc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we will learn how we can do it using Verdaccio. &lt;/p&gt;

&lt;p&gt;The usual method would be uploading your library to npm or a similar package manager, then download and install the dependency. But you do not want to add the library yet because you want to test it deeper.  &lt;/p&gt;

&lt;p&gt;Verdaccio offers you a solution for that. You can install it on your local machine and use it as the provider of your library. &lt;/p&gt;

&lt;p&gt;Let’s see how we can start using Verdaccio locally. The most straightforward method to start using it is: &lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;To install it globally in your local machine, you just need to run the following command: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; verdaccio


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;By default, Verdaccio provides you with everything to start working with the package registry. In case you want to see the default config, here you have it:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;storage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./storage&lt;/span&gt; 
&lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
 &lt;span class="na"&gt;htpasswd&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
 &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./htpasswd&lt;/span&gt; 
&lt;span class="na"&gt;uplinks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
 &lt;span class="na"&gt;npmjs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
 &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://registry.npmjs.org/&lt;/span&gt; 
&lt;span class="na"&gt;packages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;@*/*"&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; 
   &lt;span class="na"&gt;access&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$all&lt;/span&gt; 
   &lt;span class="na"&gt;publish&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$authenticated&lt;/span&gt; 
   &lt;span class="na"&gt;proxy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npmjs&lt;/span&gt; 
 &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;**"&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; 
   &lt;span class="na"&gt;proxy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npmjs&lt;/span&gt; 
&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;stdout&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;format&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;pretty&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;level&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;http&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt; 


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

&lt;/div&gt;

&lt;p&gt;For further configuration, I recommend you to check out the official documentation &lt;a href="https://verdaccio.org/docs/configuration/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;In order to run the Verdaccio service, you just need to run the command &lt;code&gt;verdaccio&lt;/code&gt; in the terminal. After a few seconds, you will have your fully working Verdaccio over &lt;code&gt;http://localhost:4873/&lt;/code&gt; &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcpaleryqqs4hk4kbnyv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcpaleryqqs4hk4kbnyv.png" alt="verdaccio-homepage"&gt;&lt;/a&gt;&lt;br&gt;
Once we have the Verdaccio package registry running, we just need to follow a few steps to have our library installed on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  First step: create the &lt;code&gt;.npmrc&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;We have to do that in both the library and the React application that will consume the components library in order to connect to the right package registry when we run the install command.&lt;/p&gt;

&lt;p&gt;This should be the content of that file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@ks:registry=http://localhost:4873


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Second step: Log in on Verdaccio package registry
&lt;/h3&gt;

&lt;p&gt;Once we have the Verdaccio running, we need to create a user to be able to publish the package. We will do that by using this following command:  &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm adduser &lt;span class="nt"&gt;--registry&lt;/span&gt; http://localhost:4873 


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

&lt;/div&gt;

&lt;p&gt;It will ask you the username, password and email. After that, the system will log you automatically as the user that you have created. &lt;/p&gt;

&lt;p&gt;In case that you want to create more than one user and change the session, you can use this command: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm login &lt;span class="nt"&gt;--registry&lt;/span&gt; http://localhost:4873 


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Third step: publish your package
&lt;/h3&gt;

&lt;p&gt;For publishing the library on the right package registry, you have to make sure that you have created the &lt;code&gt;.npmrc&lt;/code&gt; pointing to the Verdaccio local url.&lt;/p&gt;

&lt;p&gt;I will provide you with a few scripts that you have to add in the &lt;code&gt;package.json&lt;/code&gt; of your component library in order to publish the library and upgrade the version every time you publish it. If you don’t upgrade the version every time you publish, you will return an error after running the commands. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="nl"&gt;"unpublish-library"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm unpublish @ks/my-fancy-library --force --registry http://localhost:4873"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="nl"&gt;"update-version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm version patch --force"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt; 
&lt;/span&gt;&lt;span class="nl"&gt;"publish-library"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run build &amp;amp;&amp;amp; npm run update-version &amp;amp;&amp;amp; npm run unpublish-library &amp;amp;&amp;amp; npm publish"&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="w"&gt;


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

&lt;/div&gt;

&lt;p&gt;After running the command &lt;code&gt;publish-library&lt;/code&gt;, you should be able to see see something like that on the &lt;a href="http://localhost:4873/" rel="noopener noreferrer"&gt;Verdaccio dashboard&lt;/a&gt;:&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Last step: install the library in your app, update the library and see you change.
&lt;/h3&gt;

&lt;p&gt;This is the time of truth. Once we have the library published, we will install it on our React application and use a single component to see how it works. &lt;/p&gt;

&lt;p&gt;With the Verdaccio package registry running, we will install the library via your package manager (let’s use for instance npm) and run this command: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; @ks/my-fancy-library@latest 


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

&lt;/div&gt;

&lt;p&gt;Once you have the library installed, make sure that It s properly configured and is displaying the component that you want &lt;br&gt;
After that, we are going to go to the library and we will change something on the component that you are displaying, and then you just need to do two things: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build and publish your library to Verdaccio: &lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm run publish-library


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

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Go to the React app and install the latest version of the component library:
```bash
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;npm install &lt;a class="mentioned-user" href="https://dev.to/ks"&gt;@ks&lt;/a&gt;/my-fancy-library@latest &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
When the process finishes, you will be able to see the change you have done on the component library into your React application 


#Extra mile: How to run Verdaccio using docker? 


There is a way to install the Verdaccio package registry using docker.It is also quite straightforward:

You need to have a docker daemon installed and running in order to run the command and raise the container. 

After that you can take advantage of this command to do it: 

```bash


docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio 


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

&lt;/div&gt;

&lt;p&gt;If everything goes well, you should have a running Verdaccio &lt;a href="http://0.0.0.0:4873/" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Having that working, you need to follow the same instructions I mentioned above on the section which explains how to install the library locally,specifically in the configuration step because the process is almost the same. The only thing you just need to replace is the url in the .npmrc from this &lt;code&gt;http://localhost:4873/&lt;/code&gt; to this &lt;code&gt;http://0.0.0.0:4873/&lt;/code&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Possible usages
&lt;/h1&gt;

&lt;p&gt;I will leave you here a few ideas of what you can use Verdaccio for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create your own private package registry &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce the CI process time caching the packages &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Testing monorepos and the deployment in the registry &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As a backup for packages during the CI process. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Personal usage, when you have different packages related from each other's. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Teacher purposes (Sometimes npm mark packages as spam) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It allows you to use AWS or Google cloud to store the packages cache&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uplinks: You can configure it to define the proxy that you want to user in each package. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Interesting links
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/verdaccio/github-actions/tree/master/publish" rel="noopener noreferrer"&gt;Github action &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://verdaccio.org/" rel="noopener noreferrer"&gt;Official documentation&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=nFAGvWdHPP4" rel="noopener noreferrer"&gt;Talk about verdaccio (ES)&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=hDIFKzmoCaA" rel="noopener noreferrer"&gt;Talk about verdaccio (EN)&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>verdaccio</category>
      <category>node</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
