简体   繁体   中英

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

Hello Stackoverflow people!

I have a web app hosted on a Linux server running with pm2 installed. 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

I tried pm2 kill, restart, start, node update and pm2 update. All did not work. Anyone have any idea what causing it?

Have you tried deleting and reinstalling node_modules? Make sure you updated package.json on the server as well.

This is for those developing on Windows and then deploying in Linux machines.

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 .

For instance, if your file name is ErrorModule.js but you accidentally require as "errorModule", eg:

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

this will fail on Linux, because linux is case sensitive . 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; that is because Windows is case- insensitive . You can require files with the wrong letter casing on Windows and it will still work. While linux imports are case- sensitive .

I hope this post can help. I was also stuck with a similar problem for a few hours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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