使用 React-static 创建站点时出现错误找不到module“perf_hooks”

IT技术 javascript node.js reactjs
2021-05-09 00:04:33

使用 react-static create 命令创建站点时出现以下错误:

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)

刚刚使用 npm 安装了 react-static。

2个回答

perf_hooks 从 nodejs v8.5 开始可用。

通过node -v.

我的代码有:

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

解决方法:尚未解决的https://github.com/nodejs/node/issues/28635

由于在这种情况下浏览器由公开的全局处理微不足道,我只是将其修改为:

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

这使得 webpack 无法根据需要查看依赖项,如在:How can I make webpack skip a require 中提到的

在 react-scripts 4.0.3 上测试。