简体   繁体   English

Node.js 服务器启动时间很慢

[英]Node.js server very slow starting time

I've build a small angular.js web app hosted on a Node.js server running on my computer.我已经构建了一个小型 angular.js web 应用程序,该应用程序托管在我的计算机上运行的 Node.js 服务器上。 When I start my node server, it takes ~30/35 seconds.当我启动节点服务器时,大约需要 30/35 秒。 The exact same server on a collegue computer takes 2-4s to start.同事计算机上完全相同的服务器需要 2-4 秒才能启动。

I've logged times in the console to see which steps were slower.我已经在控制台中记录了时间以查看哪些步骤较慢。 It is right from the beggining, when it loads dependencies in the require() steps.它从一开始就是在 require() 步骤中加载依赖项时。 The Express module takes 26s, Morgan module takes 4s, and the rest 3s. Express模块需要26s,Morgan模块需要4s,rest 3s。

I understand that you probably can't find the root cause, but if anyone has an idea of what can cause this on my computer, I would gladly take any hint:)我知道您可能找不到根本原因,但是如果有人知道是什么导致我的计算机出现这种情况,我很乐意接受任何提示:)

Thank you for the help.感谢您的帮助。

So, sometimes this process can be very slow depending on your PC.因此,有时此过程可能会非常缓慢,具体取决于您的 PC。 require is actually loading the modules and this could take a while depending on hardware and size of the dependency. require实际上正在加载模块,这可能需要一段时间,具体取决于硬件和依赖项的大小。 For me, express takes under a second on my Macbook + SSD.对我来说,在我的 Macbook + SSD 上使用 express 不到一秒钟。

Check out how much free space you have on your drive.查看驱动器上有多少可用空间。 Sometimes, it may be because the drive is old and has run out of ample blocks!有时,可能是因为驱动器太旧并且已经用完了足够的块!

Found it... The application was on a remote drive, on a cloud or something I guess (we have access to this drive when connected to our session, no matter the pc).找到它...该应用程序位于远程驱动器上、云上或我猜的其他东西上(连接到我们的会话时,我们可以访问此驱动器,无论电脑如何)。 Relocate it on D: solved the issue...将其重新定位到 D:解决了问题...

I ran across this question as I was searching for a solution to my issue with a slow starting node server in development.当我正在寻找解决我的问题的解决方案时,我遇到了这个问题,即开发中的节点服务器启动缓慢。 None of the answers took care of my issue, but for those that are trying to root out potential causes, I was loading a large json object (~500mb) in the root of a file (outside of a function) and it caused my server to go from about 1 sec loading time to 20.没有一个答案解决了我的问题,但是对于那些试图根除潜在原因的人,我在文件的根目录(函数外部)加载了一个大型 json 对象(~500mb),它导致了我的服务器从大约 1 秒的加载时间到 20 秒。

So double check that your not loading any large dependencies on server start if this has happend to you.因此,如果您遇到这种情况,请仔细检查您是否没有在服务器启动时加载任何大的依赖项。

The restarting of my server was very slow due to this issue with faker (version: 7.3.0).由于faker(版本:7.3.0)的这个问题,我的服务器重新启动非常慢。

Using const { faker } = require('@faker-js/faker/locale/en_US');使用const { faker } = require('@faker-js/faker/locale/en_US'); instead of const { faker } = require('@faker-js/faker');而不是const { faker } = require('@faker-js/faker'); sped up the require statement and the app restart by about 50 seconds.加快了require语句的速度,应用程序重启了大约 50 秒。

Using docker and having a volume mounted for node_modules caused the server restart to be very slow.使用 docker 并为node_modules安装卷导致服务器重新启动非常慢。

Removing the mounted volume on node_modules sped up the restart by about 60 seconds.删除node_modules上已安装的卷可将重启速度加快约 60 秒。

See the following links for more details on this.有关这方面的更多详细信息,请参阅以下链接。

  1. https://stackoverflow.com/a/47564342/9530790 https://stackoverflow.com/a/47564342/9530790
  2. https://stackoverflow.com/a/49081609/9530790 https://stackoverflow.com/a/49081609/9530790

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

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