gpt4 book ai didi

node.js - Heroku Slug 大小太大 - nodejs

转载 作者:行者123 更新时间:2023-12-04 08:37:27 26 4
gpt4 key购买 nike

这是我的第一个 heroku 应用程序……我看到我的 heroku slug 大小为 296MB……令人不安地接近快速 300MB 的启动时间。
这是一个安装了 ejs、path 和 express 的 puppeteer 应用程序。我有一堆静态文件,但它们似乎没有占用大部分空间。
我真的很感激这方面的帮助!
static files in public folder
编辑:
我的 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"
}

最佳答案

在评论中的所有讨论之后,我意识到一件事:您不能在 Heroku dynos 中删除文件夹!而且您可能无论如何都不想...这些文件夹看起来很重要(请记住:Heroku dynos 通常是迷你 linux 操作系统,因此 .apt 文件夹可能包含其中一些迷你 linux 操作系统文件)。当然,那些 .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 (READMEs, CHANGELOGs, 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 没有帮助。正如您已经提到的,您的静态文件并没有真正占用太多空间——它实际上只是 node_modules .我想不出任何其他方法来减少您的 slug 大小(除非您想将静态文件移动到外部存储设施并执行一些模糊的获取和缓存操作以将它们提供给客户端......)。
    注意 : 我没有尝试过任何这些步骤。这些理论上应该有效,但我不确定它们是否有效。

    关于node.js - Heroku Slug 大小太大 - nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64734294/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com