<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ShreeJ</title>
    <description>The latest articles on DEV Community by ShreeJ (@shree_j).</description>
    <link>https://dev.to/shree_j</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F468421%2Fb119d56f-0759-4240-b7d3-ca80c574195c.jpg</url>
      <title>DEV Community: ShreeJ</title>
      <link>https://dev.to/shree_j</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shree_j"/>
    <language>en</language>
    <item>
      <title>How npm install Works Internally?
</title>
      <dc:creator>ShreeJ</dc:creator>
      <pubDate>Sun, 20 Sep 2020 14:40:34 +0000</pubDate>
      <link>https://dev.to/shree_j/how-npm-works-internally-4012</link>
      <guid>https://dev.to/shree_j/how-npm-works-internally-4012</guid>
      <description>&lt;p&gt;Most of the happening frameworks like Node.js, react.js, vue, angular, etc are built with npm as the back-bone. The npm-registry maintains the libraries or dependencies used in various frameworks.&lt;/p&gt;

&lt;p&gt;This post will help in understanding the below :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the logic behind what happens when we execute &lt;code&gt;npm install&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;the order of dependency downloaded and the &lt;code&gt;node_modules&lt;/code&gt; folder structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Basic knowledge in any JS frameworks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Any one of the following&lt;/strong&gt; installed to try on the samples below.

&lt;ul&gt;
&lt;li&gt;node and npm &lt;/li&gt;
&lt;li&gt;nvm (node-version-manager to manage different versions of node and npm in machine)&lt;/li&gt;
&lt;li&gt;docker-compose (to play with node app in a container)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What happens when we execute &lt;code&gt;npm install&lt;/code&gt; ?
&lt;/h2&gt;

&lt;p&gt;We all know that the command &lt;code&gt;npm install&lt;/code&gt; will download the dependency module from the npm-registry.&lt;br&gt;
This can be by any one of the following way.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;npm install&lt;/code&gt; - to fetch all dependencies mentioned in the dependency tree.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;dependency_name&amp;gt;&lt;/code&gt; or &lt;code&gt;npm install &amp;lt;dependency_name&amp;gt;@&amp;lt;version&amp;gt;&lt;/code&gt; - to fetch a particular dependency by name and version (if no version is specified, then it pulls the latest version).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;git remote url&amp;gt;&lt;/code&gt; - to fetch a library pushed to github or bitbucket or gitlab.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Algorithm that makes the work of &lt;code&gt;npm install&lt;/code&gt; easy :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Check if &lt;code&gt;node_modules&lt;/code&gt; folder exist or &lt;code&gt;package-lock.json&lt;/code&gt; and trace the existing the dependency tree (folder structure) in it and clone the tree (or create a empty tree).&lt;/li&gt;
&lt;li&gt;Fetch the relevant dependencies (dev, prod or direct dependencies) from the &lt;code&gt;package.json&lt;/code&gt; and add it to the clone (from step-1).

&lt;ul&gt;
&lt;li&gt;finds the difference between the trees and adds the missing dependencies.&lt;/li&gt;
&lt;li&gt;dependencies will be added as close to the top of the tree as possible.&lt;/li&gt;
&lt;li&gt;the dependencies are included without disturbing the other roots/branches of the tree.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Compare the original tree (from step-2) with the cloned tree (step-1) and make a list of actions to take to get the new tree replicated in the node_modules.

&lt;ul&gt;
&lt;li&gt;the actions are install(new dependencies), update(existing dependency versions), move(change the placee off the dependency within the tree) and remove(uninstall libraries that are not needed by new tree).&lt;/li&gt;
&lt;li&gt;execute all the actions identified (deepest first).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Folder-Structure in &lt;code&gt;node_modules&lt;/code&gt; :
&lt;/h2&gt;

&lt;p&gt;The folder structure that the npm follows varies according to the scenarios stated like below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No existing node_modules or package-lock.json or dependencies in package.json.&lt;/li&gt;
&lt;li&gt;No existing node_modules or package-lock.json, but package.json with dependency list is available.&lt;/li&gt;
&lt;li&gt;No existing node_modules, but package-lock.json and package.json with dependency list are available.&lt;/li&gt;
&lt;li&gt;The node_modules, package-lock.json and package.json with dependency list are all available.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. No existing node_modules or package-lock.json or dependencies in package.json:&lt;/strong&gt; &lt;br&gt;
This simple case is when any JS framework applications starts initially without any dependency and adds them one by one.&lt;br&gt;
In this scenario, the dependencies are downloaded in the order of installation like below: &lt;br&gt;
&lt;em&gt;Example&lt;/em&gt;: execute &lt;code&gt;npm install &amp;lt;B&amp;gt;&lt;/code&gt; in a new application.&lt;br&gt;
Here &lt;code&gt;B&lt;/code&gt; is a dependency and assume it has internal dependency on &lt;code&gt;alpha@v2.0&lt;/code&gt;, then both of them gets installed at the root level of the &lt;code&gt;node_modules&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Inference&lt;/em&gt;: All the dependencies and the internal dependencies tries to get a place in the root of the node_modules unless there is a conflict with the same dependency, but different version.&lt;/p&gt;

&lt;p&gt;node_modules&lt;br&gt;
|_ B&lt;br&gt;
|_ alpha @v2.0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. No existing node_modules or package-lock.json, but package.json with dependency list is available:&lt;/strong&gt; &lt;br&gt;&lt;br&gt;
In this scenario, an apllication has dependencies listed in package.json without lock-file. &lt;br&gt;&lt;br&gt;
&lt;em&gt;Example&lt;/em&gt;: execute &lt;code&gt;npm install&lt;/code&gt; in the application directory which has a package.json with dependencies like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.0.0"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;A&lt;/code&gt; internally depends on &lt;code&gt;alpha@v1.0&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; depends on &lt;code&gt;alpha@v2.0&lt;/code&gt;. &lt;br&gt;
&lt;em&gt;Inference&lt;/em&gt;: All the dependencies and the internal dependencies tries to get a place in the root of the node_modules unless there is a conflict with the same dependency, but different version. When a  conflict raises, it creates a sub node_modules under each dependency needed and pushes conflicting internal libraries in it.&lt;/p&gt;

&lt;p&gt;node_modules&lt;br&gt;
|_ A&lt;br&gt;
|_ alpha @v1.0&lt;br&gt;
|_ B&lt;br&gt;
    |_ node_modules&lt;br&gt;
        |_ alpha @v2.0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. No existing node_modules, but package-lock.json and package.json with dependency list are available:&lt;/strong&gt;&lt;br&gt;
Assume, &lt;code&gt;A&lt;/code&gt; internally depends on &lt;code&gt;alpha@v1.0&lt;/code&gt; whereas, &lt;code&gt;B&lt;/code&gt; depends on &lt;code&gt;alpha@v2.0&lt;/code&gt; and &lt;code&gt;beta@v3.0&lt;/code&gt;.&lt;br&gt;
package-lock.json snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"resolved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NPM REGISTRY URL of A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"requires"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"alpha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"alpha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"resolved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NPM REGISTRY URL of alpha v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"resolved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NPM REGISTRY URL of B"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"requires"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"alpha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"beta"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"3.0.0"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"alpha"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"resolved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NPM REGISTRY URL of alpha v2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"beta"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"3.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"resolved"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"NPM REGISTRY URL of beta v3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Inference:&lt;/em&gt; Irrespective of the dependency ordered in package.json, the packages will be installed in the tree structure defined by the package-lock.json. &lt;/p&gt;

&lt;p&gt;And the resulting dependency tree structure would be :&lt;/p&gt;

&lt;p&gt;node_modules&lt;br&gt;
|_ A&lt;br&gt;
|_ alpha @v1.0&lt;br&gt;
|_ B&lt;br&gt;
|    |_ node_modules&lt;br&gt;
|        |_ alpha @v2.0&lt;br&gt;
|_ beta @v3.0&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. The node_modules, package-lock.json and package.json are all available :&lt;/strong&gt;&lt;br&gt;
The node_modules folder will be re-arranged to match the incoming new tree from package-lock.json and installed in the order as defined in the package-lock.json file.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Package.json&lt;/code&gt; (vs) &lt;code&gt;Package-lock.json&lt;/code&gt; :
&lt;/h2&gt;

&lt;p&gt;Lets consider the following sequences of dependency installation in a new application without an existing dependency tree or node_modules in it.&lt;br&gt;
&lt;strong&gt;Example :&lt;/strong&gt;&lt;br&gt;
Assume, &lt;code&gt;A&lt;/code&gt; internally depends on &lt;code&gt;alpha@v1.0&lt;/code&gt; whereas, &lt;code&gt;B&lt;/code&gt; depends on &lt;code&gt;alpha@v2.0&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
    &lt;tr&gt;
        &lt;th&gt;
            npm
        &lt;/th&gt;
        &lt;th&gt;
            Scenario-1
        &lt;/th&gt;
        &lt;th&gt;
            Scenario-2
        &lt;/th&gt;
    &lt;/tr&gt;

&lt;tr&gt;
        &lt;td&gt;
            &lt;b&gt; Commands &lt;/b&gt;
        &lt;/td&gt;
        &lt;td&gt;

npm install A &lt;br&gt;
npm install B
        &lt;/td&gt;
        &lt;td&gt;

npm install B &lt;br&gt;
npm install A
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;
            &lt;b&gt; package.json &lt;/b&gt;
        &lt;/td&gt;
        &lt;td&gt;

&lt;pre&gt;
{
  "dependencies": {
    "A": "1.0.0",
    "B": "2.0.0"
  }
}
&lt;/pre&gt;

  &lt;/td&gt;
  &lt;td&gt;

&lt;pre&gt;
{
  "dependencies": {
    "A": "1.0.0",
    "B": "2.0.0"
  }
}
&lt;/pre&gt;
  &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
        &lt;td&gt;
            &lt;b&gt; package-lock.json &lt;/b&gt;
        &lt;/td&gt;
        &lt;td&gt;

&lt;pre&gt;
{
  "dependencies": {
    "A": {
      "version": "1.0.0",
      "requires": {
        "alpha": "1.0.0",
      }
    },
    "alpha": {
      "version": "1.0.0",
    },
    "B": {
      "version": "2.0.0",
      "requires": {
        "alpha": "2.0.0",
      },
      "dependencies": {
        "alpha": {
          "version": "2.0.0",
        }
      }
    }
  }
}
&lt;/pre&gt;

  &lt;/td&gt;
  &lt;td&gt;

&lt;pre&gt;
{
  "dependencies": {
    "A": {
      "version": "1.0.0",
      "requires": {
        "alpha": "1.0.0",
      },
      "dependencies": {
        "alpha": {
          "version": "1.0.0",
        }
      }
    },
    "alpha": {
      "version": "2.0.0",
    },
    "B": {
      "version": "2.0.0",
      "requires": {
        "alpha": "2.0.0",
      }
    }
  }
}
&lt;/pre&gt;
  &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
  &lt;td&gt;
      &lt;b&gt; node_modules &lt;/b&gt;
  &lt;/td&gt;
  &lt;td&gt;

node_modules&lt;br&gt;
|_ A&lt;br&gt;
|_ alpha @v1.0&lt;br&gt;
|_ B&lt;br&gt;
|    |_ node_modules&lt;br&gt;
|        |_ alpha @v2.0&lt;br&gt;

  &lt;/td&gt;
  &lt;td&gt;

node_modules&lt;br&gt;
|_ A&lt;br&gt;
|    |_ node_modules&lt;br&gt;
|        |_ alpha @v1.0&lt;br&gt;
|_ alpha @v2.0&lt;br&gt;
|_ B&lt;br&gt;
    
  &lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The above comparison helps in concluding the importance of package-lock.json. &lt;br&gt;
If the package 'alpha' is imported from the JS application like &lt;code&gt;var alpha = require('alpha');&lt;/code&gt;, the scenario-1 points to v1 whereas, scenario-2 imports v2.&lt;br&gt;
Thus, the behaviour of the code snippets depending on the imported file might differ.&lt;/p&gt;

&lt;p&gt;It is not the package.json that determines the tree structure(because the npm install downloads dependencies in the alphabetical order as saved in package.json).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; The &lt;strong&gt;best practise is to push&lt;/strong&gt; and maintain &lt;strong&gt;the package-lock.json into the source-code&lt;/strong&gt; (like git), to ensure the same dependency tree is being used by all members using the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  References :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;npm install basics- &lt;a href="https://docs.npmjs.com/cli/install"&gt;https://docs.npmjs.com/cli/install&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;npm folder basics - &lt;a href="https://docs.npmjs.com/configuring-npm/folders.html"&gt;https://docs.npmjs.com/configuring-npm/folders.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;package.json basics - &lt;a href="https://docs.npmjs.com/files/package.json"&gt;https://docs.npmjs.com/files/package.json&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;package-lock.json basics - &lt;a href="https://docs.npmjs.com/configuring-npm/package-lock-json.html"&gt;https://docs.npmjs.com/configuring-npm/package-lock-json.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>npm</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How To Install and Run PostgreSQL using Docker ?</title>
      <dc:creator>ShreeJ</dc:creator>
      <pubDate>Mon, 14 Sep 2020 06:33:21 +0000</pubDate>
      <link>https://dev.to/shree_j/how-to-install-and-run-psql-using-docker-41j2</link>
      <guid>https://dev.to/shree_j/how-to-install-and-run-psql-using-docker-41j2</guid>
      <description>&lt;p&gt;Installing, running and managing postgres in local-machine for development is not difficult anymore. Here is a simple way to get all at one place easily installed and configured within seconds with the help of docker.&lt;/p&gt;

&lt;p&gt;Now skip all complex steps in installing and configuring PSQL to get started with local development and GUI to manage the DB. It's now easy to kick-start development of postgres based applications in a few seconds. &lt;br&gt;&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: This is to make the development process easy and however follow the &lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;conventional method of installing PSQL&lt;/a&gt; individually for production environment.&lt;/p&gt;

&lt;p&gt;After following the instructions below, you will get the following installed in your machine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Postgres server running as a docker-container (which can be accessed by CLI, GUI or other application for development).&lt;/li&gt;
&lt;li&gt;Postgres container accessible through CLI.&lt;/li&gt;
&lt;li&gt;PgAdmin4 browser version to access Postgres server from GUI.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;docker&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;… nothing else  :)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Install and Configure PSQL using Docker:
&lt;/h2&gt;

&lt;p&gt;Run the below command in linux or windows or mac machine from the terminal or command-prompt to pull PSQL from docker-hub. &lt;br&gt;&lt;br&gt;
&lt;code&gt;docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=somePassword -d postgres&lt;/code&gt; &lt;br&gt;&lt;br&gt;
In the above command replace : &lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optional - &lt;em&gt;postgresql-container&lt;/em&gt; with a preferable container name if necessary.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;somePassword&lt;/em&gt; with a password to authenticate and connect to the postgres (in application with connection string as well as the PG-admin viewer).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Verify a new container created and running at 0.0.0.0:5432 with the below command.&lt;br&gt;
&lt;code&gt;docker ps -a&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;The PostgresQL is ready to connect and use. &lt;br&gt;&lt;br&gt;
&lt;strong&gt;The postgres server is now running in the IP of your local machine in 5432.&lt;/strong&gt; &lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Install PG-admin using Docker:
&lt;/h2&gt;

&lt;p&gt;Download the pgAdmin-4 browser version from docker-hub using the following command. &lt;br&gt;&lt;br&gt;
&lt;code&gt;docker run --rm -p 5050:5050 thajeztah/pgadmin4&lt;/code&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;Now manage your postgres from the browser by launching &lt;a href="http://localhost:5050" rel="noopener noreferrer"&gt;http://localhost:5050&lt;/a&gt; . &lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fphdp5tcq1133yrroub35.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fphdp5tcq1133yrroub35.jpg" alt="pg_admin_install"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  To connect the PSQL server in pgAdmin:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fve6sxo3tmrt8vmka5zj8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fve6sxo3tmrt8vmka5zj8.jpg" alt="pg_admin_1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Enter the credentials to save and manage PSQL via GUI.&lt;br&gt;
&lt;strong&gt;Host&lt;/strong&gt;  - The IP address of your machine&lt;br&gt;
&lt;strong&gt;Password&lt;/strong&gt; - Password used while creating the PSQL server with docker &lt;br&gt;&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Connecting to the PSQL server via CLI :
&lt;/h2&gt;

&lt;p&gt;The steps below are to connect to the psql server from CLI : &lt;br&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the docker-container-id in which the postgres is running using the below command.
&lt;code&gt;docker ps -a&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Run the below command to enter into the container (with the ID from step-1).
&lt;code&gt;docker exec -it &amp;lt;PSQL-Container-ID&amp;gt; bash&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Authenticate to start using as postgres user.
&lt;code&gt;psql -h localhost -p 5432 -U postgres -W&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Enter the password used while creating the PSQL server container.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;h2&gt;
  
  
  Connecting to the PSQL server via application :
&lt;/h2&gt;

&lt;p&gt;(example: JavaScript) &lt;/p&gt;

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

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;connectionString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;postgresql://postgres:test1234@192.168.225.86:5432/postgres&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Connect to Postgres ...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rej&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Select now() as run_at;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Run at date-time : &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;run_at&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="nf"&gt;resol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;run_at&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Execution Completed ...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error while Connecting DB !&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;connectDB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


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

&lt;/div&gt;

</description>
      <category>docker</category>
      <category>postgres</category>
      <category>psql</category>
      <category>pgadmin</category>
    </item>
  </channel>
</rss>
