<?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 &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/npm/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Sat, 10 Nov 2018 11:43:33 +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>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>NPM &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Having a Docker Container as Your Private NPM Registry &#8211; The Easy Way</title>
		<link>https://mail.sibeeshpassion.com/having-a-docker-container-as-your-private-npm-registry-the-easy-way/</link>
					<comments>https://mail.sibeeshpassion.com/having-a-docker-container-as-your-private-npm-registry-the-easy-way/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 10 Nov 2018 11:43:33 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Docker and NPM Private Registry]]></category>
		<category><![CDATA[Dockerizing]]></category>
		<category><![CDATA[NPM]]></category>
		<category><![CDATA[NPM Registry]]></category>
		<category><![CDATA[Private NPM Registry]]></category>
		<category><![CDATA[Verdaccio]]></category>
		<category><![CDATA[Verdaccio with Docker]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13347</guid>

					<description><![CDATA[[toc] Introduction At some point in our life, we may need almost everything in a private scope, and the same happens with your NPM packages as well. You have created a private NPM package for your company or for yourself and you wanted to host it somewhere, you might have already looked for some options like a paid version of npmjs and many more. But, you were not happy doing it, and somehow you have reached in this article, and I am sure you are expecting something with Docker here. Yes, we are actually going to change our Docker container [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>At some point in our life, we may need almost everything in a private scope, and the same happens with your NPM packages as well. You have created a private NPM package for your company or for yourself and you wanted to host it somewhere, you might have already looked for some options like a paid version of npmjs and many more. But, you were not happy doing it, and somehow you have reached in this article, and I am sure you are expecting something with <a href="https://sibeeshpassion.com/category/docker/">Docker</a> here. Yes, we are actually going to change our Docker container instance to a private NPM registry, and once it is ready you can always host your private npm packages to that registry. If you are not sure of how to create an npm package, please read my Sounds cool? Then, let&#8217;s jump into it. I hope you will find this post useful.</p>
<h2>Background</h2>
<p>As I mentioned before, I am going to create a Container Instance and will be changing it to a private NPM registry. I will be using <a href="https://sibeeshpassion.com/category/azure/">Microsoft Azure</a>, if you don&#8217;t have a valid Azure subscription, I strongly recommend you to create one.</p>
<h2>Setting up a Docker Private NPM Registry</h2>
<p>Once you get the Azure subscription, you can start the setups.</p>
<h3>Creating Azure Container Registry</h3>
<p>Do you have a GitHub account? Do you know what we can do with a Github account? Yes, we can upload our projects/source codes there and we can do versioning, we can create branches and many more. You can consider that the Azure Container Registry is a similar service to that. An Azure Container Registry or ACR is a private Docker registry where you can easily manage all of your Docker container images in the same place. Isn&#8217;t that cool? According to Microsoft, you can use Azure Container Registry to:</p>
<ul>
<li>Store and manage container images across all types of Azure deployments</li>
<li>Use familiar, open-source Docker command line interface (CLI) tools</li>
<li>Keep container images near deployments to reduce latency and costs</li>
<li>Simplify registry access management with Azure Active Directory</li>
<li>Maintain Windows and Linux container images in a single Docker registry</li>
</ul>
<p>To create the Azure Container Registry, you should have a valid Azure subscription. If you don&#8217;t have one, please get one, that will help you follow this article.</p>
<p>Once you logged in to the Portal. You can click on the + icon, Create a resource, and search for the Container Registry.</p>
<div id="attachment_13349" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Search-Container-Registry-e1541839869109.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-13349" class="size-full wp-image-13349" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Search-Container-Registry-e1541839869109.png" alt="Search Container Registry" width="634" height="304" /></a><p id="caption-attachment-13349" class="wp-caption-text">Search Container Registry</p></div>
<p>When you click the Container Registry, you will be asked to give the details as shown in the preceding image.</p>
<div id="attachment_13350" style="width: 310px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Registry-Details.png"><img decoding="async" aria-describedby="caption-attachment-13350" class="wp-image-13350" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Registry-Details-225x300.png" alt="Container Registry Details" width="300" height="400" srcset="/wp-content/uploads/2018/11/Container-Registry-Details-225x300.png 225w, /wp-content/uploads/2018/11/Container-Registry-Details-400x533.png 400w, /wp-content/uploads/2018/11/Container-Registry-Details-450x600.png 450w, /wp-content/uploads/2018/11/Container-Registry-Details.png 558w" sizes="(max-width: 300px) 100vw, 300px" /></a><p id="caption-attachment-13350" class="wp-caption-text">Container Registry Details</p></div>
<p>Please remember to enable the Admin user, as you will be able to use the registry name as username and admin user access key as the password to docker login to your container registry only if it is enabled. I will explain the same in the coming section.</p>
<p>Once you create your Container Registry, you will be given a login server as yourregistryname.azurecr.io. You can always see other information in the overview section. If you click on the Access Keys link under the settings, you can see the username and the password for your Container Registry. Remember we have enabled the Admin User property while creating the Container Registry?</p>
<div id="attachment_13351" style="width: 463px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Registry-Settings.png"><img decoding="async" aria-describedby="caption-attachment-13351" class="size-full wp-image-13351" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Registry-Settings.png" alt="Container Registry Settings" width="453" height="662" srcset="/wp-content/uploads/2018/11/Container-Registry-Settings.png 453w, /wp-content/uploads/2018/11/Container-Registry-Settings-205x300.png 205w, /wp-content/uploads/2018/11/Container-Registry-Settings-400x585.png 400w, /wp-content/uploads/2018/11/Container-Registry-Settings-411x600.png 411w" sizes="(max-width: 453px) 100vw, 453px" /></a><p id="caption-attachment-13351" class="wp-caption-text">Container Registry Settings</p></div>
<div class="fxc-base azc-control azc-editableControl azc-validatableControl azc-itemList azc-optionPicker azc-validation-border azc-noRadio azc-validatableControl-valid-validated azc-is-edited" data-control="true" data-editable="true" data-canfocus="true"></div>
<div data-control="true" data-editable="true" data-canfocus="true">Please be noted that the registry name and the admin username is same.</div>
<h3 data-control="true" data-editable="true" data-canfocus="true">Creating a Docker Container in Local</h3>
<div data-control="true" data-editable="true" data-canfocus="true">To do this step, you should download and install Docker in your machine. Once it is installed, please make sure that the docker is running in your machine. If yes, let&#8217;s go ahead and start some amazing things together. Let&#8217;s just try Docker version command first to make sure that it is working.</div>
<div data-control="true" data-editable="true" data-canfocus="true"></div>
<div data-control="true" data-editable="true" data-canfocus="true">
<div id="attachment_13352" style="width: 666px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-version.png"><img decoding="async" aria-describedby="caption-attachment-13352" class="size-full wp-image-13352" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-version.png" alt="Docker version" width="656" height="102" srcset="/wp-content/uploads/2018/11/Docker-version.png 656w, /wp-content/uploads/2018/11/Docker-version-300x47.png 300w, /wp-content/uploads/2018/11/Docker-version-400x62.png 400w" sizes="(max-width: 656px) 100vw, 656px" /></a><p id="caption-attachment-13352" class="wp-caption-text">Docker version</p></div>
</div>
<div data-control="true" data-editable="true" data-canfocus="true"></div>
<div data-control="true" data-editable="true" data-canfocus="true">Cool, the docker is working fine. Now we can start creating a local NPM registry. To do so I am going to pull a package from Docker hub, which is nothing but Verdaccio.</div>
<div data-control="true" data-editable="true" data-canfocus="true"></div>
<div data-control="true" data-editable="true" data-canfocus="true">Verdaccio is a private npm proxy registry which is built on top of Node.js. According to the Verdaccio documentation,</div>
<div data-control="true" data-editable="true" data-canfocus="true">
<ul>
<li>It&#8217;s a web app based on Node.js</li>
<li>It&#8217;s a private npm registry</li>
<li>It&#8217;s a local network proxy</li>
<li>It&#8217;s a Pluggable application</li>
<li>It&#8217;s a fairly easy install and use</li>
<li>It is 100% compatible with yarn, npm, and pnpm</li>
<li>It was <strong>forked</strong> based on and<code>sinopia@1.4.0</code> 100% <strong>backward compatible</strong>.</li>
</ul>
<p>Let&#8217;s pull the image of Verdaccio now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker pull verdaccio/verdaccio</pre>
<p>Once you have done that, try to run &#8220;docker images&#8221;  you should be able to see your Verdaccio image.</p>
</div>
<pre class="EnlighterJSRAW" data-enlighter-language="null">verdaccio/verdaccio           latest              c542d9e516e4        3 weeks ago         659MB</pre>
<p>It may take a few minutes to pull the entire image. As I have already pulled the latest image, it will be quick for me and get the output as below.</p>
<div id="attachment_13353" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-pull-verdaccio.png"><img decoding="async" aria-describedby="caption-attachment-13353" class="wp-image-13353 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-pull-verdaccio-e1541843418760.png" alt="Docker pull verdaccio" width="634" height="91" /></a><p id="caption-attachment-13353" class="wp-caption-text">Docker pull verdaccio</p></div>
<p>You can also tag the version you need, just in case.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker pull verdaccio/verdaccio:3</pre>
<p>Now it is time to run the Verdaccio, are you ready?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio</pre>
<p>The last argument is our image name, if you don&#8217;t have such an image in your local, the same will be pulled from the Docker hub. As you might have noticed already we are using the port 4873, and the same will be mapped to the container instance port later when we create the container instance. Now please make sure that you are getting an output as preceding.</p>
<div id="attachment_13354" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Verdaccio-docker-run.png"><img decoding="async" aria-describedby="caption-attachment-13354" class="wp-image-13354 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Verdaccio-docker-run-e1541844369136.png" alt="Verdaccio docker run" width="634" height="90" /></a><p id="caption-attachment-13354" class="wp-caption-text">Verdaccio docker run</p></div>
<p>You should now be able to go into http://localhost:4873 and see that verdaccio is running successfully.</p>
<h3>Pushing Local Container to Azure Container Registry</h3>
<p>Now that we have an Azure Container Registry running and a local container which can act as a private NPM registry. What are we waiting for? Let&#8217;s just push this to our ACR. Before that, we need to login to our Azure Container Registry server.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker login sibeeshpassion.azurecr.io</pre>
<p>Once you run the above command with the correct credential, you will be able to see the output as below.</p>
<div id="attachment_13355" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-login-Azure-container-registry.png"><img decoding="async" aria-describedby="caption-attachment-13355" class="wp-image-13355 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Docker-login-Azure-container-registry-e1541845257194.png" alt="Docker login Azure container registry" width="634" height="97" /></a><p id="caption-attachment-13355" class="wp-caption-text">Docker login Azure container registry</p></div>
<p>Now let&#8217;s just tag our image, as this will help to manage our images easily in the Container Registry.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker tag &lt;image name&gt; &lt;login server&gt;/&lt;repository name&gt;/&lt;name&gt;:v1
</pre>
<p>For example, I used this command as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker tag verdaccio/verdaccio sibeeshpassion.azurecr.io/verdaccio/verdaccio:v1</pre>
<p>Let&#8217;s push our image to the Container Registry now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">docker push sibeeshpassion.azurecr.io/verdaccio/verdaccio:v1</pre>
<p>If everything goes well, you must see an output as preceding.</p>
<div id="attachment_13356" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Pushing-to-Container-Registry.png"><img decoding="async" aria-describedby="caption-attachment-13356" class="wp-image-13356 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Pushing-to-Container-Registry-e1541846656271.png" alt="Pushing to Container Registry" width="634" height="191" /></a><p id="caption-attachment-13356" class="wp-caption-text">Pushing to Container Registry</p></div>
<p>Now, you can log in to your Azure portal and check the repositories of your Azure Container Registry. You should be able to see your image with the tag we have provided.</p>
<div id="attachment_13358" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Repository-in-Azure-Container-Registry.png"><img decoding="async" aria-describedby="caption-attachment-13358" class="size-full wp-image-13358" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Repository-in-Azure-Container-Registry.png" alt="Repository in Azure Container Registry" width="634" height="404" srcset="/wp-content/uploads/2018/11/Repository-in-Azure-Container-Registry.png 634w, /wp-content/uploads/2018/11/Repository-in-Azure-Container-Registry-300x191.png 300w, /wp-content/uploads/2018/11/Repository-in-Azure-Container-Registry-400x255.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-13358" class="wp-caption-text">Repository in Azure Container Registry</p></div>
<h3>Creating a Container Instance</h3>
<p>Now, we have a valid Container Registry which can act as a private NPM registry and is that enough? N,o it is not, we are not finished yet. We need a place where this registry can run. Well, Azure has everything we need, we are going to create a Container Instance for this purpose. A container instance is a simplest and fastest way to run a single container. While creating the Container Instance, you will have to go through 3 steps.</p>
<ol>
<li>Configure the basic settings</li>
<li>Specify container requirements</li>
<li>Container instance</li>
</ol>
<h4>Configure Basic Settings</h4>
<p>You can configure the settings as per your needs, I have configured mine as below.</p>
<div id="attachment_13359" style="width: 583px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Instance-Basic-Settings.png"><img decoding="async" aria-describedby="caption-attachment-13359" class="size-full wp-image-13359" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Container-Instance-Basic-Settings.png" alt="Container Instance Basic Settings" width="573" height="1075" srcset="/wp-content/uploads/2018/11/Container-Instance-Basic-Settings.png 573w, /wp-content/uploads/2018/11/Container-Instance-Basic-Settings-160x300.png 160w, /wp-content/uploads/2018/11/Container-Instance-Basic-Settings-546x1024.png 546w, /wp-content/uploads/2018/11/Container-Instance-Basic-Settings-400x750.png 400w, /wp-content/uploads/2018/11/Container-Instance-Basic-Settings-320x600.png 320w" sizes="(max-width: 573px) 100vw, 573px" /></a><p id="caption-attachment-13359" class="wp-caption-text">Container Instance Basic Settings</p></div>
<p>Please be noted that I have given the container image type as Private.</p>
<h4>Specify Container Requirements</h4>
<p>Here in this step, I am going to select the Public IP address as &#8220;Yes&#8221;, so that I can set the DNS label while configuring. And I will also set the port as 4873.</p>
<div id="attachment_13360" style="width: 534px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Network-Settings-in-Azure-Container-Instance.png"><img decoding="async" aria-describedby="caption-attachment-13360" class="size-full wp-image-13360" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Network-Settings-in-Azure-Container-Instance.png" alt="Network Settings in Azure Container Instance" width="524" height="402" srcset="/wp-content/uploads/2018/11/Network-Settings-in-Azure-Container-Instance.png 524w, /wp-content/uploads/2018/11/Network-Settings-in-Azure-Container-Instance-300x230.png 300w, /wp-content/uploads/2018/11/Network-Settings-in-Azure-Container-Instance-400x307.png 400w" sizes="(max-width: 524px) 100vw, 524px" /></a><p id="caption-attachment-13360" class="wp-caption-text">Network Settings in Azure Container Instance</p></div>
<h4>Container Instance</h4>
<p>In this step, you can verify all of your settings, and in the background, Azure will validate the settings and if it is valid, the OK button will be enabled, click OK once it is enabled. It may take some time to build your resource and once it is done you can always go to the overview of your Container Instance and run it with the port 4873. For example, I can see my container in this URL http://sibeeshpassion.westeurope.azurecontainer.io:4873/#/.</p>
<h2>Pushing Your Own Private NPM Package to Docker Private NPM Registry</h2>
<p>Wow!, we have a private Docker NPM registry now. Let&#8217;s just try to push our private NPM package to this registry now. I have created my own NPM package a few days back, if you want to know how, you can always read this article: <a href="https://medium.com/@sibeeshvenu/create-your-own-npm-package-private-or-public-60edc2c42f68">Create Your Own NPM Package — Private or Public</a>. I will open my package in the vscode.</p>
<p>Let&#8217;s add the user before we publish the package.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm adduser --registry=http://sibeeshpassion.westeurope.azurecontainer.io:4873/#/</pre>
<p>The above command will ask you for the username and password.</p>
<div id="attachment_13361" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/NPM-Publish-with-Private-Registry-AddUser.png"><img decoding="async" aria-describedby="caption-attachment-13361" class="size-full wp-image-13361" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/NPM-Publish-with-Private-Registry-AddUser.png" alt="NPM Publish with Private Registry AddUser" width="634" height="140" srcset="/wp-content/uploads/2018/11/NPM-Publish-with-Private-Registry-AddUser.png 634w, /wp-content/uploads/2018/11/NPM-Publish-with-Private-Registry-AddUser-300x66.png 300w, /wp-content/uploads/2018/11/NPM-Publish-with-Private-Registry-AddUser-400x88.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-13361" class="wp-caption-text">NPM Publish with Private Registry AddUser</p></div>
<p>Now we can run our publish command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm publish dist --registry=http://sibeeshpassion.westeurope.azurecontainer.io:4873/#/</pre>
<p>You can get an output as preceding if the publishing is successful.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm notice === Tarball Details ===
npm notice name:          org-npm-package
npm notice version:       1.1.1
npm notice package size:  7.8 kB
npm notice unpacked size: 37.9 kB
npm notice shasum:        f1d8e033bb6d2545502802510258cf9f8bcc9ab3
npm notice integrity:     sha512-3MuplP75D0gpj[...]Z5KFo0DNbzGFw==
npm notice total files:   22
npm notice
+ org-npm-package@1.1.1</pre>
<p>What are you waiting for, just go and see your package in your Docker, I was eagerly waiting for this moment, and I am going to see my package by hitting my container here http://sibeeshpassion.westeurope.azurecontainer.io:4873/#/detail/org-npm-package.</p>
<h2>Using Your Private Package from the Docker Private NPM Registry</h2>
<p>Just run the below command and your private package will be pulled from your private Docker NPM registry.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm install org-npm-package --registry=http://sibeeshpassion.westeurope.azurecontainer.io:4873</pre>
<h2><span id="conclusion_1"><span id="conclusion">Conclusion</span></span></h2>
<p>Congratulation, we have successfully created a Docker Private NPM Registry, go ahead and create as many private packages you wish and move those to your Private NPM registry. Thanks a lot for following/reading this article. I really appreciate your time and effort.  In this article, we have learned how to,</p>
<ol>
<li>Create an Azure Container Registry</li>
<li>Create a Local Docker Container</li>
<li>Create Verdaccio image and run it in a local docker container</li>
<li>Push the local Verdaccio image to the Azure Container Registry</li>
<li>Set up the Docker Container Instance using the Azure Container Registry</li>
<li>Pushing the NPM package to the Private Docker NPM registry</li>
<li>Pulling the package from the Private registry</li>
</ol>
<h2><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h2>
<p>Thanks a lot for reading. I will come back with another post on the same topic very soon. Did I miss anything that you may think which is needed? Could you find this post as useful? Kindly do not forget to share me your feedback.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/having-a-docker-container-as-your-private-npm-registry-the-easy-way/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Create Your Own NPM Package &#8211; Private or Public</title>
		<link>https://mail.sibeeshpassion.com/create-your-own-npm-package-private-or-public/</link>
					<comments>https://mail.sibeeshpassion.com/create-your-own-npm-package-private-or-public/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Mon, 05 Nov 2018 21:36:57 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[create your own npm package]]></category>
		<category><![CDATA[custom package angular]]></category>
		<category><![CDATA[NPM]]></category>
		<category><![CDATA[npm package]]></category>
		<category><![CDATA[publish npm package]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13326</guid>

					<description><![CDATA[[toc] Introduction Wow, you are going to create your own package? Either a private one or a public one? You were looking for a documentation on how to create your own package in NPM, and you ended up landing here? No, worries, in this post I will help you with that. At the end of this article, you will get to know how can you create your own package in the NPM registry. Enough talking, let&#8217;s jump into the coding. I hope you will find this post useful. Source Code The source code can be found here. Background Sometimes creating your [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>Wow, you are going to create your own package? Either a private one or a public one? You were looking for a documentation on how to create your own package in NPM, and you ended up landing here? No, worries, in this post I will help you with that. At the end of this article, you will get to know how can you create your own package in the NPM registry. Enough talking, let&#8217;s jump into the coding. I hope you will find this post useful.</p>
<h2>Source Code</h2>
<p>The source code can be found <a href="https://github.com/SibeeshVenu/Your-Own-Npm-Package">here</a>.</p>
<h2>Background</h2>
<p>Sometimes creating your own packages will be essential especially when you work with some big companies/clients where they can reuse their components/packages. Doesn&#8217;t it really make sense? As a developer, it is our job to give that option to our client. In this article, I will be creating</p>
<ul>
<li>an <a href="https://sibeeshpassion.com/category/angular/">Angular</a> application</li>
<li>the build and publish the angular application to the NPM registry</li>
<li>another application, where we can use the NPM package which we have created</li>
</ul>
<p>Before you start with, please make sure that you have an account in npmjs, so that you can publish your package there. You can always have a private space in NPM registry, but you must pay for that service, hence in this article, I will walk you through how we can publish your packages to publically, however the steps are going to be same for Private as well.</p>
<h2>Creating your Angular application</h2>
<p>The first thing we are going to do is to create an application and I will name this org-npm-package.</p>
<h3>Installing Angular CLI</h3>
<p>Yes, as you guessed, we are using Angular CLI. If you haven&#8217;t installed Angular CLI, I recommend you to install the same. It is a great CLI tool for Angular, I am sure you will love that. You can do that by running the below command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm install -g @angular/cli</pre>
<p>Once we set up this project we will be using the Angular CLI commands and you can check Angular CLI documentation for understanding the things you can do with the CLI.</p>
<h3>Generating a new project</h3>
<p>Now it is time to generate our new project. We can use the below command for the same.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">ng new org-npm-package</pre>
<p>And you will be able to see all the hard work this CLI is doing for us. Now that we have created our application, let&#8217;s run our application and see if it is working or not.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">ng serve --open (if you need to open the browser by the app)
ng serve (if you want to manually open the browser).
You can always use 'ng s' as well
</pre>
<p><span style="font-size: 14px;">The command will build your application and run it in the browser.</span></p>
<p>As we develop we will be using the Angular material for the design and we can install it now itself along with the animation and cdk. With the Angular 6+ versions you can also do this by following the below command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">ng add @angular/material</pre>
<h3>Generate menu module and ng-private-menu component</h3>
<p>Now we have an application to work with and let&#8217;s create a module called Menu and a component ng-private-menu now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">ng g m modules/menu
ng g c modules/ng-private-menu</pre>
<p>The above command will generate all the files you need to work with. I am going to edit only the HTML of the ng-private-menu component for myself and not going to add any logic. You can add anything you wish.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">&lt;div&gt;
  &lt;button mat-button [matMenuTriggerFor]="animals"&gt;Animal index&lt;/button&gt;

  &lt;mat-menu #animals="matMenu"&gt;
    &lt;button mat-menu-item [matMenuTriggerFor]="vertebrates"&gt;Vertebrates&lt;/button&gt;
    &lt;button mat-menu-item [matMenuTriggerFor]="invertebrates"&gt;Invertebrates&lt;/button&gt;
  &lt;/mat-menu&gt;

  &lt;mat-menu #vertebrates="matMenu"&gt;
    &lt;button mat-menu-item [matMenuTriggerFor]="fish"&gt;Fishes&lt;/button&gt;
    &lt;button mat-menu-item [matMenuTriggerFor]="amphibians"&gt;Amphibians&lt;/button&gt;
    &lt;button mat-menu-item [matMenuTriggerFor]="reptiles"&gt;Reptiles&lt;/button&gt;
    &lt;button mat-menu-item&gt;Birds&lt;/button&gt;
    &lt;button mat-menu-item&gt;Mammals&lt;/button&gt;
  &lt;/mat-menu&gt;

  &lt;mat-menu #invertebrates="matMenu"&gt;
    &lt;button mat-menu-item&gt;Insects&lt;/button&gt;
    &lt;button mat-menu-item&gt;Molluscs&lt;/button&gt;
    &lt;button mat-menu-item&gt;Crustaceans&lt;/button&gt;
    &lt;button mat-menu-item&gt;Corals&lt;/button&gt;
    &lt;button mat-menu-item&gt;Arachnids&lt;/button&gt;
    &lt;button mat-menu-item&gt;Velvet worms&lt;/button&gt;
    &lt;button mat-menu-item&gt;Horseshoe crabs&lt;/button&gt;
  &lt;/mat-menu&gt;

  &lt;mat-menu #fish="matMenu"&gt;
    &lt;button mat-menu-item&gt;Baikal oilfish&lt;/button&gt;
    &lt;button mat-menu-item&gt;Bala shark&lt;/button&gt;
    &lt;button mat-menu-item&gt;Ballan wrasse&lt;/button&gt;
    &lt;button mat-menu-item&gt;Bamboo shark&lt;/button&gt;
    &lt;button mat-menu-item&gt;Banded killifish&lt;/button&gt;
  &lt;/mat-menu&gt;

  &lt;mat-menu #amphibians="matMenu"&gt;
    &lt;button mat-menu-item&gt;Sonoran desert toad&lt;/button&gt;
    &lt;button mat-menu-item&gt;Western toad&lt;/button&gt;
    &lt;button mat-menu-item&gt;Arroyo toad&lt;/button&gt;
    &lt;button mat-menu-item&gt;Yosemite toad&lt;/button&gt;
  &lt;/mat-menu&gt;

  &lt;mat-menu #reptiles="matMenu"&gt;
    &lt;button mat-menu-item&gt;Banded Day Gecko&lt;/button&gt;
    &lt;button mat-menu-item&gt;Banded Gila Monster&lt;/button&gt;
    &lt;button mat-menu-item&gt;Black Tree Monitor&lt;/button&gt;
    &lt;button mat-menu-item&gt;Blue Spiny Lizard&lt;/button&gt;
    &lt;button mat-menu-item disabled&gt;Velociraptor&lt;/button&gt;
  &lt;/mat-menu&gt;
&lt;/div&gt;</pre>
<p>Once you have added that HTML, you should import some modules to your menu.module.ts file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgPrivateMenuComponent } from '../ng-private-menu/ng-private-menu.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatMenuModule, MatButtonModule } from '@angular/material';


@NgModule({
  declarations: [
    NgPrivateMenuComponent
  ],
  imports: [
    BrowserAnimationsModule,
    CommonModule,
    MatMenuModule,
    MatButtonModule
  ],
  exports: [
    NgPrivateMenuComponent
  ]
})
export class MenuModule { }</pre>
<h3>Set up app.module.ts</h3>
<p>Now it is time to set our App Module. Every Angular app will be having at least one NgModule class <code>AppModule</code>resides in <code>app.module.ts</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MenuModule } from './modules/menu/menu.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    MenuModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
</pre>
<div>
<h4>Run your application</h4>
<p>Once you have done everything, you will be able to run &#8220;ng serve&#8221; and see your application working in the browser.</p>
<div id="attachment_13327" style="width: 447px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/Angular-application-output.png"><img decoding="async" aria-describedby="caption-attachment-13327" class="size-full wp-image-13327" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/Angular-application-output.png" alt="Angular application output" width="437" height="314" srcset="/wp-content/uploads/2018/11/Angular-application-output.png 437w, /wp-content/uploads/2018/11/Angular-application-output-300x216.png 300w, /wp-content/uploads/2018/11/Angular-application-output-400x287.png 400w" sizes="(max-width: 437px) 100vw, 437px" /></a><p id="caption-attachment-13327" class="wp-caption-text">Angular application output</p></div>
</div>
<h2>Creating the NPM package</h2>
<p>Whenever we do something, it is always a good idea to check whether something is already there or not, to help our development easier. That&#8217;s the advantage of using NPM public registry, we can always search for public repositories or packages which will definitely help in our development process. We have such packages in this case as well, so we are going to use the package called &#8220;ng-packagr&#8221;.</p>
<p>According to the package documentation, the packagr is a tool which transpile your libraries to Angular package format. It is a great package which does its job very well. I would like to give a huge round of applause to the developers/collaborators of that package. Please feel free to see the documentation of that package. You can install the same by running the command below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm i ng-packagr</pre>
<h3>Setting up the package.json</h3>
<p>The first thing we are going to do is, to set the package.json as per the requirement of the packagr. Please add the preceding code to your package.json file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">"$schema": "./node_modules/ng-packagr/package.schema.json",
  "name": "@sibeeshpassion/org-npm-package",
  "version": "2.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "packagr": "ng-packagr -p package.json"
  },
  "ngPackage": {
    "lib": {
      "entryFile": "public_api.ts"
    },
    "whitelistedNonPeerDependencies": [
      "@angular/animations",
      "@angular/cdk",
      "@angular/common",
      "@angular/compiler",
      "@angular/forms",
      "@angular/core",
      "@angular/http",
      "@angular/material",
      "@angular/platform-browser",
      "@angular/platform-browser-dynamic",
      "@angular/router",
      "core-js",
      "rxjs",
      "tsickle",
      "zone.js"
    ]
  }</pre>
<p>The<strong> $schema </strong>is for making the editing easier and it will add the JSON editing support, especially if you are using the IDE VSCode.</p>
<p>The <strong>name</strong> is the name of your package. And it is a scoped name here. Did you notice that I have used @ before the SibeeshPassion? A scoped name should be started with @ and should be followed by a slash (/). It is a way of grouping all of your packages together in a common place. Doesn&#8217;t it make sense to have all of your company packages under your company name? It has some other benefits as well.</p>
<ol>
<li>You can get unique names for your package, as you will be the only one who can add a package to that group</li>
<li>To install such packages, you need to use @your-org-name/your-package-name</li>
</ol>
<p>You can always read more on the same topic <a href="https://docs.npmjs.com/misc/scope">here</a>.</p>
<p>The <strong>script packagr </strong>is a custom script so that you can just run &#8220;npm run packagr&#8221; and everything else will be done in the background.</p>
<p>The <strong>ngPackage </strong>is the main settings for the packagr package, as it sets the entry point for our packaging. The file public_api.ts should be placed with the same directory of the package.json file. In our case, the public_api.ts contents will be as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">export * from './src/app/modules/menu/menu.module';
</pre>
<p>We are exporting from our module menu.module.</p>
<div>
<div>The<strong> whitelistedNonPeerDependencies </strong>is to whitelist the dependent packages, if you miss this step, you will see the below error.</div>
<div>
<pre class="EnlighterJSRAW" data-enlighter-language="null">Distributing npm packages with 'dependencies' is not recommended. Please consider adding @angular/animations to 'peerDependencies' or remove it from 'dependencies'.

BUILD ERROR
Dependency @angular/animations must be explicitly whitelisted.
</pre>
<p>Please keep in mind that if you make the property private to true, the npm will refuse to publish it, this is for avoiding accidental publication of private repositories.</p>
</div>
</div>
<h3>Run the commands and generate the dist</h3>
<p>As we have already configured our package.json file, now we can go and run the commands. Are you ready? The first thing we are going to run is &#8220;npm run packagr&#8221;. It will do the following things for you.</p>
<ul>
<li>Building Angular Package</li>
<li>Building entry point &#8216;@sibeeshpassion/org-npm-package&#8217;</li>
<li>Compiling TypeScript sources through ngc</li>
<li>Bundling to FESM2015</li>
<li>Bundling to FESM5</li>
<li>Bundling to UMD</li>
<li>Minifying UMD bundle</li>
<li>Copying declaration files</li>
<li>Writing package metadata</li>
</ul>
<p>From the image below, you can understand that it is creating a folder called dist.</p>
<div id="attachment_13329" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/npm-run-packagr-e1541451015510.png"><img decoding="async" aria-describedby="caption-attachment-13329" class="wp-image-13329 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/npm-run-packagr-e1541451066893.png" alt="npm run packagr" width="634" height="237" /></a><p id="caption-attachment-13329" class="wp-caption-text">npm run packagr</p></div>
<p>If you just look at the dist folder you can see that all the packaging work has been done.</p>
<div id="attachment_13330" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/dist-folder-after-packaging-e1541451333557.png"><img decoding="async" aria-describedby="caption-attachment-13330" class="size-full wp-image-13330" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/dist-folder-after-packaging-e1541451333557.png" alt="dist folder after packaging" width="634" height="634" /></a><p id="caption-attachment-13330" class="wp-caption-text">dist folder after packaging</p></div>
<h3>Publishing your package to NPM registry</h3>
<p>As our package is ready, we can go ahead and start publishing our package to the NPM registry.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm publish dist</pre>
<p>When you run the above command, if you haven&#8217;t signed up for private packages, you will get an error as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm ERR! publish Failed PUT 402
npm ERR! code E402
npm ERR! You must sign up for private packages : @sibeeshpassion/org-npm-package</pre>
<p>Let&#8217;s just publish this package publically now, so that I don&#8217;t want to pay anything. Before you do that, go to your package.json file and change the name from &#8220;name&#8221;: &#8220;@sibeeshpassion/org-npm-package&#8221; to &#8220;name&#8221;: &#8220;org-npm-package&#8221;. Run &#8220;npm run packagr&#8221; again. It will update your dist folder with the new files. The contents of the dist folder will be a little than the one we had for our organization.</p>
<div id="attachment_13331" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/dist-folder-after-packaging-publically-e1541452203501.png"><img decoding="async" aria-describedby="caption-attachment-13331" class="size-full wp-image-13331" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/dist-folder-after-packaging-publically-e1541452203501.png" alt="dist folder after packaging publically" width="634" height="621" /></a><p id="caption-attachment-13331" class="wp-caption-text">dist folder after packaging publically</p></div>
<p>Let&#8217;s just run &#8220;npm publish dist&#8221; again, this time we should not see any errors. Please be noted that you should run &#8220;npm login&#8221; before you run npm publish command.</p>
<div id="attachment_13333" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/npm-publish-dist-e1541452493726.png"><img decoding="async" aria-describedby="caption-attachment-13333" class="wp-image-13333 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/npm-publish-dist-e1541452540426.png" alt="npm publish dist" width="634" height="214" /></a><p id="caption-attachment-13333" class="wp-caption-text">npm publish dist</p></div>
<p>Now if you look at <a href="https://www.npmjs.com/~sibeeshvenu">your NPM registry</a> you should be able to see <a href="https://www.npmjs.com/package/org-npm-package">your package</a>.</p>
<h2>Using the package in another solution</h2>
<p>Wow!, we have successfully created an Angular NPM package, now we can easily use the same in our other applications. Let&#8217;s just create another application.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">ng new use-org-npm-package</pre>
<p>Once the application is been created, let&#8217;s just install the new package we have created.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm i org-npm-package</pre>
<p>Now, we can add our MenuModule to the new application&#8217;s app.module.ts file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { MenuModule } from 'org-npm-package';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MenuModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
</pre>
<p>The only thing pending now is, to use the app-ng-private-menu in your app.component.html.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">&lt;app-ng-private-menu&gt;&lt;/app-ng-private-menu&gt;</pre>
<h2>Output</h2>
<p>Let&#8217;s just run our new application and see everything is working as expected.</p>
<div id="attachment_13334" style="width: 644px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/11/my-own-npm-package-e1541453211484.png"><img decoding="async" aria-describedby="caption-attachment-13334" class="wp-image-13334 size-full" src="https://sibeeshpassion.com/wp-content/uploads/2018/11/my-own-npm-package-e1541453246899.png" alt="my own npm package" width="634" height="617" /></a><p id="caption-attachment-13334" class="wp-caption-text">my own npm package</p></div>
<div class="mceTemp"></div>
<h2><span id="conclusion_1"><span id="conclusion">Conclusion</span></span></h2>
<p>Congratulation, you have created and used your own NPM package. In this article, we have learned how to,</p>
<ol>
<li>Create an angular 7 application</li>
<li>Create a Private NPM package</li>
<li>Create a Public NPM package</li>
<li>Publish a package to NPM registry</li>
<li>Use the created package in another solution</li>
</ol>
<p>We will make this package to accept the properties while using the same so that we can easily pass the menu items from the application where this package is being consumed. Isn&#8217;t that the way to make a package reusable? Please feel free to play with this GitHub repository. Please do share me your findings while you work on the same. I really appreciate that, thanks in advance.</p>
<h2><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h2>
<p>Thanks a lot for reading. I will come back with another post on the same topic very soon. Did I miss anything that you may think which is needed? Could you find this post as useful? Kindly do not forget to share me your feedback.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/create-your-own-npm-package-private-or-public/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>npm vs npx</title>
		<link>https://mail.sibeeshpassion.com/npm-vs-npx/</link>
					<comments>https://mail.sibeeshpassion.com/npm-vs-npx/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 20 Oct 2018 14:21:08 +0000</pubDate>
				<category><![CDATA[npm]]></category>
		<category><![CDATA[difference between npm and npx]]></category>
		<category><![CDATA[node package manager]]></category>
		<category><![CDATA[NPM]]></category>
		<category><![CDATA[npx]]></category>
		<category><![CDATA[npx and npm]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=13290</guid>

					<description><![CDATA[[toc] Introduction We all are using npm as our package manager, it is easy, right? But with the version npm@5.2.0, when you install the npm, it installs a new package called npx.  Have you ever thought what it is? And why it is needed? Are there any benefits of using npx instead of npm? I know you were trying to find the answer for the above questions and at the end, you landed up on this page. I will try to answer those questions for you. And at the end of the article, I am sure that you will get the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h2>Introduction</h2>
<p>We all are using <a href="https://sibeeshpassion.com/tag/npm/">npm</a> as our package manager, it is easy, right? But with the version npm@5.2.0, when you install the npm, it installs a new package called npx.  Have you ever thought what it is? And why it is needed? Are there any benefits of using npx instead of npm? I know you were trying to find the answer for the above questions and at the end, you landed up on this page. I will try to answer those questions for you. And at the end of the article, I am sure that you will get the answer to your questions. I hope you will find this pose useful.</p>
<h2>Source Code</h2>
<p>The source code can be found <a href="https://github.com/SibeeshVenu/npm-vs-npx">here</a>.</p>
<h2>Background</h2>
<p>I was trying to create a react application and as you know that we can create a react application very easy with the help of the <em>react-app </em>command. But when I have visited the official <a href="https://github.com/facebook/create-react-app#creating-an-app">Github documentation</a> of the create react app, it is been mentioned there that we can either use npx or npm. And I was just wondering what is the difference? And that question changed to this article form.</p>
<h2>What are we going to do</h2>
<p>We will be creating two applications and install the package mocha in both applications so that we can run some tests. If you don&#8217;t know what is mocha, visiting the <a href="https://mochajs.org/">official website</a> wouldn&#8217;t be a bad idea.</p>
<ol>
<li>mocha-example-npm</li>
<li>mocha-example-npx</li>
</ol>
<div id="attachment_13292" style="width: 364px" class="wp-caption aligncenter"><a href="https://sibeeshpassion.com/wp-content/uploads/2018/10/npm-npx.jpg"><img decoding="async" aria-describedby="caption-attachment-13292" class="size-full wp-image-13292" src="https://sibeeshpassion.com/wp-content/uploads/2018/10/npm-npx.jpg" alt="npm-npx" width="354" height="313" srcset="/wp-content/uploads/2018/10/npm-npx.jpg 354w, /wp-content/uploads/2018/10/npm-npx-300x265.jpg 300w" sizes="(max-width: 354px) 100vw, 354px" /></a><p id="caption-attachment-13292" class="wp-caption-text">npm-npx</p></div>
<h3>Coding with mocha-example-npm application</h3>
<p>Let&#8217;s try to create a new folder named mocha-example-npm and open it in my favorite code editor, which is vscode. Once you open the folder, you can install mocha by running this command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">npm install mocha</pre>
<p>Let&#8217;s create a folder called test.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">mkdir test</pre>
<p>Let&#8217;s create a file called test.js.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">code test/test.js</pre>
<p>And paste the below sample test in it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal([1,2,3].indexOf(4), -1);
    });
  });
});</pre>
<p>By seeing the code, you might have already found that the test will pass as the indexOf (4) is going to be -1. Now how do we check this? So to run the test we need to run mocha right? Let&#8217;s do it.</p>
<p>If you just type <em>mocha </em>in the terminal, you will get an error as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">mocha : The term 'mocha' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ mocha
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (mocha:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException</pre>
<p>It is saying that mocha is not a recognized command. To fix this you have below three option.</p>
<h4>1. Install mocha globally, so that the command will be recognized globally</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npm@6.1.0 C:\Program Files (x86)\nodejs\node_modules\npm
PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm&gt; npm i mocha -g
C:\Users\Sibeesh\AppData\Roaming\npm\mocha -&gt; C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\mocha
C:\Users\Sibeesh\AppData\Roaming\npm\_mocha -&gt; C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha
+ mocha@5.2.0
added 24 packages from 436 contributors in 3.458s
PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm&gt; npm list -g mocha
C:\Users\Sibeesh\AppData\Roaming\npm
`-- mocha@5.2.0

PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm&gt; mocha


  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (14ms)

PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm&gt;</pre>
<h4>2. Use the path to mocha from node_modules</h4>
<p><code>./node_modules/mocha/bin/mocha</code></p>
<h4>3. Configure test in your package.json file as follows.</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="json">"scripts": {
    "test": "mocha"
  }</pre>
<p>So that you can run <em>npm test</em> command, and it will take the mocha files from your node_modules folder.</p>
<h3>Coding with mocha-example-npx application</h3>
<p>So, we have understood the problem we have using npm and we have three solutions to fix. Now, let&#8217;s try using the npx instead of npm. Let&#8217;s open the folder and run <em>npx mocha. </em>The result is a warning right as it is trying to find the folder test in the solution and then the tests.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npx: installed 24 in 6.203s
Warning: Could not find any test files matching pattern: test
No test files found</pre>
<p>Let&#8217;s create a test folder, test.js file and write a test in it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; mkdir test


    Directory: F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       20-10-2018  06:26 PM                test


PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; code test/test.js
PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt;</pre>
<p>Let&#8217;s run the command <em>npx mocha</em> now, and you will get the output as below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; npx mocha
npx: installed 24 in 4.142s


  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (12ms)

PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt;</pre>
<p>As you can see that our tests are passed. But are you seeing any node_modules folder or any packages are being added to your solution?</p>
<h3>What is npx</h3>
<p>Now let&#8217;s just summarize what is npx. It is an npm package runner.  We all know that we are installing the packages to our solution from the location called npm registry and use it. And the npx has been designed in a way that we can directly use the package from the npm registry without even installing it in our solution. You may be thinking why it is needed?</p>
<p><strong>Let me give you an example, as I mentioned in the background section of this article, we can create a simple react application with the help of <em>Create-React-App </em>from FaceBook. And this is used only for creating the application, and once we create the application, and we are no longer needed this. If you use npm, you will have to install this package either globally or locally before you use this, or else you will get an error. </strong></p>
<p><strong>Isn&#8217;t it good, if we can just use this create-react-app package from the npm registry and create the application in our local machine? And that&#8217;s where npx stands. With the help of npx, we can reduce the size of our node_modules and I strongly believe that this is a huge benefit. And we can even execute the packages and command simultaneously like below. </strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">npx create-react-app my-app</pre>
<p>It is smart enough to identify whether the package you are trying to get from the npm registry is already there in your machine, either globally or locally. If the package is available in your machine, it will take it from there.</p>
<h3>What if mocha is installed globally or locally and we run the command npx mocha?</h3>
<p>Let&#8217;s try to install mocha globally and run the command npx mocha. Before that, let&#8217;s just make sure that we haven&#8217;t installed mocha yet.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; npm list mocha
mocha-example-npx@1.0.0 F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx
`-- (empty)

PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; npm list -g mocha
C:\Users\Sibeesh\AppData\Roaming\npm
`-- (empty)

PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt;</pre>
<p>Now let&#8217;s just install mocha globally</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; npm i mocha -g
C:\Users\Sibeesh\AppData\Roaming\npm\mocha -&gt; C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\mocha
C:\Users\Sibeesh\AppData\Roaming\npm\_mocha -&gt; C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha
+ mocha@5.2.0
added 24 packages from 436 contributors in 3.033s</pre>
<p>Try to run npx mocha again</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt; npx mocha


  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (12ms)

PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx&gt;</pre>
<p>Did you see any difference, now mocha is been loaded from your global registry.</p>
<h2><span id="conclusion_1"><span id="conclusion">Conclusion</span></span></h2>
<p>Thanks a lot for reading. I will come back with another post on the same topic very soon. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h2><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h2>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/npm-vs-npx/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Using MongoDB on Node JS Application Using Mongoose</title>
		<link>https://mail.sibeeshpassion.com/using-mongodb-on-node-js-application-using-mongoose/</link>
					<comments>https://mail.sibeeshpassion.com/using-mongodb-on-node-js-application-using-mongoose/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sun, 03 Dec 2017 17:15:12 +0000</pubDate>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Node JS]]></category>
		<category><![CDATA[Easy way to insert data to MongoDB]]></category>
		<category><![CDATA[MongoDB with Mongoose]]></category>
		<category><![CDATA[Mongoose]]></category>
		<category><![CDATA[Node JS and MongoDB]]></category>
		<category><![CDATA[NPM]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=12589</guid>

					<description><![CDATA[[toc] Introduction In this post, we are going to see how we can use a MongoDB on our Node JS application with the help of the package Mongoose. We will also be covering some facts about MongoDB so that as a reader, you will understand why we had chosen MongoDB as our backend. We will be going through some steps to install the required packages using a node package manager terminal, so please be with me. At the end of this article, I guarantee that you will be proficient on how to connect a MongoDB in our Node JS application. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>In this post, we are going to see how we can use a <a href="http://sibeeshpassion.com/category/MongoDB">MongoDB</a> on our Node JS application with the help of the package Mongoose. We will also be covering some facts about MongoDB so that as a reader, you will understand why we had chosen MongoDB as our backend. We will be going through some steps to install the required packages using a node package manager terminal, so please be with me. At the end of this article, I guarantee that you will be proficient on how to connect a MongoDB in our Node JS application. I hope you will like this article.</p>
<h3>Why MongoDB?</h3>
<p>Here, I am going to list down the reasons why I had chosen MongoDB.</p>
<ol>
<li>MongoDB is a NoSQL database when I say NoSQL, it means that it doesn&#8217;t have any structure to be followed.</li>
<li>There are no relationships, we can perform the relation using separate packages like Mongoose</li>
<li>Everything is JSON, even the data and collections are stored as JSON</li>
<li>Since the data is stored as JSON, no more conversions are needed. We can directly use it in our application.</li>
</ol>
<h3><span id="source-code">Source Code</span></h3>
<p>You can always clone or download the source code <a href="https://code.msdn.microsoft.com/Node-MongoDB-Mongoose-40fc3ad4">here</a></p>
<h3>Background</h3>
<p>Node JS has become a trend now, thus some most used databases. One of the recommended database for a Node application is MongoDB. As we discussed a MongoDB doesn&#8217;t have any structure inbuilt, here we are going to use a package called Mongoose, with that we can create some structure for our Database. Let&#8217;s just skip the talking and start developing.</p>
<h3>Setting up Node application</h3>
<p>To get started with the Node JS, create a folder and name as per your wish, this is going to be our project directory. Here I am going to use one of my favorite editor, Visual Studio Code. Now please point to your project container, and run the command below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">npm init</pre>
<div id="attachment_12591" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/12/NPM-Init.png"><img decoding="async" aria-describedby="caption-attachment-12591" class="size-full wp-image-12591" src="http://sibeeshpassion.com/wp-content/uploads/2017/12/NPM-Init.png" alt="NPM Init" width="634" height="449" srcset="/wp-content/uploads/2017/12/NPM-Init.png 504w, /wp-content/uploads/2017/12/NPM-Init-300x212.png 300w, /wp-content/uploads/2017/12/NPM-Init-400x283.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12591" class="wp-caption-text">NPM Init</p></div>
<p>This will create a file named &#8220;Package.json&#8221; in your directory. We will be adding all of our dependencies in this file very soon. The command may ask you some default questions which you need to answer. If you need to create the package.json file with default values in it, you may consider running the below command.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">npm init --yes</pre>
<p>Let&#8217;s review our package.json file.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="json">{
  "name": "node-mongodb-mongoose",
  "version": "1.0.0",
  "description": "A Node application with MongoDB and Mongoose",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  },
  "keywords": [
    "Node",
    "Mongoose",
    "MongoDB"
  ],
  "author": "Sibeesh Venu",
  "license": "ISC"
}
</pre>
<h3>Getting the required packages</h3>
<p>Now that we have the application ready, let&#8217;s get the required packages ready.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">npm install --save mongoose</pre>
<p>This will create a new folder npm_modules on your project directory where you can see the package Mongoose in it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">npm install --save express</pre>
<p>The above command will add the package express to our application.</p>
<h3>Creating the Node App</h3>
<p>Now we can create our server.js file where we are going to write most of our application code.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">var express = require("express")
var app = express()
app.listen("3010",()=&gt;{
    console.log("I just started listening!.")
})
</pre>
<p>Now run the command <code class="EnlighterJSRAW" data-enlighter-language="shell">node server.js</code>and make sure that you are getting a console as &#8221; I just started listening!.&#8221;.</p>
<p>If you are getting an error as preceding, please make sure that you are running the application on an unused port.</p>
<blockquote><p>PS F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose&gt; node server.js<br />
events.js:136<br />
throw er; // Unhandled &#8216;error&#8217; event<br />
^</p>
<p>Error: listen EADDRINUSE :::3000<br />
at Object._errnoException (util.js:1031:13)<br />
at _exceptionWithHostPort (util.js:1052:20)<br />
at Server.setupListenHandle [as _listen2] (net.js:1367:14)<br />
at listenInCluster (net.js:1408:12)<br />
at Server.listen (net.js:1496:7)<br />
at Function.listen (F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose\node_modules\express\lib\application.js:618:24)<br />
at Object.&lt;anonymous&gt; (F:\Visual Studio\Node.JS\Node-MongoDB-Mongoose\server.js:3:5)<br />
at Module._compile (module.js:641:30)<br />
at Object.Module._extensions..js (module.js:652:10)<br />
at Module.load (module.js:560:32)&#8221;</p></blockquote>
<h3>Setting up database</h3>
<p>Now that we have our application ready, let&#8217;s go and create our database. I am going to use mLab for creating the database. If you have not installed MongoDB on your machine, I strongly recommend creating a database in <a href="https://mlab.com/home">mLab.</a> I had created a database there and have got my connection string as preceding.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">mongodb://&lt;dbuser&gt;:&lt;dbpassword&gt;@ds038319.mlab.com:38319/mylearning</pre>
<p>We will be updating the connection string with actual DB user and password later. Please make sure that you are creating a user for your DB and remember the password.</p>
<h3>Creating a Mongoose model</h3>
<p>Let&#8217;s create a Mongoose model and set up our connection string now, which is going to be our collection. No worries, it is just a <a href="http://sibeeshpassion.com/category/JavaScript">JavaScript</a> model.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="shell">var conString = "mongodb://admin:admin@ds038319.mlab.com:38319/mylearning"

/**
 * Models 
 */
var User = mongoose.model("User", {
    firstName: String,
    lastName: String
})</pre>
<blockquote><p>Please be noted that, when you move the codes to production, make sure that your password and user fields on the connection string are separated from this file and encrypted</p></blockquote>
<p>Now let&#8217;s connect our database.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">mongoose.connect(conString, { useMongoClient: true }, () =&gt; {
    console.log("DB is connected")
})</pre>
<h3>Setup the data</h3>
<p>We have our model ready, what else is needed now? Yes, you are right, we need data.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">var dummyUser = {
    firstName: "Sibeesh",
    lastName: "Venu"
}</pre>
<h3>Inserting the data into MongoDB</h3>
<p>We have got everything ready, we can insert the model now. Let&#8217;s do the saving logic now.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">mongoose.connect(conString, { useMongoClient: true }, () =&gt; {
    console.log("DB is connected")
    saveData()
})</pre>
<pre class="EnlighterJSRAW" data-enlighter-language="js">function saveData() {
    var user = new User(dummyUser);
    user.save();
}</pre>
<h3>Verify the data</h3>
<p>Once you run your application, go to your database and check for the entries there. I am sure there will be a new collection &#8220;User&#8221; and our inserted data. As I am using mLab database, here is how my data saved.</p>
<div id="attachment_12590" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/12/MongoDB-Data-Insertion.png"><img decoding="async" aria-describedby="caption-attachment-12590" class="size-full wp-image-12590" src="http://sibeeshpassion.com/wp-content/uploads/2017/12/MongoDB-Data-Insertion.png" alt="MongoDB Data Insertion" width="634" height="420" srcset="/wp-content/uploads/2017/12/MongoDB-Data-Insertion.png 539w, /wp-content/uploads/2017/12/MongoDB-Data-Insertion-300x199.png 300w, /wp-content/uploads/2017/12/MongoDB-Data-Insertion-400x265.png 400w" sizes="(max-width: 634px) 100vw, 634px" /></a><p id="caption-attachment-12590" class="wp-caption-text">MongoDB Data Insertion</p></div>
<p>If you have noticed, you can see there are an another property names &#8220;_id&#8221; in our database collection. This is a unique id generated by MongoDB for us, so no need to think about it.</p>
<h3><em>Todo</em></h3>
<p>As the purpose of this article is to show how we can insert a data to MongoDB, I called the save method right after the database is connected. In our real life, this is not a recommended way. You must create an Ajax post request and handle the same. I am leaving that task to you. Please try to do that and let me know. I would love to hear back from you.</p>
<h3><span id="conclusion">Conclusion</span></h3>
<p>Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h3><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h3>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/using-mongodb-on-node-js-application-using-mongoose/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
