<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>From Classic To Modern Web</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/xinjinms/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/xinjinms/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:54:09 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Use local-npm for offline NPM package installation</title>
      <link>https://docs.microsoft.com/archive/blogs/xinjinms/use-local-npm-for-offline-npm-package-installation</link>
      <pubDate>Thu, 25 Aug 2016 07:45:19 GMT</pubDate>
      <dc:creator><![CDATA[Xin Jin - MSFT]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/xinjinms/?p=25</guid>
      <description><![CDATA[npm is a Node.js package manager. Users can fetch and install Node.js packages easily using npm. It...]]></description>
      <content:encoded><![CDATA[npm is a <a href="https://nodejs.org">Node.js</a> package manager. Users can fetch and install <a href="https://nodejs.org">Node.js </a>packages easily using npm. It makes it easier to resolve package dependencies.

The official repository of npm is <a href="https://www.npmjs.com/">https://www.npmjs.com/</a>

&nbsp;
<h3><strong>Why do we need offline NPM package installation?</strong></h3>
<span>There are scenarios where not every user has access to the official npm repository. For example:</span>
<ul>
 	<li>You may deliver an on-site workshop. The trainees need to run <code>npm install</code> for their lab in the restricted environment.</li>
 	<li>You may need to install packages on a flight.</li>
</ul>
&nbsp;
<h3><strong>Why do I choose local-npm?</strong></h3>
<span>Addy Osmani has </span><a href="https://addyosmani.com/blog/using-npm-offline/">a nice post</a><span> comparing </span><a href="https://github.com/nolanlawson/local-npm"><code>local-npm</code></a><span> to other options. I just follow this post.</span>

&nbsp;
<h3><strong>local-npm Installation</strong></h3>
Environment: I am using a Windows 2008 R2 Server with .NET Framework 4.5.2 installed.

&nbsp;

1. Install Node.js

<p>Download and install the current version of Node.js from <a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a></p>
<p>
<img src="https://msdnshared.blob.core.windows.net/media/2016/08/note.png" alt="Note" width="25" height="25" class="alignnone wp-image-35" />Because local-npm depends on <a href="https://github.com/nodejs/node-gyp">node-gyp</a>, you need to install Python 2.7 <span>(</span><code>v3.x.x</code><span> is not supported) </span>and <span>Visual C++ Build Environment. (Refer to <a href="https://github.com/nodejs/node-gyp">the document of node-gyp</a>) Otherwise you will receive the following error when you run the command <code>npm install -g local-npm</code> directly.</span>

<img src="https://msdnshared.blob.core.windows.net/media/2016/08/localnpmerror.png" alt="localnpmerror" width="528" height="260" class="alignnone wp-image-46" />
</p>

2. Install Visual C++ Build Environment
<ul>
 	<li>Option 1: <span>Install </span><a href="https://landinghub.visualstudio.com/visual-cpp-build-tools">Visual C++ Build Tools</a><span> using the </span><strong>Default Install</strong><span> option.</span></li>
 	<li><span>Option 2: Install </span><a href="https://www.visualstudio.com/products/visual-studio-community-vs">Visual Studio 2015</a><span> (or modify an existing installation) and select </span><em>Common Tools for Visual C++</em><span> during setup.</span></li>
</ul>
3. Install Python 2.7

Download and install Python 2.7 from <a href="https://www.python.org/downloads/">https://www.python.org/downloads/</a> (I installed it in the default folder <code>c:\python27</code>)

<img src="https://msdnshared.blob.core.windows.net/media/2016/08/note.png" alt="Note" width="25" height="25" class="alignnone wp-image-35" />You may meet the following error during Python 2.7 installation:

<code>"There is a problem with this Windows Installer Package. A program required for this install to complete could not be run. Contact your support personnel or package vendor"</code>

<img src="https://msdnshared.blob.core.windows.net/media/2016/08/pythoninstallerror.png" alt="pythoninstallerror" width="504" height="178" class="alignnone wp-image-36" />

I applied the solution in <a href="https://stackoverflow.com/questions/23349957/solving-install-issues-with-python-3-4-on-windows">this Stack Overflow post</a>.
<ul>
 	<li>De-select pip from the install options. Then install.</li>
 	<li>After the installation completes, re-run the setup.</li>
 	<li>Choose "<code>change python</code>" and then install pip.</li>
</ul>
4. Launch command prompt, run the command <code>npm config set python C:\Python27\python.exe</code>

5. Run the command <code>npm config set msvs_version 2015</code>

6. Simply run the command <code>npm install -g local-npm</code>
<h3><strong>Basic Usage of local-npm</strong></h3>
<img src="https://msdnshared.blob.core.windows.net/media/2016/08/note.png" alt="Note" width="25" height="25" class="alignnone wp-image-35" />Before you run the command <code>local-npm</code> to start local-npm, you'd better create a folder (e.g. <code>c:\local-npm</code>) and change the current directory to this folder. The reason to create a specific folder is because local-npm will replicate the skimdb from the official repository and store the data in local folder. The default folder to store the data is the folder where you run local-npm. (You can specific the folder by the command line option <code>-d, --directory</code>)

Run the command <code>local-npm</code>

Then the users can set npm to get registry through the local server:

<code>npm set registry https://127.0.0.1:5080</code>

If you want to switch back to the official repository, run this:

<code>npm set registry https://registry.npmjs.org</code>

Now the users can run <code>npm install</code> to get the package through the local-npm server.
<h3><strong>Offline mode</strong></h3>
After you start local-npm, it will begin to replicate skimdb from remote official registry.

<img src="https://msdnshared.blob.core.windows.net/media/2016/08/localnpmreplicate.png" alt="localnpmreplicate" width="446" height="290" class="alignnone wp-image-56" />

There's no need to wait for the complete skimdb download (which can take a few hours). You can test it with the following steps:
<ol>
 	<li>Make sure the network is up.</li>
 	<li><span>Run an </span><code>npm install</code><span> to grab a module. (e.g. <code>npm install jquery</code>)</span></li>
 	<li>local-npm will cache it.</li>
 	<li>delete jquery from node_modules directory.</li>
 	<li>Turn off the network.</li>
 	<li>Run that npm install again. (e.g. <code>npm install jquery</code>)</li>
 	<li>You will notice the following behaviors
<ul>
 	<li>If the complete skimdb has been replicated (100%), you will get the cached package immediately from local-npm.</li>
 	<li>If the replication is still in process, local-npm will try to connect to the official registry three times. After about 1 minutes, you will still get the cached package from local-npm.</li>
</ul>
</li>
</ol>
&nbsp;

&nbsp;
]]></content:encoded>
    </item>
  </channel>
</rss>