Heroku Slug 尺寸太大 - nodejs
Heroku Slug Size too large - nodejs
这是我的第一个 heroku 应用程序...我看到我的 heroku slug 大小是 296MB...启动时间快到 300MB 了,令人不安。
这是一个安装了 ejs、path 和 express 的木偶应用程序。我有一堆静态文件,但它们似乎并没有占据大部分 space..
非常感谢对此的帮助!
编辑:
我的 package.json 现在看起来像这样(看不出我如何真正 trim 降低我的 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"
}
在评论里说了这么多之后,我意识到一件事:你不能在Heroku dynos中删除文件夹!而且您可能无论如何都不想...这些文件夹看起来很重要(请记住:Heroku dynos 通常是 mini-linux OSes,因此 .apt
文件夹可能包含其中一些mini-linux OS 个文件)。当然,那些 .apt
和 .heroku
文件夹可能不会增加您的 slug 大小。对不起,徒劳无功。
所以呢?让我们减少真正重要的东西的大小。
第一:减小 node_modules
尺寸。
由于您的 node_modules
文件夹是您可以控制的最大文件夹,让我们从这里开始。我们将关注 this 文章。我将修改一些说明,以便它们可以与 Heroku 一起使用。
- 减少依赖项的数量:这听起来似乎很容易,但它有很大帮助。考虑一下您安装的软件包。你真的需要它们吗?如果你这样做了,是否有更轻量级的软件包可供你使用?示例:
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).
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...). Because you can't remove those files manually (and who wants to), you need to use an automated tool and Heroku build scripts. Here's an example (remove the comments when you put this in your 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"
}
安...
这就是您所能做的。该文章中提到的其他步骤对 Heroku 没有帮助。正如您已经提到的,您的静态文件并没有真正占用太多 space——它实际上只是 node_modules
。我想不出任何其他方法来减少你的 slug 大小(除非你想将你的静态文件移动到外部存储设施并做一些模糊的获取和缓存操作来为客户提供服务......)。
注意:我没有尝试过任何这些步骤。这些在理论上应该有效,但我不确定它们是否有效。
由于 Puppeteer 占用了如此多的 Heroku slug 大小,因此操作空间真的非常小。目前,坚持使用 Node 12 和 Puppeteer 2.0.0 将确保您可以设法将 Heroku slug 大小保持在 300M 以下。
在您的 package.json 中使用这些:
"dependencies": {
"puppeteer": "2.0.0"
}
"engines": {
"node": "12.x"
}
如果您升级 node 或 puppeteer 的版本号,即使您几乎没有其他依赖项,您的 slug 大小也会超过 300M。
我使用 Puppeteer 2.0.0 的另一个原因是我需要使用 waitForFileChooser() 函数将媒体文件上传到某些网站。但是 waitForFileChooser() 在 Puppeteer 2.1.1 中被破坏了。而且在5.5.0还是坏了。
另外,我认为 Node 12 和 Puppeteer 实际上非常稳定。因此,为了帮助 Heroku 管理其出色的免费服务的成本,我可以接受一点版本延迟。
我想加入这个讨论,因为我遇到了同样的问题,并且因为 500MB 的限制而无法让它工作,我真的很沮丧。我尝试了各种方法来减少它,但我发现即使我将它减小到 500MB 以下,性能也会大大降低。所以我决定做的是为 Puppeteer 提供一个单独的专用服务器,并从我的代码库中完全删除 Puppeteer 部分,这将我的 slug 大小减少了 200+MB。
看看它:
https://github.com/davidjuhyung/puppeteer-api
这是我的第一个 heroku 应用程序...我看到我的 heroku slug 大小是 296MB...启动时间快到 300MB 了,令人不安。
这是一个安装了 ejs、path 和 express 的木偶应用程序。我有一堆静态文件,但它们似乎并没有占据大部分 space..
非常感谢对此的帮助!
编辑: 我的 package.json 现在看起来像这样(看不出我如何真正 trim 降低我的 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"
}
在评论里说了这么多之后,我意识到一件事:你不能在Heroku dynos中删除文件夹!而且您可能无论如何都不想...这些文件夹看起来很重要(请记住:Heroku dynos 通常是 mini-linux OSes,因此 .apt
文件夹可能包含其中一些mini-linux OS 个文件)。当然,那些 .apt
和 .heroku
文件夹可能不会增加您的 slug 大小。对不起,徒劳无功。
所以呢?让我们减少真正重要的东西的大小。
第一:减小 node_modules
尺寸。
由于您的 node_modules
文件夹是您可以控制的最大文件夹,让我们从这里开始。我们将关注 this 文章。我将修改一些说明,以便它们可以与 Heroku 一起使用。
- 减少依赖项的数量:这听起来似乎很容易,但它有很大帮助。考虑一下您安装的软件包。你真的需要它们吗?如果你这样做了,是否有更轻量级的软件包可供你使用?示例:
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). 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...). Because you can't remove those files manually (and who wants to), you need to use an automated tool and Heroku build scripts. Here's an example (remove the comments when you put this in yourpackage.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"
}
安...
这就是您所能做的。该文章中提到的其他步骤对 Heroku 没有帮助。正如您已经提到的,您的静态文件并没有真正占用太多 space——它实际上只是 node_modules
。我想不出任何其他方法来减少你的 slug 大小(除非你想将你的静态文件移动到外部存储设施并做一些模糊的获取和缓存操作来为客户提供服务......)。
注意:我没有尝试过任何这些步骤。这些在理论上应该有效,但我不确定它们是否有效。
由于 Puppeteer 占用了如此多的 Heroku slug 大小,因此操作空间真的非常小。目前,坚持使用 Node 12 和 Puppeteer 2.0.0 将确保您可以设法将 Heroku slug 大小保持在 300M 以下。
在您的 package.json 中使用这些:
"dependencies": {
"puppeteer": "2.0.0"
}
"engines": {
"node": "12.x"
}
如果您升级 node 或 puppeteer 的版本号,即使您几乎没有其他依赖项,您的 slug 大小也会超过 300M。
我使用 Puppeteer 2.0.0 的另一个原因是我需要使用 waitForFileChooser() 函数将媒体文件上传到某些网站。但是 waitForFileChooser() 在 Puppeteer 2.1.1 中被破坏了。而且在5.5.0还是坏了。
另外,我认为 Node 12 和 Puppeteer 实际上非常稳定。因此,为了帮助 Heroku 管理其出色的免费服务的成本,我可以接受一点版本延迟。
我想加入这个讨论,因为我遇到了同样的问题,并且因为 500MB 的限制而无法让它工作,我真的很沮丧。我尝试了各种方法来减少它,但我发现即使我将它减小到 500MB 以下,性能也会大大降低。所以我决定做的是为 Puppeteer 提供一个单独的专用服务器,并从我的代码库中完全删除 Puppeteer 部分,这将我的 slug 大小减少了 200+MB。 看看它: https://github.com/davidjuhyung/puppeteer-api