Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require cache problem/question #3144

Closed
nairihar opened this issue Dec 26, 2020 · 3 comments
Closed

Require cache problem/question #3144

nairihar opened this issue Dec 26, 2020 · 3 comments

Comments

@nairihar
Copy link

  • Node.js v12.18.1:
  • OS: MacOS
  • Scope runtime:

I'm trying to create a JSON file after running node, and then require it.

This code works fine.

Screen Shot 2020-12-26 at 3 34 22 PM

When I tried to require the file inside try-catch before creation, I wasn't able to require it after creation.

Screen Shot 2020-12-26 at 3 36 48 PM

Error:
Screen Shot 2020-12-26 at 3 38 40 PM

This error related to my second requires, since the first one is inside trying catch. Meanwhile, my file was created.

Screen Shot 2020-12-26 at 3 39 33 PM

I thought there is a cache, so I need to delete the value for my file from require.cache.

Screen Shot 2020-12-26 at 3 40 55 PM

But it didn't help:

Screen Shot 2020-12-26 at 3 41 11 PM

Could you please help me to understand how Node.js works in this case? And how can I solve this issue?

@vsemozhetbyt
Copy link

It seems failed requires are not cached:

try {
  require('./test.json');
} catch (err) {}

console.log(require.cache); // no json entries

@vsemozhetbyt
Copy link

Reported in nodejs/node#36638

@marsonya
Copy link
Member

This issue was reported as a bug in nodejs/node here.
It was fixed in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants