javascriptnode.jshttpserverhapi

Hapi HTTP Server Error | NodeJS | UnhandledPromiseRejectionWarning: TypeError: PerfHooks.performance.eventLoopUtilization is not a function


I'm learning backend development using NodeJS with Hapi framework, and I got an error after starting the node with npm run start.

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"
    }
}

Running npm run start on PowerShell:

> 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.

I expect the output should be:

Server berjalan pada http://localhost:5000’

And when I enter curl -X GET http://localhost:5000, it should give this output:

{"statusCode":404,"error":"Not Found","message":"Not Found"}

Solution

  • Problem could be solved with installing Hapi version @hapi/hapi@^20.1.0.

    But that doesn't explain why the above code causes an error on Hapi version @hapi/hapi@^21.0.0 (latest version / default from npm install @hapi/hapi)

    Edit

    Explanation from derpirscher:

    performance.eventLoopUtilization was introduced in node versions v14.10.0 and v12.19.0 respectively. So if you for instance are using node 14.7 or 12.16, that function won't exist yet. And @hapi before version 21 didn't use this particular function, so downgrading resolves that problem

    My node version is v14.8.0
    That's what causing the problem.

    After knowing the problem, I tried installing @hapi/hapi and nvm to latest version and the problem solved.

    I also learned that it's easier to maintain node version with NVM for Windows.