简体   繁体   English

如何修复 Linux 服务器的 Node/npm/pm2 上找不到错误模块?

[英]How to fix Error Modules Not found on Node/npm/pm2 in Linux Server?

Hello Stackoverflow people!你好 Stackoverflow 人!

I have a web app hosted on a Linux server running with pm2 installed.我有一个 web 应用程序托管在安装了 pm2 的 Linux 服务器上。 I encountered an error in the log which I haven't seen before.我在日志中遇到了以前从未见过的错误。 The error is as follow:错误如下:

1|joo-api  |     at tryModuleLoad (module.js:505:12)
1|joo-api  |     at Function.Module._load (module.js:497:3)
1|joo-api  |     at Function.Module.runMain (module.js:693:10)
1|joo-api  |     at startup (bootstrap_node.js:188:16) code: 'MODULE_NOT_FOUND' }
1|joo-api  | { Error: Cannot find module '/srv/joo-api'
1|joo-api  |     at Function.Module._resolveFilename (module.js:547:15)
1|joo-api  |     at Function.Module._load (module.js:474:25)
1|joo-api  |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
1|joo-api  |     at Module._compile (module.js:652:30)
1|joo-api  |     at Object.Module._extensions..js (module.js:663:10)
1|joo-api  |     at Module.load (module.js:565:32)
1|joo-api  |     at tryModuleLoad (module.js:505:12)
1|joo-api  |     at Function.Module._load (module.js:497:3)
1|joo-api  |     at Function.Module.runMain (module.js:693:10)
1|joo-api  |     at startup (bootstrap_node.js:188:16) code: 'MODULE_NOT_FOUND' }

/home/appadmin/.pm2/logs/joo-app-error.log last 15 lines:
0|joo-app  |     at tryModuleLoad (module.js:505:12)
0|joo-app  |     at Function.Module._load (module.js:497:3)
0|joo-app  |     at Function.Module.runMain (module.js:693:10)
0|joo-app  |     at startup (bootstrap_node.js:188:16) code: 'MODULE_NOT_FOUND' }
0|joo-app  | { Error: Cannot find module '/srv/joo-app'
0|joo-app  |     at Function.Module._resolveFilename (module.js:547:15)
0|joo-app  |     at Function.Module._load (module.js:474:25)
0|joo-app  |     at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
0|joo-app  |     at Module._compile (module.js:652:30)
0|joo-app  |     at Object.Module._extensions..js (module.js:663:10)
0|joo-app  |     at Module.load (module.js:565:32)
0|joo-app  |     at tryModuleLoad (module.js:505:12)
0|joo-app  |     at Function.Module._load (module.js:497:3)
0|joo-app  |     at Function.Module.runMain (module.js:693:10)
0|joo-app  |     at startup (bootstrap_node.js:188:16) code: 'MODULE_NOT_FOUND' }

and now pm2 status is errored现在 pm2 状态是错误

I tried pm2 kill, restart, start, node update and pm2 update.我试过 pm2 kill、restart、start、node update 和 pm2 update。 All did not work.一切都没有奏效。 Anyone have any idea what causing it?有人知道是什么原因造成的吗?

Have you tried deleting and reinstalling node_modules?您是否尝试过删除并重新安装 node_modules? Make sure you updated package.json on the server as well.确保在服务器上也更新了 package.json。

This is for those developing on Windows and then deploying in Linux machines.这适用于那些在 Windows 上开发,然后在 Linux 机器上部署的人。

If you deploy on a linux machine and it is suddenly telling you that modules not found everywhere, most likely it is because of the letter casing of your require or import statements .如果你在 linux 机器上部署,它突然告诉你模块没有在任何地方找到,很可能是因为你的requireimport语句的字母大小写

For instance, if your file name is ErrorModule.js but you accidentally require as "errorModule", eg:例如,如果您的文件名是 ErrorModule.js 但您不小心要求为“errorModule”,例如:

const ErrorModule = require('../errors/errorModule');

this will fail on Linux, because linux is case sensitive .这将在 Linux 上失败,因为 linux区分大小写 It should be imported as它应该被导入为

const ErrorModule = require('../errors/ErrorModule');

Now, you might be wondering why when you ran on your Windows machine it works;现在,您可能想知道为什么当您在 Windows 机器上运行时它可以工作; that is because Windows is case- insensitive .这是因为 Windows不区分大小写 You can require files with the wrong letter casing on Windows and it will still work.您可以要求 Windows 上字母大小写错误的文件,它仍然可以工作。 While linux imports are case- sensitive .而 linux 导入区分大小写

I hope this post can help.我希望这篇文章能有所帮助。 I was also stuck with a similar problem for a few hours我也被类似的问题困住了几个小时

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

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