<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>npm Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/npm/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/npm/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Thu, 24 Aug 2023 15:49:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>npm Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/npm/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>How (And Why?) To Wrap External Libraries?</title>
		<link>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/</link>
					<comments>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 20 Feb 2023 09:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[road-to-dotnet-full-stack]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4346</guid>

					<description><![CDATA[<p>If you use external libraries in your application, wrapping them may be very helpful. How to wrap external libraries and why it&#8217;s worth doing that? Today we&#8217;re going to dive into that, based on a TypeScript web app example 😉 Why? You probably know what a&#160;wrapper is. As its name suggests, it&#8217;s a practice of&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/">How (And Why?) To Wrap External Libraries?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you use external libraries in your application, wrapping them may be very helpful. How to wrap external libraries and why it&#8217;s worth doing that? Today we&#8217;re going to dive into that, based on a TypeScript web app example <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4346"></span>



<h2 class="wp-block-heading">Why?</h2>



<p>You probably know what a&nbsp;<em>wrapper</em> is. As its name suggests, it&#8217;s a practice of putting another layer on a piece of something. In our case, <em>wrapping</em>&nbsp;a piece of code in another piece of code <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>But why would you do that? To make your life easier! <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4aa.png" alt="💪" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br>Wrapping external libraries lets you abstract your code from their implementation details. In effect, it makes your life a lot easier when you want to keep the behavior, but change the library providing it. This approach also lets you use only those features from a given external dependency that you actually need.<br>Let&#8217;s see that with an example.</p>



<h2 class="wp-block-heading">Wrapping HTTP client</h2>



<p>A very good example is HTTP client&nbsp;wrapper. HTTP calls are used in almost every web application. In order to perform them, we need to choose an HTTP client. We can either use <code><a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch">fetch</a></code>, or something more sophisticated like&nbsp;<code><a href="https://axios-http.com/docs/intro">axios</a></code>.</p>



<p>However, with time, we may decide to replace it with something else. There might be many reasons for that &#8211; either the library stops to be maintained or something new and better is out there. It would be a shame if we&#8217;d now need to change the code in those thousands of places where the current library is being used. This would take a lot of time and might be error-prone. We can definitely prepare better for such cases <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Create HttpClient wrapper for axios</h3>



<p>Let&#8217;s say that, for now, we will go with <code>axios</code>. Instead of <a href="https://axios-http.com/docs/example">calling it directly from our code</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,10-11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { useEffect, useState } from &quot;react&quot;;<br/>import axios from &quot;axios&quot;;<br/>import { Product } from &quot;../types/product&quot;;<br/>import { DummyJsonProductsResult } from &quot;../types/dummyJsonProductsResult&quot;;<br/><br/>export const ProductsList = () =&gt; {<br/>  const [products, setProducts] = useState&lt;Product[] | null&gt;(null);<br/><br/>  useEffect(() =&gt; {<br/>    axios<br/>      .get&lt;DummyJsonProductsResult&gt;(&quot;https://dummyjson.com/products&quot;)<br/>      .then((result) =&gt; {<br/>        setProducts(result.data.products);<br/>      });<br/>  }, []);<br/> <br/> // ...<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ProductsList.tsx &#8211; calling axios directly from component&#039;s code</span> </div> </div></div>



<p>I will create an <em>HttpClient</em> wrapper for it and use it instead.</p>



<p>First, I create <code>httpClient.ts</code> file in <code>wrappers</code> folder. I like to have such a catalog in my React projects and keep all the wrappers there.</p>



<p>I start writing all wrappers with an interface. In that case, I treat the interface as a <em>contract</em>. It should say what I need this small wrapper to do, without worrying about implementation details.</p>



<p><code>IHttpClient</code> interface initially looks as follows:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">interface IHttpClient {<br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt;;<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; IHttpClient interface (get only)</span> </div> </div></div>



<p>That&#8217;s what we have so far. We just need to retrieve the data with <code>GET</code> method.</p>



<p>Next step is to create the actual implementation of <code>IHttpClient</code> using <code>axios</code>. This is pretty straightforward using a <code>class</code> implementing <code>IHttpClient</code> interface and taking a look at <a href="https://axios-http.com/docs/instance"><code>axios</code>&#8216;s documentation</a>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">class AxiosHttpClient implements IHttpClient {<br/>  private instance: AxiosInstance | null = null;<br/><br/>  private get axiosClient(): AxiosInstance {<br/>    return this.instance ?? this.initAxiosClient();<br/>  }<br/><br/>  private initAxiosClient() {<br/>    return axios.create();<br/>  }<br/><br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      this.axiosClient<br/>        .get&lt;TResponse, AxiosResponse&lt;TResponse&gt;&gt;(url)<br/>        .then((result) =&gt; {<br/>          resolve(result.data);<br/>        })<br/>        .catch((error: Error | AxiosError) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; AxiosHttpClient implementation (get only)</span> </div> </div></div>



<p>This implementation lets us encapsulate a simple singleton inside the class.</p>



<p>The last step here is to expose the instance of our HTTP client. Remember to <strong>always export the interface type variable</strong>:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const httpClient: IHttpClient = new AxiosHttpClient();</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; exporting AxiosHttpClient as IHttpClient singleton instance</span> </div> </div></div>



<p>That&#8217;s basically how we wrap external libraries in TypeScript. Easy-peasy <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Using the wrapper</h3>



<p>I can now use our wrapper in <code>ProductsList.tsx</code> component:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="2,10-11" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">import { useEffect, useState } from &quot;react&quot;;<br/>import { httpClient } from &quot;../wrappers/httpClient&quot;; // we don&#039;t import axios here anymore<br/>import { Product } from &quot;../types/product&quot;;<br/>import { DummyJsonProductsResult } from &quot;../types/dummyJsonProductsResult&quot;;<br/><br/>export const ProductsList = () =&gt; {<br/>  const [products, setProducts] = useState&lt;Product[] | null&gt;(null);<br/><br/>  useEffect(() =&gt; {<br/>    httpClient // instead of axios, we use our wrapper in components<br/>      .get&lt;DummyJsonProductsResult&gt;(&quot;https://dummyjson.com/products&quot;)<br/>      .then((result) =&gt; {<br/>        setProducts(result.data.products);<br/>      });<br/>  }, []);<br/> <br/> // ...<br/>};</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">ProductsList.tsx &#8211; using IHttpClient wrapper</span> </div> </div></div>



<p>Notice how easy that was. Since now, we only import stuff from <code>axios</code> package in <code>httpClient.ts</code> file. Only this single file is dependent on <a href="https://www.npmjs.com/package/axios"><code>axios</code> npm package</a>. None of our components (and other project files) know about <code>axios</code>. Our IDE only knows that the wrapper is an object instance fulfilling <code>IHttpClient</code> contract:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="537" height="332" data-attachment-id="4361" data-permalink="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/1_usingwrapperinterface/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" data-orig-size="537,332" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="1_UsingWrapperInterface" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?fit=537%2C332&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2023/02/1_UsingWrapperInterface.png?resize=537%2C332&#038;ssl=1" alt="Interface-implementing wrapper instance usage in Visual Studio Code" class="wp-image-4361"/></figure>
</div>


<h3 class="wp-block-heading">Extra wrapper features</h3>



<p>Apart from nicely isolating us from dependencies, wrappers have more advantages. One of them is a possibility to configure the library in a single place. In our example with <code>axios</code> &#8211; imagine that one day you want to add custom headers to each HTTP request. Having all API calls going via <code>AxiosHttpClient</code>, you can configure such things there, in a single place. That way, you follow the <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY principle</a> and keep all the logic related to <code>axios</code> (or to any other external dependency) in a single place. It also comes with benefits like easy testability etc.</p>



<p>For clarity, I also added <code>post</code> support to our <code>IHttpClient</code>. You can <a href="https://github.com/dsibinski/codejourney/commit/200f31ba0c5483f3e834a5ec4cd82566887e3d98">check it here</a>.</p>



<h2 class="wp-block-heading">Replacing the wrapped library</h2>



<p>Ok, it&#8217;s time to have our solution battle-tested. We have the HTTP client nicely wrapped and exposed as an instance of <code>IHttpClient</code>. However, we came to the conclusion that <code>axios</code> is not good enough, and we want to have it replaced with <code>fetch</code>.</p>



<p>Remember that in the real web application, you would have hundreds or thousands of usages of <code>IHttpClient</code> instance. That&#8217;s where the power of wrappers comes into play <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f60e.png" alt="😎" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>So how do I make sure those thousands of usages will now use <code>fetch</code> instead of <code>axios</code>? That&#8217;s actually pretty straightforward. I&#8217;ll simply add a new class &#8211; <code>FetchHttpClient</code> implementing <code>IHttpClient</code> interface:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-line="4,23" data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">class FetchHttpClient implements IHttpClient {<br/>  get&lt;TResponse&gt;(url: string): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      fetch(url)<br/>        .then((response) =&gt;<br/>          response<br/>            .json()<br/>            .then((responseJson) =&gt; {<br/>              resolve(responseJson as TResponse);<br/>            })<br/>            .catch((error: Error) =&gt; {<br/>              reject(`Response JSON parsing error: ${error}`);<br/>            })<br/>        )<br/>        .catch((error: Error) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/><br/>  post&lt;TResponse&gt;(url: string, data?: object): Promise&lt;TResponse&gt; {<br/>    return new Promise&lt;TResponse&gt;((resolve, reject) =&gt; {<br/>      fetch(url, {<br/>        method: &quot;POST&quot;,<br/>        headers: {<br/>          &quot;Content-Type&quot;: &quot;application/json&quot;,<br/>        },<br/>        body: JSON.stringify(data),<br/>      })<br/>        .then((response) =&gt;<br/>          response<br/>            .json()<br/>            .then((responseJson) =&gt; {<br/>              resolve(responseJson as TResponse);<br/>            })<br/>            .catch((error: Error) =&gt; {<br/>              reject(`Response JSON parsing error: ${error}`);<br/>            })<br/>        )<br/>        .catch((error: Error) =&gt; {<br/>          reject(error);<br/>        });<br/>    });<br/>  }<br/>}</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; new FetchHttpClient using fetch for get and post</span> </div> </div></div>



<p>For completion, I included <code>POST</code> here as well.</p>



<p>The one last thing I have to do to make our new <code>FetchHttpClient</code> be used in the whole app in place of <code>AxiosHttpClient</code> is to change a single line with export:</p>


<div class="wp-block-wab-pastacode">
	<div class="code-embed-wrapper"> <pre class="language-typescript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-typescript code-embed-code">export const httpClient: IHttpClient = new FetchHttpClient(); // instead of new AxiosHttpClient()</code></pre> <div class="code-embed-infos"> <span class="code-embed-name">httpClient.ts &#8211; AxiosHttpClient replaced with FetchHttpClient</span> </div> </div></div>



<p>and that&#8217;s it! Our whole application now uses <code>fetch</code> for <code>GET</code> and <code>POST</code> HTTP requests <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> And it even still works <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f605.png" alt="😅" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h2 class="wp-block-heading">Summary and source code</h2>



<p>I hope that now you see how important it is to wrap external libraries. We have seen that on JavaScript/TypeScript app example, but this is applicable to any programming language and framework. </p>



<p>It&#8217;s always good to be as independent as possible of 3rd party stuff. Too many times I&#8217;ve been in a situation that some <code>npm</code> package is so extensively used in a project, directly in the source code in hundreds of places, that it cannot be replaced without spending several days on it. Creating wrappers forces us to think abstract, which is another great advantage.</p>



<p>You can find the complete source code here: <a href="https://github.com/dsibinski/codejourney/tree/main/wrapping-external-libraries">https://github.com/dsibinski/codejourney/tree/main/wrapping-external-libraries</a></p>


<script>(function() {
	window.mc4wp = window.mc4wp || {
		listeners: [],
		forms: {
			on: function(evt, cb) {
				window.mc4wp.listeners.push(
					{
						event   : evt,
						callback: cb
					}
				);
			}
		}
	}
})();
</script><!-- Mailchimp for WordPress v4.10.9 - https://wordpress.org/plugins/mailchimp-for-wp/ --><form id="mc4wp-form-1" class="mc4wp-form mc4wp-form-2612" method="post" data-id="2612" data-name="Download a free guide form" ><div class="mc4wp-form-fields"><table bgcolor="#f2f6f5"><tr><td> <p><p>
    <label>
<h1 style="">
  <center>GET A FREE GUIDE <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f381.png" alt="🎁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></center>
      </h1>        
      <h2 style="font-family: Helvetica">
        <center>16 STEPS TO BECOME <br/>.NET FULL STACK WEB DEVELOPER </br>IN 2025</center>
      </h2>
</p>
  <center><div>
	<input type="email" name="EMAIL" placeholder="Email address" required />
    <p>
    <input type="text" name="FNAME" placeholder="Your name"
    required="">
  </p>
  </div>
    <center>

	<center><input type="submit" value="DOWNLOAD THE FREE GUIDE" style="color: #7b1fa2; font-weight:bold; font-size: 20px" /></center>
<p style="font-size: 12px; font-style: italic;">After you sign up, I may be sending you some emails with additional free content from time to time.
<br/>No spam, only awesome stuff</p>
</p></td></tr></table>

</div><label style="display: none !important;">Leave this field empty if you're human: <input type="text" name="_mc4wp_honeypot" value="" tabindex="-1" autocomplete="off" /></label><input type="hidden" name="_mc4wp_timestamp" value="1766882502" /><input type="hidden" name="_mc4wp_form_id" value="2612" /><input type="hidden" name="_mc4wp_form_element_id" value="mc4wp-form-1" /><div class="mc4wp-response"></div></form><!-- / Mailchimp for WordPress Plugin -->
<p>The post <a href="https://www.codejourney.net/how-and-why-to-wrap-external-libraries/">How (And Why?) To Wrap External Libraries?</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-and-why-to-wrap-external-libraries/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4346</post-id>	</item>
		<item>
		<title>How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</title>
		<link>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/</link>
					<comments>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Fri, 30 Apr 2021 04:33:47 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[npm]]></category>
		<category><![CDATA[npm ERR! enoent ENOENT]]></category>
		<category><![CDATA[npm error]]></category>
		<category><![CDATA[npm install package]]></category>
		<category><![CDATA[react]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=3927</guid>

					<description><![CDATA[<p>I recently struggled for a while with an npm error thrown when executing npm install of some package. The error message was npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&#62; 'D:\\WebApp\\node_modules.lz-string.DELETE' Finally, I found a solution and a reason for that issue. The error occurred when I was trying to install&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[


<p>I recently struggled for a while with an <code>npm</code> error thrown when executing <code>npm install</code> of some package. The error message was <code>npm ERR! enoent ENOENT: no such file or directory, rename 'D:\\WebApp\\node_modules\\lz-string' -&gt; 'D:\\WebApp\\node_modules.lz-string.DELETE'</code></p>



<p>Finally, I found a solution and a reason for that issue.</p>



<span id="more-3927"></span>



<p>The error occurred when I was trying to install <code>@testing-library/react</code> npm package. It looked like that:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?ssl=1"><img data-recalc-dims="1" decoding="async" width="1024" height="404" data-attachment-id="3928" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/npm_rename_error/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1360%2C536&amp;ssl=1" data-orig-size="1360,536" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="npm_rename_error" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=300%2C118&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?fit=1024%2C404&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&#038;ssl=1" alt="npm ERR! enoent ENOENT error in Visual Studio Code" class="wp-image-3928" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=1024%2C404&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=300%2C118&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=768%2C303&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?resize=676%2C266&amp;ssl=1 676w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/npm_rename_error.png?w=1360&amp;ssl=1 1360w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>
</div>


<h2 class="wp-block-heading">Hotfix</h2>



<p>If you ever get this error, the hotfix is to follow these steps:</p>



<ul class="wp-block-list"><li>delete <code>node-modules</code> folder</li><li>run command <code>npm cache clean --force</code></li><li>run command <code>npm install</code></li><li>install the package again with <code>npm install your-package-name</code></li></ul>



<p>It should all work fine after that. </p>



<p>If these commands still don&#8217;t solve your issue, and you use <code>git</code> as a source control system, you can try the unbeatable <code>git clean -fdx</code> command. After that, run <code>npm install</code> again. Beware &#8211; it removes all files not checked in to git. It may also remove your IDE settings etc., so use with care.</p>



<p>But it&#8217;s only a hotfix, a solution <em>for now</em> to unblock you.</p>



<h2 class="wp-block-heading">Coldfix (solution)</h2>



<p>The real reason for this issue in my case turned out to be related to <code>jest</code>. However, not to the testing library itself, but to <a href="https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest" target="_blank" rel="noreferrer noopener">jest extension for Visual Studio Code</a>.</p>



<p>The reason for the issue is the jest tests runner working in the background. You can see that in the VS Code bottom bar:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="320" height="158" data-attachment-id="3929" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" data-orig-size="320,158" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="vsCode_jest_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=300%2C148&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?fit=320%2C158&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=320%2C158&#038;ssl=1" alt="Visual Studio Code - jest runner" class="wp-image-3929" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?w=320&amp;ssl=1 320w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/vsCode_jest_runner.png?resize=300%2C148&amp;ssl=1 300w" sizes="(max-width: 320px) 100vw, 320px" /></figure>
</div>


<p>The real solution is to disable jest runner when installing new packages. You can do it with a <code>Jest: Stop Runner</code> command in Ctrl+Shift+P:</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img data-recalc-dims="1" loading="lazy" decoding="async" width="777" height="246" data-attachment-id="3930" data-permalink="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/vscode_jest_stop_runner/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" data-orig-size="777,246" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="VSCode_jest_stop_runner" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=300%2C95&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?fit=777%2C246&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=777%2C246&#038;ssl=1" alt="npm ERR! enoent ENOENT fix in Visual Studio by stopping jest runner" class="wp-image-3930" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?w=777&amp;ssl=1 777w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=300%2C95&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=768%2C243&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2021/04/VSCode_jest_stop_runner.png?resize=676%2C214&amp;ssl=1 676w" sizes="auto, (max-width: 777px) 100vw, 777px" /></figure>
</div>


<p>I don&#8217;t know exactly why this is an issue. I guess jest runner is blocking some files in <code>node-modules</code>, so they cannot be renamed/processed. I hope it helps you too <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/">How to fix: npm ERR! enoent ENOENT: no such file or directory, rename</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-to-fix-npm-err-enoent-enoent-no-such-file-or-directory-rename/feed/</wfw:commentRss>
			<slash:comments>13</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3927</post-id>	</item>
	</channel>
</rss>
