node.js 加速 npm 安装
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22077725/
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
Speeding up the npm install
提问by Prashanth
I am trying to speed up the npm install during the build process phase. My package.json has the list of packages pretty much with locked revisions in it. I've also configured the cache directory using the command
我正在尝试在构建过程阶段加快 npm install 的速度。我的 package.json 有很多包的列表,里面有锁定的修订版。我还使用命令配置了缓存目录
npm config set cache /var/tmp/npm-cache --global
However, on trying to install using npm install -g --cache, I find that this step isn't reducing the time to install by just loading the packages from cache as I would expect. In fact, I doubt if it's even using the local cache to look up packages first.
但是,在尝试使用 安装时npm install -g --cache,我发现这一步并没有像我期望的那样通过从缓存中加载包来减少安装时间。事实上,我怀疑它是否首先使用本地缓存来查找包。
回答by Anurag Jain
As suggested by @Daniel Serodio
正如@Daniel Serodio 所建议的
You could also include your node_modulesfolder inside your repository but you should probably zipit first than add to repo, and while installing you can unzipit and just
您还可以将node_modules文件夹包含在您的存储库中,但您可能应该先将其压缩而不是添加到repo 中,并且在安装时您可以解压缩它,然后
npm rebuild
(which works cross platform) it is quite fast.
(跨平台工作)它非常快。
This would also give you the benefit of full control over all your dependencies.
这也将使您可以完全控制所有依赖项。
Also you can set the process flag to falseto increase your speed by 2x.
您也可以将进程标志设置为false以将速度提高 2 倍。
npm set progress=false
Update:You can also use pnpm for this
更新:您也可以为此使用 pnpm
npm i -g pnpm
This basically use local cached modules (i have heard its better then YARN)
这基本上使用本地缓存模块(我听说它比 YARN 更好)
回答by kano
Proposing two more modern approches:
提出两种更现代的方法:
1) npm ci
1) npm ci
Use npm ci, which is available from npm version 5.7.0(although I recommend 5.7.1and upwards because of the broken release) - this requires package-lock.jsonto be present and it skips building your dependency tree off of your package.jsonfile, respecting the already resolved dependency URLs in your lock file.
使用npm ci,它可以从 npm 版本中获得5.7.0(尽管我推荐5.7.1并且由于发行版中断而向上) - 这需要package-lock.json存在并且它跳过从package.json文件构建依赖树,尊重锁定文件中已经解析的依赖 URL。
A very quick
boost for your CI/CD envs (our build time was cut down to a quarter of the original!) and/or to make sure all your developers sit on the same versions of dependencies during development (without having to hard-code strict versions in your package.jsonfile).
快速提升您的 CI/CD 环境(我们的构建时间减少到原来的四分之一!)和/或确保您的所有开发人员在开发过程中使用相同版本的依赖项(无需硬编码package.json文件中的严格版本)。
Note however that npm ciremoves the node_modules/directory before installing, so it won't benefit from any caching strategies.
但是请注意,在安装之前npm ci删除node_modules/目录,因此它不会从任何缓存策略中受益。
2) npm i --prefer-offline
2) npm i --prefer-offline
Use the --prefer-offlineflag with your regular npm install/ npm i. With this approach, you need to make sure you've cached your node_modules/directory between builds (in a CI/CD environment). If it fails to find packages locally with the specific version, it falls back to the network safely.
将--prefer-offline标志与您的常规npm install/ 一起使用npm i。使用这种方法,您需要确保node_modules/在构建之间缓存了目录(在 CI/CD 环境中)。如果在本地找不到特定版本的包,它会安全地回退到网络。
回答by Mostafa Ghadimi
It's better to install pnpmpackage using the following command:
最好pnpm使用以下命令安装软件包:
npm i -g pnpm
npm i -g pnpm
pnpm uses hard links and symlinks to save one version of a module only ever once on a disk. When using npm or Yarn for example, if you have 100 projects using the same version of lodash, you will have 100 copies of lodash on disk. With pnpm, lodash will be saved in a single place on the disk and a hard link will put it into the node_modules where it should be installed.
pnpm 使用硬链接和符号链接在磁盘上只保存一个版本的模块一次。例如,当使用 npm 或 Yarn 时,如果您有 100 个项目使用相同版本的 lodash,则磁盘上将有 100 个 lodash 副本。使用 pnpm,lodash 将被保存在磁盘上的一个位置,并且一个硬链接会将它放入应该安装它的 node_modules 中。
As an example I can mention that whenever you want to install the dependencies of package.jsonfile, what you should do is simply that enter the pnpm iand it handles the other things by itself.
作为一个例子,我可以提到,无论何时你想安装package.json文件的依赖项,你应该做的只是输入它pnpm i,它自己处理其他事情。
回答by hereandnow78
You could also include your node_modulesfolder inside your repository (you are probably using git), and just npm rebuild(which works cross platform) on build/deploy processes, and is pretty fast.
您还可以node_modules在您的存储库中包含您的文件夹(您可能正在使用 git),并且只是npm rebuild(跨平台工作)在构建/部署过程中,并且速度非常快。
This would also give you the benefit of full control over all your dependencies (I know that's what shrinkwrap usually should be used for)
这也将使您完全控制所有依赖项的好处(我知道这通常应该用于shrinkwrap)
Edit:
编辑:
Also you can set the progressflag to false to increase your speed by at least20%. This works only with [email protected], and there will be hopefully fixes for that soon (see second link)
您也可以将进度标志设置为 false 以将您的速度提高至少20%。这仅适用于 [email protected],并且有望很快修复(请参阅第二个链接)
npm set progress=false
回答by BlackStork
As very modern solution you can start to use Docker. Docker allows you virtualize and pre-define as image the current state of your code, including installed npm-modules and other goodies.
作为非常现代的解决方案,您可以开始使用 Docker。Docker 允许您将代码的当前状态虚拟化并预定义为镜像,包括已安装的 npm 模块和其他好东西。
Once the docker image for your infrastructure/env is built locally, or retrieved from remote repository, it will be stored on the host machine, and you can spin server in seconds. Another benefit of it is that you use same virtualized code infrastructure on any machine where you deploy your code. Docker speeds up install/deployment processes and is widely used technology.
一旦您的基础设施/环境的 docker 镜像在本地构建,或从远程存储库中检索,它将存储在主机上,您可以在几秒钟内启动服务器。它的另一个好处是您可以在部署代码的任何机器上使用相同的虚拟化代码基础架构。Docker 加快了安装/部署过程,是一种广泛使用的技术。
To start using docker is enough to (all the snippets are just mock/example for pre-setup and are not by any means most robust/elegant solution) :
开始使用 docker 就足够了(所有代码片段只是用于预设置的模拟/示例,绝不是最强大/优雅的解决方案):
1) Install docker and docker-compose using manuals and get some basic understanding of it at docker.com
1) 使用手册安装docker 和 docker-compose 并在docker.com上对其有一些基本的了解
2) Write Dockerfile file in root of your application
2) 在应用程序的根目录中写入 Dockerfile 文件
FROM node:6.9.5
RUN mkdir /usr/local/app
WORKDIR /usr/local/app
COPY package.json package.json
RUN npm install
3) create docker-compose.yml in the root of your project with such content:
3)在项目的根目录中创建 docker-compose.yml ,内容如下:
version: "2"
server:
hostname: server
container_name: server
image: server
build: .
command: sh -c 'NODE_ENV=development PORT=8080 node app.js'
ports:
- "8080:8080"
volumes: #list of folders and files to use
- ${PWD}/server:/usr/local/server
- ${PWD}/app.js:/usr/local/app.js
4) To start server you will need to docker-compose up -d. To see the logs docker-compose logs -f server. If you will restart your server it will do it in seconds once it built the image already at once.
Then it will cache build layers locally so next run will take only few seconds.
4) 要启动服务器,您需要docker-compose up -d. 查看日志docker-compose logs -f server。如果您将重新启动服务器,它会在几秒钟内完成一次构建映像。然后它将在本地缓存构建层,以便下次运行只需几秒钟。
I know this might be bit of a robust solution, but I am sure it is have most potential/flexibility and is widely used in industry. And while it requires some learning for anyone who did not use Docker before, in my humble oppinion, it is the best one for your problem.
我知道这可能是一个强大的解决方案,但我相信它具有最大的潜力/灵活性并且在工业中被广泛使用。虽然对于之前没有使用过 Docker 的任何人来说,它都需要一些学习,但在我看来,它是解决您问题的最佳选择。
回答by Ondra
Nothing helped me more than disabling antivirus (Windows Defender in my case) I got from 2:30 to 1 minute. With npm-cachepackage I got to ~30 secs. I tried to use yarn, which is very fast, but was randomly failing in my case.
没有什么比从 2:30 到 1 分钟禁用防病毒软件(在我的情况下为 Windows Defender)更能帮助我的了。使用npm-cache包,我达到了大约 30 秒。我尝试使用速度非常快的纱线,但在我的情况下随机失败。
回答by Lee Chee Kiam
For pure npm solution, you may try
对于纯 npm 解决方案,您可以尝试
npm install --prefer-offline --no-audit --progress=false
Prefer offline may not be useful for the first run.
首选离线可能对第一次运行没有用。
回答by Justin
We have been trying to solve this problem to speed up our deployments.
我们一直在努力解决这个问题以加快我们的部署。
We have settled on using pac, which follows the principles in the other answers. It zips the npm modules and includues them in your repo so you don't have a million files in your commits and code reviews and you can just unzip/rebuild for the target machine.
我们已经决定使用 pac,它遵循其他答案中的原则。它压缩 npm 模块并将它们包含在您的存储库中,因此您的提交和代码中没有一百万个文件,您只需为目标机器解压/重建即可。

