Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Cannot find module 'perf_hooks' when creating site with React-static

I got the following error when creating a site with react-static create command:

Error: Cannot find module 'perf_hooks'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/react-static/lib/utils/index.js:45:19)

Just installed react-static using npm.

like image 517
MiguelSlv Avatar asked Feb 25 '26 19:02

MiguelSlv


2 Answers

perf_hooks is available since nodejs v8.5.

Check your nodejs version by node -v.

like image 119
hankchiutw Avatar answered Feb 27 '26 08:02

hankchiutw


My code had:

if (typeof performance === 'undefined') {
  // Older Node.js
  globals.performance = require('perf_hooks').performance;
} else {
  // Browser.
  globals.performance = performance;
}

to work around: https://github.com/nodejs/node/issues/28635 that hadn't been solved.

Since in that case the browser is taken care of trivially by the exposed global, I just hacked it to:

  globals.performance = eval('require')('perf_hooks').performance;

which makes webpack incapable of seeing the dependency as desired, as mentioned at: How can I make webpack skip a require

Tested on react-scripts 4.0.3.