How to install npm package while offline?

Jan 2016 - check out Addy Osmani's recommendations for offline installation of npm packages

May 2017 - as of npm 5, you can pass the --prefer-offline flag to npm install

yarn does this out of the box.


You simply copy the package and all dependencies in your node_modules folder, inside the project for local installation, or in the global folder (npm config get prefix to see where it is located) for a global installation.

The behavior of npm install is to check for the dependencies, and install them first. When it doesn't find them installed, nor the local file containing them, it tries to download them.

Since all of those steps fail (you don't have the dependency installed, it isn't available on the expected location, and it can't download it), the installation fails.

You can find the dependency list in the package.json of each module, but since it is recursive, it can take a long time to have everything set right if you do it manually, npm does it by recursion.

For you, the easiest way would be to create a new folder on the connected PC, and inside it npm install angular-cli, zip the folder and transfer it on the offline machine.


Problem: I'd been in similar situation where I can't install the express.js and all other dependencies specifies by package.json on my local machine (offline) using npm due to unavailability of internet connectivity.

Solution: I've a solution that works on Windows(not so sure of other platforms) through which I installed express framework with all the dependencies I required for my project which include cookie-parser, jade, morgan etc.


Steps :

  1. Install all the package(s) on a remote machine which has an internet access.
    In my case I'm using Windows on both remote as well as local machines and my requirement was of installation of express.js on local machine . So I run below command on my remote machine to install express.js

    C:\Users>npm install -g express-generator`
    
  2. After installation of express.js I created an app on my remote machine using:

    C:\Users\Name\Desktop>express Project`
    
    C:\Users\Name\Desktop\Project>npm install -g =>to install all other dependencies globally*
    
  3. Now browse to location where npm's global modules are stored, you can view the location by

    C:\Users>npm config get prefix
    

    Generally in Windows its

    C:\Users\{Username}\AppData\Roaming\
    
  4. Simply copy the npm and npm-cache folder of your remote machine.

  5. And place both copied folders viz. npm and npm-cache into your local machine on same location thats

    C:\Users\{Username}\AppData\Roaming\
    

In 2019, I found none recommended approaches were applicable to an "air gapped" server with no internet access.

I found the only solution was to, on windows, using artillery.io as an example:

  1. install the package on a machine with internet access, e.g local dev machine. npm install -g artillery
  2. Browse to C:\Users\{username}\npm
  3. zip up the \node_modules\artillery (e.g artillery.7z)
  4. Copy the zip and the files artillery, artillery.cmd (at root of npm folder) to the server
  5. Paste the two artillery, artillery.cmd to the root of the servers npm folder (C:\Users\{serverusername}\npm)
  6. Extract the zip to C:\Users\{serverusername}\npm\node_modules

This is the complicated version for just one tool. If your local machine's npm folder is relatively light on tools, you could always just zip the whole npm folder and copy + extract it on the server.

I still think it's odd that npm insists on trying to connect to the registry even when using npm pack and npm install -g <tarfile>