Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:58:19+00:00 2026-06-07T12:58:19+00:00

From the node.js documentation: Modules are cached after the first time they are loaded.

  • 0

From the node.js documentation:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require(‘foo’) will get exactly the same object returned, if it would resolve to the same file.

Is there a way to invalidate this cache? i.e. for unit testing, I’d like each test to be working on a fresh object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:58:20+00:00Added an answer on June 7, 2026 at 12:58 pm

    You can always safely delete an entry in require.cache without a problem, even when there are circular dependencies. Because when you delete, you just delete a reference to the cached module object, not the module object itself, the module object will not be GCed because in case of circular dependencies, there is still a object referencing this module object.

    Suppose you have:

    script a.js:

    var b=require('./b.js').b;
    exports.a='a from a.js';
    exports.b=b;
    

    and script b.js:

    var a=require('./a.js').a;
    exports.b='b from b.js';
    exports.a=a;
    

    when you do:

    var a=require('./a.js')
    var b=require('./b.js')
    

    you will get:

    > a
    { a: 'a from a.js', b: 'b from b.js' }
    > b
    { b: 'b from b.js', a: undefined }
    

    now if you edit your b.js:

    var a=require('./a.js').a;
    exports.b='b from b.js. changed value';
    exports.a=a;
    

    and do:

    delete require.cache[require.resolve('./b.js')]
    b=require('./b.js')
    

    you will get:

    > a
    { a: 'a from a.js', b: 'b from b.js' }
    > b
    { b: 'b from b.js. changed value',
      a: 'a from a.js' }
    

    ===

    The above is valid if directly running node.js. However, if using tools that have their own module caching system, such as jest, the correct statement would be:

    jest.resetModules();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove a specific node from a XmlNodeList named listaWidths. This specific
I'm using the following library https://github.com/Leonidas-from-XIV/node-xml2js To convert XML to JSON. After conversion console.log
From the documentation of oracle : Domain Runtime MBean Server : This MBean server
When i do describe-function info , documentation says: Called from a program, FILE-OR-NODE may
From the MongoDB Update documentation they talk about the update operators being atomic. If
I'd like to pass input from node into a commandline-based program I wrote. Starting
I'd like to execute a shell command starting a long-running script from node and
From the node repl: foo = { bar: 'baz'}; console.log (Object.getOwnPropertyDescriptor(foo, 'bar')) Returned value:
can i perform select single node from IXMLDOMElement ,if its possible give me one
just,I tried to remove the node from the xml file what i have.But it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.