You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Hapi框架开发Node.js后端时遇PerfHooks启动错误求助

Hapi服务启动报错:PerfHooks.performance.eventLoopUtilization is not a function

问题描述

使用Node.js结合Hapi框架开发后端,执行npm run start启动服务时触发错误,无法正常启动服务。预期启动后输出Server berjalan pada http://localhost:5000,执行curl -X GET http://localhost:5000返回{"statusCode":404,"error":"Not Found","message":"Not Found"},但实际出现报错。

相关代码

server.js

const Hapi = require('@hapi/hapi');
const init = async() => {
    const server = Hapi.server({
        port: 5000,
        host: 'localhost',
    });
    await server.start();
    console.log(`Server berjalan pada ${server.info.uri}`);
}
init();

package.json

{
    "name": "hapi-web-server",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "start": "node server.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
        "@hapi/hapi": "^21.0.0"
    }
}

错误输出

> hapi-web-server@1.0.0 start E:\DOCUMENT\Dicoding\hapi-web-server
> node server.js
(node:19696) UnhandledPromiseRejectionWarning: TypeError: PerfHooks.performance.eventLoopUtilization is not a function
    at new Heavy (E:\DOCUMENT\Dicoding\hapi-web-server\node_modules\@hapi\heavy\lib\index.js:42:60)
    at new module.exports.internals.Core (E:\DOCUMENT\Dicoding\hapi-web-server\node_modules\@hapi\hapi\lib\core.js:122:22)
    at Object.module.exports (E:\DOCUMENT\Dicoding\hapi-web-server\node_modules\@hapi\hapi\lib\server.js:22:18)
    at init (E:\DOCUMENT\Dicoding\hapi-web-server\server.js:4:25)
    at Object.<anonymous> (E:\DOCUMENT\Dicoding\hapi-web-server\server.js:13:1)
    at Module._compile (internal/modules/cjs/loader.js:1251:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
    at Module.load (internal/modules/cjs/loader.js:1100:32)
    at Function.Module._load (internal/modules/cjs/loader.js:962:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:19696) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19696) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

解决方案

方案1:升级Node.js版本

Hapi 21.x版本要求Node.js最低版本为v14.10.0或v16.0.0及以上,eventLoopUtilization是Node.js v14.10.0才新增的API,当前使用的Node.js版本低于该要求,升级到符合要求的版本即可解决问题。

方案2:降级Hapi到兼容版本

如果暂时不想升级Node.js,可以将Hapi降级到20.x版本(支持Node.js v12.16.0+):

  • 执行卸载命令:npm uninstall @hapi/hapi
  • 安装指定版本:npm install @hapi/hapi@20.2.2
  • 重新启动服务:npm run start

额外优化

给init函数添加错误捕获,避免未处理的Promise拒绝警告,修改server.js的init调用部分:

init().catch(err => {
    console.error('服务启动失败:', err);
    process.exit(1);
});

内容的提问来源于stack exchange,提问作者Owen Lie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 07:35:26