NPM:仅安装缺失 - 如何加速 npm 安装

NPM: Only install missing - how to speed up npm install

我的 npm 脚本中有很多开发依赖项。 npm install第一次需要几分钟,没关系。

但由于我正在与 TFS 构建服务器集成,它只需要 npm install 一次。在那之后, npm install 只是在浪费时间,因为它需要 2-3 分钟才能确定软件包已经安装。此外,它似乎总是重新安装带有 -g 全局标志的包,即使存在。

如何让它检查软件包是否存在,如果存在,跳过 npm install?

您可以使用 npm-cache as an alternative way if you use on-premise build agents 进行构建。

It is useful for build processes that run [npm|bower|composer|jspm] install every time as part of their build process. Since dependencies don't change often, this often means slower build times. npm-cache helps alleviate this problem by caching previously installed dependencies on the build machine. npm-cache can be a drop-in replacement for any build script that runs [npm|bower|composer|jspm] install.

How it Works

When you run npm-cache install [npm|bower|jspm|composer], it first looks for package.json, bower.json, or composer.json in the current working directory depending on which dependency manager is requested. It then calculates the MD5 hash of the configuration file and looks for a filed named .tar.gz in the cache directory ($HOME/.package_cache by default). If the file does not exist, npm-cache uses the system's installed dependency manager to install the dependencies. Once the dependencies are installed, npm-cache tars the newly downloaded dependencies and stores them in the cache directory. The next time npm-cache runs and sees the same config file, it will find the tarball in the cache directory and untar the dependencies in the current working directory.

你也可以试试npm-install-missing

但是,如果您使用的是 VSTS Hosted Build Agent,则无法执行此操作,因为每次使用 Hosted Build Agent 对构建进行排队时,都会为该构建分配一个干净的构建代理。这意味着代理上没有安装依赖包。您需要执行完整的 npm 安装。