Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
Why doesn't setting NodeJS module cache to false get NodeJS to reload module?

I have 2 files, one named one.js and other name first.js.
one.js

var first = require("./first.js")
require.cache[first.module.id].loaded=false
require("./first.js")

first.js

console.log("First is running");
module.exports={module}

It's output is:

First is running

and not

First is running
First is running

as I expected. Why is it that even though I set the loaded property of the first.js module to false, it is still not being reloaded?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to remove the cached module from the require.cache object to force node to load the module again. Node won't execute the module as long as it is in the require.cache.

One way to delete the module from the cache is shown below:

delete require.cache[require.resolve("./first.js")];

Side Tip: You could export a function from a module and just call that function again and again to have a similar result of loading the module everytime it is required.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!