What is the reason for the failure of the require("perf_hooks") function?

My understanding is that "perf_hooks" is a component of Node.js. However, when I run tests with npm test, it fails for me and shows the following error (some file names have been altered):

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)

The snippet below is from the file (lib/profiler.js) where "perf_hooks" is utilized:

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

What could possibly be causing this issue? Interestingly, in some instances (npm run ..) it behaves as expected. Any insights or recommendations would be greatly appreciated.

PS. My Node version is relatively recent:

> node --version
v14.8.0

Answer №1

The problem with the "sandboxed-module" 2.0.3 has been resolved in version 2.0.4.

Answer №2

perf_hooks is indeed present in the specified version of Node.js. In release 14.8.0, the require() function generates a MODULE_NOT_FOUND error (instead of ENOENT) when unable to locate a module, indicating that the issue lies elsewhere.

If the error is originating from the require() line, it's possible that require() has been altered through monkey patching. If only require() was patched and not module.require(), consider switching instances of require() to module.require() within lib/profiler.js.

UPDATE: Given the OP's insight, it is probable that sandboxed-module is causing trouble by interfering with module loading. Although they claim the issue is resolved in version 2.0.4, according to details from the repository fix information, it appears to be addressed in version 2.0.1. It is advisable to upgrade to 2.0.4 (or later) for resolution, as of this moment. 👍

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to display percentage value on ReactJS Material UI progress bar

For displaying the progress completed in numbers, I utilize the Linear Determinate component. Take a look at the image below to see how it appears. ...

The Angular 4 application encountered a TypeError due to being unable to access the 'setupScene' property of an undefined object

How can I execute an Angular class function within the Load function in TypeScript? I am encountering an error when trying to call a method within the load method in Angular CLI, resulting in an undefined function error. import { Component, OnInit ,Elemen ...

Apologies, there seems to be an issue with nodist while attempting to initialize a new npm project

I'm currently following a guide on GitHub at this link. However, every time I run the command npm init, I encounter the error message: Sorry, there's an issue with nodist. It couldn't resolve node version %s: %s 11.13.0. No matching versio ...

How to remove a variable definition in Typescript

Is there a way to reset a variable to undefined after assigning it a value? To check, I am using the Underscore function _.isUndefined(). I have attempted both myVariable = undefined and delete myVariable without success. ...

The command "Npm Start Causes sleep is not accepted" just popped up

After cloning a React project from GitHub, I proceeded to run npm install, which successfully installed the node_modules folder. However, upon trying to run npm start, I encountered the following error: 'sleep' is not recognized as an internal or ...

Struggling to display the sorted array items on the screen?

Within the realm of my jsfiddle experiment, my aim is to organize items based on price from highest to lowest by utilizing the following function: function mySortingFunction() { var elements = [].slice.call(document.getElementsByClassName("price")); e ...

How is it possible that the SetTimeout code continues to run even after calling clearTimeout

I have this code snippet within my react component: //some code var timeout = null; //global variable //some more code useEffect(() => { if(...some condition) { console.log('test1'); timeout = setTimeout(() => { ...

Struggling to navigate the DOM using JavaScript and looking for guidance?

I have been searching for an answer to this question without any luck. I am wondering if someone can assist me with finding a solution... I need help using JavaScript (not jQuery) to retrieve the class of the following li element when searching for ' ...

Using JQuery to parse an XML file and automatically redirecting the page if a specific attribute equals "Update"

Updated I've made some edits to clarify my request. My website is built using HTML5, CSS3, and JQuery, resulting in all content being on one page. During updates, I want the ability to set an option in a configuration file so that when users visit m ...

Rendering deeply nested data in a Vue table

I am currently in the process of updating some older code, transitioning to Vue as a replacement. Everything has been going smoothly except for one specific table that is templated using handlebars. With handlebars and nested {{each}} loops, I can easily ...

Guide on incrementing the package.json version using npm's command npm.commands.version

I'm trying to automatically increase the package.json version of my project every time it runs. I added the npm package as a local node-module, but the code snippet below doesn't seem to be working: var npm = require('npm'); npm.load(f ...

Retrieve a specific line of code from a snippet of JavaScript

I am looking to extract a specific line of code from a script that I am receiving via ajax... The line in question is new Date(2010, 10 - 1, 31, 23, 59, 59) found within the following snippet: jQuery(function () { jQuery('#dealCountdown').count ...

The nodejs events function is being triggered repeatedly

I have been developing a CMS on nodejs which can be found at this link. I have incorporated some event hooks, such as: mpObj.emit('MP:FOOTER', '<center>MPTEST Plugin loaded successfully.</center>'); Whenever I handle this ...

Having trouble passing arguments to button methods in jasmine when applying vue and moment libraries

I am working on unit testing a Vue app using `jasmine` and `karma`. Here is an example of the code inside one of my components: After fetching data from a database with `v-for=(data,index)`, I am displaying the `data.date` in the template: <p class=&qu ...

Using jQuery to add a checkbox to a list that is not ordered

My HTML structure includes an unordered list with nested lists. Here is a snippet of the code: <div id="checklist"> <ul> <li>On Page SEO <ul> <li>My work <i>And note.</i>< ...

What is the method of posting to PHP using JavaScript without utilizing Ajax?

My current code involves a drag and drop file uploader to PHP using ajax, but it seems that my web host does not support ajax. Is there an alternative method to achieve this without using ajax? Specifically, I am facing issues with the UploadFile functio ...

Building a basic music player with the <audio> element and JavaScript / jQuery

I am new to Javascript and jQuery, and this is my first time posting a question here. I have been trying to create custom functions for the HTML5 audio tag by following tutorials online. Despite my efforts, I cannot seem to get it working. Although I can m ...

The content of XMLHttpRequest is accessible via the property response

Typically, when we want to retrieve data using AJAX, we would use code like this: var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ elem.innerHTML = xhr.responseText; ...

How can I retrieve the data returned by an ajax call using jQuery?

I have a function that is making an AJAX call to return data. Here is the code: function reqNodelistByField( i ) { $.ajax({ type: 'post', url: './req.nodelist.by.field.php', dataType: 'text', ...

Exploring methods to reload a parent window from a child window using PHP

After opening a popup window from a modal, I am now trying to refresh the page where my modal is located. I have attempted to write some code that I found here, but it doesn't seem to be working. Here is an example of what I have tried: // This fun ...