How to install npm package while offline?

2019-02-03 02:42发布

问题:

Im working on offline network and want to install angular-cli using npm. I have a zip file of angular-cli and using the latest node and npm version. Im using the command: npm install ./angular-cli-master to install angular-cli from the folder. But i keep getting this error telling me I dont have an internet connection (which is ok). So how can I install this angular-cli while offline using the zip I downloaded from github?

Thanks for your help

回答1:

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.



回答2:

as of 2016, there are a couple of softwares available.

https://addyosmani.com/blog/using-npm-offline/

as of now (May 2017) with the advent of Npm 5 you can use the flag: --prefer-offline.

yarn does this out of the box.



回答3:

the short answer, you can't. Most NPM packages such as @angular/cli need other dependencies and those need child dependencies which get installed when you run npm install

You can, however, install the cli when on the network and use it when offline.



回答4:

You can find the npm install command documentation here: https://docs.npmjs.com/cli/install

I am not quite sure and unfortunately, I do not have the chance to test it myself right now, but I would try to either unzip the folder and remove the dot, like that: npm install /angular-cli-master (= installing a folder not a zip file) or just add the zip file ending like that: npm install ./angular-cli-master.tgz
(= installing a zip-file not a folder, file ending may be .zip or something else, though)