-1

When running a specific javascript in node, I get a console.log() message that is not in the current code, but was in a previous version of the code. The code in question is in a custom module. I suspect node cache is corrupted. The next thing the console shows is an error, but I wouldn't be surprised if my code has an error. Seeing an error doesn't surprise me. But seeing a console.log message that shouldn't be there does.

I powered down/up the computer. Prior to loading the module, I tried: delete require.cache[require.resolve('')]; But nothing I've tried has worked.

I did read node.js require() cache - possible to invalidate? . But it did not lead me to a solution. The module in question requires 2 modules that come with node. I did not try deleting them since they don't see to be an issue.

I'm new with node and anything more than simple js. Do you think cache is the problem? Is there a better way to clear it? My next attempt may be to uninstall node and then reinstall it, but I'm hoping someone has a better idea.

I am exporting the module with: module.exports = async (param1, param2){...} I am loading the module with: oModule = require('').

  • 1
    Nodejs does not have a persistent cache. When you stop and restart the server, it loads all new copies of code from disk, no cache. A module cache is used only once nodejs is already running for modules that have already been loaded in this session of the server. – jfriend00 Oct 31 '20 at 00:53
  • FYI, `module.exports = async (param1, param2){...}` is not valid syntax. Do you mean to have an arrow function definition there? – jfriend00 Oct 31 '20 at 00:54
  • Zip everything up (excluding node_modules) and email it to drummondaw82@gmail.com. I'll see if I can figure it out. Comment once sent as I rarely check that email. – Tony Drummond Oct 31 '20 at 00:54
  • @TonyDrummond - That's an odd request for stackoverflow. The whole point of this site is to get the OP to disclose enough information ON THIS SITE so we can all help troubleshoot. – jfriend00 Oct 31 '20 at 00:55
  • 1
    If code is running that isn't your current version of the code, then there must be another copy somewhere on your hard drive that is getting found first before the one you want. – jfriend00 Oct 31 '20 at 02:17
  • jfriend, thank you for pointing out that there is not a persistent cache. You were right that I had a console.log() message somewhere else. I diligently looked, tested, re-looked, but missed it in code I didn't realize I needed to look at. Do you think the fact that there is no persistent cache should be an answer, or that I should delete this question? – Jim Thompson Oct 31 '20 at 02:37

1 Answers1

2

Nodejs does not have a persistent cache that survives from one running of your program to another. When you restart your program, all your code is reloaded from disk from the original location - no cache. The module cache is used only once a given instance of your program has already loaded your code the first time from disk and then if some other code in your program asks to load the same module again, then it's loaded from the cache that was populated from the first time. But, once you restart your program, there is no persistent cache at all.

So, if you continue to see a console.log() that you don't think is in your code, then you're either getting that console.log() from somewhere else in your code (not where you thought it was coming from) or there's another copy of the code somewhere that is getting loaded (other than the copy you have updated).

jfriend00
  • 683,504
  • 96
  • 985
  • 979