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 file which not exists and create it latter and call require the file again will throw Cannot find module error #39214

Closed
lovetingyuan opened this issue Jul 1, 2021 · 4 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. module Issues and PRs related to the module subsystem.

Comments

@lovetingyuan
Copy link

  • Version: v14.15.4
  • Platform: Darwin tingyuandeMacBook-Pro.local 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64
  • Subsystem: no

What steps will reproduce the bug?

Just run this code:

const fs = require('fs')
const path = require('path')

const jsonFile = path.resolve('./test.json')

if (fs.existsSync(jsonFile)) {
  fs.unlinkSync(jsonFile)
}

try {
  const a = require(jsonFile)
  console.log('test1', a)
} catch (e) {
  // throw error, it is correct, cause jsonFile not exists.
  console.log('test error1: ', e.message)
}

fs.writeFileSync(jsonFile, JSON.stringify({
  name: 'test'
}, null, 2))

try {
  const a = require(jsonFile)
  console.log('test2', a)
} catch (e) {
  //  still throw error, but jsonFile has exists.
  console.log('test error2: ', e.message)
}

Promise.resolve().then(() => {
  try {
    const a = require(jsonFile)
    console.log('test3', a)
  } catch (e) {
    //  no more error.
    console.log('test error3: ', e.message)
  }
})

How often does it reproduce? Is there a required condition?

No condition, 100% occurs.

What is the expected behavior?

First require call, it is reasonable to throw error, but with second require call, there is should no error.

What do you see instead?

Error throwed when second calling require.

Additional information

I think this issue is caused by incorrect statCache process, see Module._findPath in modules/cjs/loader.js.

@lovetingyuan lovetingyuan changed the title require file which not exists and create it latter and call require the file will throw Cannot find module error require file which not exists and create it latter and call require the file again will throw Cannot find module error Jul 1, 2021
@Ayase-252 Ayase-252 added module Issues and PRs related to the module subsystem. v14.x labels Jul 1, 2021
@Ayase-252
Copy link
Member

Reproed in only v14.x, not in v16

@lovetingyuan
Copy link
Author

Reproed in only v14.x, not in v16

yes, I found some difference

image

@aduh95
Copy link
Contributor

aduh95 commented Jul 1, 2021

That looks like a duplicate of #36638. The fix is unlikely to be backported to v14.x as it would be potentially breaking for folks who expects this behavior.

@aduh95
Copy link
Contributor

aduh95 commented Jul 1, 2021

Closing as duplicate.

@aduh95 aduh95 closed this as completed Jul 1, 2021
@aduh95 aduh95 added the duplicate Issues and PRs that are duplicates of other issues or PRs. label Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants