<?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 - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/npm/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/npm/</link>
	<description>Software development blog</description>
	<lastBuildDate>Mon, 07 Jun 2021 14:56:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>npm Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/npm/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How To Replace npm Library With Your Own Implementation?</title>
		<link>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/</link>
					<comments>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 05 May 2021 12:51:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[npm]]></category>
		<guid isPermaLink="false">http://blog.yumasoft.pl/?p=20</guid>

					<description><![CDATA[<p>In one of our projects at Yumasoft we needed to replace npm library&#8217;s implementation with a custom one. This was needed because one of the components we use created their own version (fork) of this npm package (the component uses this library internally). In order to make the component working correctly, we had to use&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/">How To Replace npm Library With Your Own Implementation?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="has-black-color has-text-color">In one of our projects at Yumasoft we needed to replace npm library&#8217;s implementation with a custom one. This was needed because one of the components we use created their own version (fork) of this npm package (the component uses this library internally). In order to make the component working correctly, we had to use the component&#8217;s authors version of the library, instead of using the original npm package. It turned out to not be an easy task, but finally we found a nice and easy solution.</p>



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



<h2 class="wp-block-heading">The problem</h2>



<p>Let&#8217;s say that we install&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>&nbsp;npm package to our web app. This is a kind of package that has many forks. Some companies or individuals fork&nbsp;<a href="https://github.com/ckeditor/ckeditor5-build-classic" target="_blank" rel="noreferrer noopener">ckeditor5-build-classic repository</a>&nbsp;and create their own versions of it, adjusted for use with their products.</p>



<p>The problem is that if you use such a product that has their own fork of&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>, you might want to use this fork as npm package, not the&nbsp;<a href="https://www.npmjs.com/package/@ckeditor/ckeditor5-build-classic" target="_blank" rel="noreferrer noopener">original npm library</a>. However, in most cases there&#8217;s no npm package published for the forked repository.</p>



<p>We need a way to replace the contents (source code) of the original npm library with contents (source code) from the forked library (custom version). Let&#8217;s see how to replace npm library with our own code.</p>



<h2 class="wp-block-heading">The solution</h2>



<p>The first idea that comes to mind could be to create a modified&nbsp;<a href="https://docs.npmjs.com/creating-and-publishing-private-packages" target="_blank" rel="noreferrer noopener">private npm package</a>&nbsp;and use it in our project. However, it requires some maintenance and forking the original library. We found a better solution.</p>



<p>If we go to&nbsp;<em>node_modules</em>&nbsp;folder looking&nbsp;<em>@ckeditor/ckeditor5-build-classic</em>&nbsp;package&#8217;s source, we can see that its source files (bundles) are present in&nbsp;<em>node_modules\@ckeditor\ckeditor5-build-classic\build\</em>&nbsp;folder:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="700" height="273" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content.png" alt="" class="wp-image-21" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content.png 700w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/Ckeditor_node_modules_content-300x117.png 300w" sizes="(max-width: 700px) 100vw, 700px" /></figure></div>



<p>If you check some forks of this library on GitHub, there are also bundles present there, e.g.&nbsp;<a href="https://github.com/formio/ckeditor5-build-classic/tree/master/build" target="_blank" rel="noreferrer noopener">here</a>&nbsp;or&nbsp;<a href="https://github.com/freeshineit/ckeditor5-build-classic/tree/master/dist" target="_blank" rel="noreferrer noopener">here</a>. Let&#8217;s say that we need to take the forked version of&nbsp;<em>ckeditor.js</em>&nbsp;file and replace this file in our original npm library. How do we do that?</p>



<h3 class="wp-block-heading">patch-package</h3>



<p>To achieve this, we need to install&nbsp;<a href="https://www.npmjs.com/package/patch-package" target="_blank" rel="noreferrer noopener">patch-package</a>&nbsp;npm package. This is an awesome, life-saving tool created just for what we need. Kudos to our teammate Przemek who recommended this gold tool to me ?</p>



<p>After installing the package with&nbsp;<strong>npm i patch-package</strong>, we can simply go to our npm library&#8217;s source and modify it. In our case, we replace&nbsp;<em>node_modules\@ckeditor\ckeditor5-build-classic\build\ckeditor.js</em>&nbsp;file contents with the contents of&nbsp;<em>ckeditor.js</em>&nbsp;file from our forked library (for example with&nbsp;<a href="https://github.com/formio/ckeditor5-build-classic/blob/master/build/ckeditor.js" target="_blank" rel="noreferrer noopener">this one</a>). After that, we save the file.</p>



<p>The last step is to generate a patch for our original npm library. We do it by executing the following command:&nbsp;<strong>npx patch-package @ckeditor/ckeditor5-build-classic</strong>&nbsp;(replace&nbsp;<strong>@ckeditor/ckeditor5-build-classic</strong>&nbsp;with your package name). This is how it looks:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="854" height="206" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output.png" alt="" class="wp-image-22" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output.png 854w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output-300x72.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/npx_patch-package_output-768x185.png 768w" sizes="(max-width: 854px) 100vw, 854px" /></figure></div>



<p>We can now see that a new folder called&nbsp;<em>patches</em>&nbsp;was created in our web app&#8217;s directory. In our case, it contains only a single patch file:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="859" height="136" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file.png" alt="" class="wp-image-23" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file.png 859w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file-300x47.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patches_ckeditor_patch_file-768x122.png 768w" sizes="(max-width: 859px) 100vw, 859px" /></figure></div>



<p>What&#8217;s interesting, this file is basically a&nbsp;<em>git diff</em>&nbsp;between the original and modified&nbsp;<em>ckeditor.js</em>&nbsp;files:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="362" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-1024x362.png" alt="" class="wp-image-24" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-1024x362.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-300x106.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff-768x271.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch_file_contents_git_diff.png 1237w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>When we now execute&nbsp;<strong>npx patch-package</strong>&nbsp;command, the changes we&#8217;ve made before are applied to the actual npm library&#8217;s files:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="419" height="82" src="http://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches.png" alt="" class="wp-image-25" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches.png 419w, https://blog.yumasoft.pl/wp-content/uploads/2021/05/patch-package_applying_patches-300x59.png 300w" sizes="auto, (max-width: 419px) 100vw, 419px" /></figure></div>



<p>This is great, because we can now add this command&nbsp;<strong>patch-package</strong>&nbsp;to our build script, so our custom changes to the libraries are applied on every build. It&#8217;s very quick and works like a charm. That&#8217;s how we managed to easily replace npm library with our own implementation ?</p>



<p>We should check in the patch files to our GitHub repository, so our teammates have them locally and our build server can also apply the patches.</p>



<p>Since this moment, we modified the npm library&#8217;s code, so everyone who imports and uses this library in our app will be actually using our modified version of it.</p>



<p>If needed, we can easily revert the changes with&nbsp;<strong>npx patch-package &#8211;reverse</strong>. It restores the npm libraries to their original versions (thanks to the patch file stored as&nbsp;<em>git diff</em>).</p>



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



<p>I hope you found this article helpful. You can use the&nbsp;<em>patch-package</em>&nbsp;library not only to replace the npm library&#8217;s source files, but also as a quick-fix for bugs found in these libraries. No need to wait for the open-source community to fix the issue or the PR to be merged. You can make the patch quickly and at the same time report the issue on the concerned package&#8217;s GitHub. The tool even offers a feature to create a GH issue in the original repo with the diff containing your changes by using command&nbsp;<strong>npx patch-package package-name &#8211;create-issue</strong>.</p>



<p>For us at Yumasoft, the&nbsp;<a href="https://www.npmjs.com/package/patch-package" target="_blank" rel="noreferrer noopener">patch-package</a>&nbsp;is a life-saver. Maybe it also saves you some headache? ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/">How To Replace npm Library With Your Own Implementation?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/05/how-to-replace-npm-library-with-your-own-implementation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
