简体   繁体   English

Heroku Slug 尺寸太大 - nodejs

[英]Heroku Slug Size too large - nodejs

This is my first heroku app... and I see that my heroku slug size is 296MB... getting uncomfortably close to the fast 300MB boot time.这是我的第一个 heroku 应用程序...我看到我的 heroku slug 大小是 296MB...接近 300MB 的快速启动时间令人不安。

This is a puppeteer app with ejs, path, and express installed.这是一个安装了 ejs、path 和 express 的 puppeteer 应用程序。 I have a bunch of static files but they don't seem to be taking up majority of the space..我有一堆 static 文件,但它们似乎没有占用大部分空间..

I would really appreciate help with this!我真的很感激这方面的帮助!

公共文件夹中的静态文件

Edit: My package.json looks like this now (don't see how I could really trim down my node_modules - in fact, I wanted to add more dependencies as I further develop my app):编辑:我的 package.json 现在看起来像这样(看不出我如何真正削减我的 node_modules - 事实上,我想在进一步开发我的应用程序时添加更多依赖项):

    "scripts": {
    "start": "node server.js",
    "heroku-postbuild": "curl -sf https://gobinaries.com/tj/node-prune | PREFIX=. sh&&./node-prune"
  },
  "dependencies": {
    "ejs": "^3.1.5",
    "express": "^4.17.1",
    "path": "^0.12.7",
    "puppeteer": "^5.3.1"
  }

After all the talk in the comments, I realized one thing: you can't delete folders in Heroku dynos!在评论中的所有讨论之后,我意识到一件事:您不能在 Heroku dynos 中删除文件夹! And you probably wouldn't want to anyways... Those folders look important (remember: Heroku dynos are usually mini-linux OSes, so that .apt folder probably contains some of those mini-linux OS files).而且您可能无论如何都不想...这些文件夹看起来很重要(请记住:Heroku dynos 通常是迷你 linux 操作系统,因此.apt文件夹可能包含其中一些迷你 linux 操作系统文件)。 And of course, those .apt and .heroku folders probably don't add to your slug size.当然,那些.apt.heroku文件夹可能不会增加您的 slug 大小。 Sorry for that wild goose chase.对不起,那次大雁追逐。

So?所以? Let's reduce the sizes of stuff that actually matter.让我们减少真正重要的东西的大小。

Number one: reduce your node_modules size.第一:减少你的node_modules大小。

Since your node_modules folder is the biggest folder that you could control, let's start there.由于您的node_modules文件夹是您可以控制的最大文件夹,让我们从那里开始。 We'll be followingthis article.我们将关注这篇文章。 I'll be modifying some instructions so that they work with Heroku.我将修改一些指令,以便它们与 Heroku 一起使用。

  1. Reduce the number of dependencies : This may sound like a no-brainer, but it helps a lot.减少依赖项的数量:这听起来很简单,但它有很大帮助。 Think about the packages you install.想想你安装的包。 Do you really need them?你真的需要它们吗? And if you do, is there a more lightweight package somewhere out there that you could use instead?如果你这样做了,是否有一个更轻量级的包可以替代? Example:例子:

A lot of times I see people installing Jest just for simple unit tests (about 460+ dependencies, +/-60MB) when there are smaller libs that will do exactly the same (Mocha – 115 dependencies, 12MB).很多时候我看到人们安装 Jest 只是为了简单的单元测试(大约 460 多个依赖项,+/-60MB),而当有更小的库可以做完全相同的事情时(Mocha – 115 个依赖项,12MB)。 2. Remove unnecessary files : Besides the usual installed when you install a package ( .js files, etc.), there's also a lot of... unneeded junk included ( README s, CHANGELOG s, source files...). 2.删除不必要的文件:除了安装包时通常安装的文件( .js文件等),还有很多......包括不需要的垃圾( README s、 CHANGELOG s、源文件......)。 Because you can't remove those files manually (and who wants to), you need to use an automated tool and Heroku build scripts .因为您无法手动删除这些文件(以及谁想要删除),所以您需要使用自动化工具和Heroku 构建脚本 Here's an example (remove the comments when you put this in your package.json ).这是一个示例(将其放入package.json时删除注释)。

"scripts": {
  // Other scripts...
  // The following script dowloads node-prune (https://github.com/tj/node-prune)
  // in the current build directory and runs it.
  // The following script is run AFTER Heroku installs dependencies, but BEFORE
  // Heroku caches them.
  // EDIT: You have to do `./node-prune` instead of `node-prune`.
  "heroku-postbuild": "curl -sf https://gobinaries.com/tj/node-prune | PREFIX=. sh&&./node-prune"
}

Annnd...安恩...

That's about all you could do.这就是你所能做的。 The other steps mentioned in that article won't help on Heroku.那篇文章中提到的其他步骤对 Heroku 没有帮助。 And as you already mentioned, your static files aren't really taking up much space–it's actually just node_modules .正如您已经提到的,您的静态文件并没有真正占用太多空间——它实际上只是node_modules I can't think of any other ways to reduce your slug size (unless you want to move your static files to an external storage facility and do some obscure get-and-cache maneuverers to serve them to clients...).我想不出任何其他方法来减少您的 slug 大小(除非您想将静态文件移动到外部存储设施并执行一些模糊的获取和缓存操作以将它们提供给客户端......)。

NOTE : I have not tried any of these steps.注意:我还没有尝试过这些步骤中的任何一个。 These should work in theory, but I'm not positive if they work or not.这些理论上应该有效,但我不确定它们是否有效。

Since Puppeteer takes so much Heroku slug size, there's really very little room to maneuver.由于 Puppeteer 占用了如此多的 Heroku slug 大小,因此几乎没有回旋余地。 For now, sticking to Node 12 and Puppeteer 2.0.0 will ensure that you can manage to keep your Heroku slug size just below 300M.目前,坚持使用 Node 12 和 Puppeteer 2.0.0 将确保您可以设法将 Heroku slug 大小保持在 300M 以下。

Use these in your package.json:在 package.json 中使用这些:

"dependencies": {
    "puppeteer": "2.0.0"
}

"engines": {
    "node": "12.x"
}

If you upgrade either of the version numbers of node or puppeteer, your slug size will exceed 300M even if you have very little other dependencies.如果你升级 node 或 puppeteer 的版本号,即使你的其他依赖很少,你的 slug 大小也会超​​过 300M。

Another reason I'm using Puppeteer 2.0.0 is that I need to use the waitForFileChooser() function to upload media files to some web sites.我使用 Puppeteer 2.0.0 的另一个原因是我需要使用 waitForFileChooser() 函数将媒体文件上传到某些网站。 But waitForFileChooser() is broken in Puppeteer 2.1.1.但是 waitForFileChooser() 在 Puppeteer 2.1.1 中被破坏了。 And it's still broken in 5.5.0.而且它在 5.5.0 中仍然被破坏。

Plus, I think Node 12 and Puppeteer are actually quite stable.另外,我认为 Node 12 和 Puppeteer 实际上非常稳定。 So I can accept a little bit version lag for the benefit of helping Heroku manage the cost of its fantastic free service.所以我可以接受一点版本延迟,以帮助 Heroku 管理其出色的免费服务的成本。

I wanted to chip in to this discussion, because I have faced to same problem, and was really frustrated that I can't get it work because of the 500MB limit.我想加入这个讨论,因为我遇到了同样的问题,并且因为 500MB 的限制而无法让它工作而感到非常沮丧。 I tried every way to reduce it but I found that the performance is hugely degraded even if I get to make it just under 500MB.我尝试了各种方法来减少它,但我发现即使我将它减小到 500MB 以下,性能也会大大降低。 So what I decided to do was to have a separate dedicated server for Puppeteer, and completely remove the Puppeteer portion out of my codebase, this reduced my slug size by 200+MB.所以我决定做的是为 Puppeteer 提供一个单独的专用服务器,并从我的代码库中完全删除 Puppeteer 部分,这将我的 slug 大小减少了 200+MB。 Take a look at it: https://github.com/davidjuhyung/puppeteer-api看一看: https://github.com/davidjuhyung/puppeteer-api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM