Why require(“perf_hooks”) fails?

杀马特。学长 韩版系。学妹 提交于 2021-01-07 02:58:43

问题


In my understanding "perf_hooks" is a part of Node.js. However when testing with npm test it fails for me with the following (some filenames are changed):

Error: ENOENT: no such file or directory, open 'perf_hooks'
      at Object.openSync (fs.js:465:3)
      at Object.readFileSync (fs.js:368:35)
      at SandboxedModule._getCompileInfo (node_modules/sandboxed-module/lib/sandboxed_module.js:265:20)
      at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:245:22)
      at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
      at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
      at /Users/asmirnov/Documents/blabla2.js:51:12
      at Object.<anonymous> (lib/profiler.js:53:3)
      at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:251:19)
      at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
      at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
      at Object.<anonymous> (lib/blabla1.js:29:20)
      at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:251:19)
      at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
      at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
      at SandboxedModule.requireInterceptor (node_modules/sandboxed-module/lib/sandboxed_module.js:227:9)
      at Context.<anonymous> (test/blabla1.js:73:22)
      at processImmediate (internal/timers.js:458:21)

This is a part of the file (lib/profiler.js) that uses it:

const {performance, PerformanceObserver} = (function()
{
  try
  {
    if (window && typeof window.performance == "undefined")
      throw new TypeError();
    return window;
  }
  catch (e)
  {
    return require("perf_hooks");
  }
})();

What can be a reason? However in some scenarios (npm run ..) it works as expected. Any suggestions are appreciated.

PS. Node is relatively fresh:

> node --version
v14.8.0

回答1:


That's a "sandboxed-module" 2.0.3 issue that was fixed in 2.0.4.




回答2:


perf_hooks is indeed in that version of Node.js. In 14.8.0, require() throws MODULE_NOT_FOUND (and not ENOENT) when it can't find a module, so the problem is something else.

If it is definitely the require() line that is throwing, then it seems likely that require() has been monkey-patched. If they patched require() but not module.require(), you can try changing require() to module.require() in lib/profiler.js.

EDIT: As OP suggests, sandboxed-module is a likely culprit since it messes with module loading. They report that it's fixed in 2.0.4, but based on the repository information on the fix, it would appear to be fixed in 2.0.1. Either way, updating to 2.0.4 (or later, but that's the latest as of this writing) is 👍.



来源:https://stackoverflow.com/questions/64752215/why-requireperf-hooks-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!