Installing Node.js packages in a production environment without an internet connection

Marcus Amaral Rolim
2 min readJan 19, 2021
Photo by Max Chen on Unsplash

What looks like a simple task could be a little tricky when you don’t have an internet connection. It happens because every time you install a node package, it gets the package specifically for your Operational System and architecture.

The solution here is to have a similar computer with internet, install everything you need there, like your project dependencies, pm2 (and pm2-windows-startup if you are going to install on a windows machine), and use cache to store what npm got from the internet.

Here is a step-by-step on how to accomplish this:

1. On the virtual machine server (with internet)

1.1. Create an environment (with internet) very similar to the place you are going to install

You can use a virtual machine for this step, and make sure you are using the same operational system, and the same architecture (x86 or x64) also, if it’s an old version of windows, make sure that you are using the same service pack. Make sure that Node.js is also on the same version.

1.2. Clear the npm cache to make sure that are not storing unnecessary files

npm clean cache --force

1.3. Install everything that you normally need

Example: if you want to install pm2

npm install -g pm2

And install your dependencies normally

npm install

1.4. Now you need to get the cache

Npm stores everything on cache automatically, ~/.npm on Unix and %AppData%/npm-cache on Windows

So, you need to copy the entire folder to your flash drive (or the tool you are using to install the system).

2. On the server (without internet)

2.1. Install the node packages from the cache

After installing node.js on the system, you’ll have to point to the place where your cache folder is located and install the packages:

npm install --global --cache <cache-folder> --optional --cache-min Infinity --shrinkwrap false pm2

That is it! You can use the installed packages now.

Reference:

https://docs.npmjs.com/cli/v6/commands/npm-cache

--

--