node.js 如何在离线时安装 npm 包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43064107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:07:24  来源:igfitidea点击:

How to install npm package while offline?

node.jsnpmvisual-studio-codeangular-cli

提问by Dorni

I'm working on an 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. I'm using the command: npm install ./angular-cli-masterto install angular-cli from the folder. But I keep getting this error telling me I don't have an internet connection (which is ok). So how can I install this angular-cli while offline using the zip I downloaded from Github?

我在离线网络上工作,想使用 npm 安装 angular-cli。我有一个 angular-cli 的 zip 文件并使用最新的 node 和 npm 版本。我正在使用命令:npm install ./angular-cli-master从文件夹安装 angular-cli。但是我不断收到这个错误,告诉我我没有互联网连接(没关系)。那么如何使用我从 Github 下载的 zip 离线安装这个 angular-cli?

Thanks for your help.

谢谢你的帮助。

采纳答案by DrakaSAN

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

您只需将包和所有依赖项复制到您的node_modules文件夹中,在项目中进行本地安装,或在全局文件夹中(npm config get prefix以查看它所在的位置)中进行全局安装。

The behavior of npm installis 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.

的行为npm install是检查依赖项,并首先安装它们。当它没有找到安装它们,也没有找到包含它们的本地文件时,它会尝试下载它们。

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.jsonof 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.

您可以在package.json每个模块的 的 中找到依赖项列表,但由于它是递归的,如果您手动完成所有设置可能需要很长时间,npm 通过递归来完成。

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.

对您来说,最简单的方法是在连接的 PC 上创建一个新文件夹,然后在其中npm install angular-cli压缩文件夹并将其传输到离线机器上。

回答by user2167582

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

20161 月- 查看 Addy Osmani 关于离线安装 npm 包的建议

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

20175 月-从 npm 5 开始,您可以将--prefer-offline标志传递给npm install

yarndoes this out of the box.

yarn这是开箱即用的。

回答by Ahmed Musallam

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

简短的回答,你不能。大多数 NPM 包,例如@angular/cli需要其他依赖项和那些需要在运行时安装的子依赖项npm install

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

但是,您可以在联网时安装 cli,并在离线时使用它。

回答by Oliver

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

在 2019 年,我发现没有推荐的方法适用于无法访问互联网的“气隙”服务器。

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

我发现唯一的解决方案是在 Windows 上使用 artillery.io 作为示例:

  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.cmdto the root of the servers npm folder (C:\Users\{serverusername}\npm)
  6. Extract the zip to C:\Users\{serverusername}\npm\node_modules
  1. 将软件包安装在可以访问互联网的机器上,例如本地开发机器。 npm install -g artillery
  2. 浏览到 C:\Users\{username}\npm
  3. 拉上拉链\node_modules\artillery(例如 artillery.7z)
  4. 将 zip 和文件artilleryartillery.cmd(在 npm 文件夹的根目录下)复制到服务器
  5. 粘贴两个artilleryartillery.cmd至服务器NPM夹的根目录(C:\Users\{serverusername}\npm
  6. 将 zip 解压缩到 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 npmfolder and copy + extract it on the server.

这是只有一种工具的复杂版本。如果您本地机器的 npm 文件夹在工具上相对较少,您总是可以压缩整个npm文件夹并在服务器上复制+解压缩它。

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

我仍然认为 npm 坚持尝试连接到注册表即使在使用npm packnpm install -g <tarfile>

回答by Zahid Khan

Problem: I'd been in similar situation where I can't install the express.jsand all other dependencies specifies by package.jsonon my local machine (offline) using npmdue to unavailability of internet connectivity.

问题:我遇到过类似的情况,由于 Internet 连接不可用,我无法使用npm在我的本地机器(离线)上安装express.js和所有其他依赖项指定的。package.json

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

解决方案:我是在工作的解决方案的Windows,通过它我安装了(所以千万不要其他平台)express与所有我需要为我的项目的依赖关系,其中包括框架cookie-parserjademorgan等。



Steps :

脚步 :

  1. Install all the package(s) on a remote machine which has an internet access.
    In my case I'm using Windowson both remote as well as local machines and my requirement was of installation of express.json 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.jsI 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 Windowsits

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

  5. And place both copied folders viz. npmand npm-cacheinto your local machine on same location thats

    C:\Users\{Username}\AppData\Roaming\
    
  1. 在可以访问 Internet 的远程计算机上安装所有软件包。
    就我而言,我在远程和本地机器上都使用Windows,我的要求是express.js在本地机器上安装。所以我在我的远程机器上运行下面的命令来安装express.js

    C:\Users>npm install -g express-generator`
    
  2. 安装后,express.js我使用以下命令在远程计算机上创建了一个应用程序:

    C:\Users\Name\Desktop>express Project`
    
    C:\Users\Name\Desktop\Project>npm install -g =>to install all other dependencies globally*
    
  3. 现在浏览到npm存储全局模块的位置,您可以通过以下方式查看位置

    C:\Users>npm config get prefix
    

    通常在Windows 中它的

    C:\Users\{Username}\AppData\Roaming\
    
  4. 简单地复制npmnpm-cache远程计算机的文件夹中。

  5. 并放置两个复制的文件夹即。npmnpm-cache进入您位于同一位置的本地机器

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

回答by Andre

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

您可以在此处找到 npm install 命令文档: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)

我不太确定,不幸的是,我现在没有机会自己测试它,但我会尝试解压缩文件夹并删除点,就像这样:npm install /angular-cli-master(=安装一个文件夹不是一个 zip 文件)或只是添加这样的 zip 文件结尾: npm install ./angular-cli-master.tgz
(= 安装一个 zip 文件而不是一个文件夹,文件结尾可能是 .zip 或其他东西,虽然)