node.jstypescripthttp-toolkitmockttp

UnhandledPromiseRejectionWarning: /root/mockttp/node_modules/mockttp/dist/server/mockttp-server.js:62


Please bear with me since I'm very new to Linux/Node.js in general :-)

I'm trying to put together the basics to run simple first example of mockttp from this page:

https://httptoolkit.com/blog/javascript-mitm-proxy-mockttp/

I have the basic example:

(async () => {
    const mockttp = require('mockttp');

    // Create a proxy server with a self-signed HTTPS CA certificate:
    const https = await mockttp.generateCACertificate();
    const server = mockttp.getLocal({ https });

    // Inject 'Hello world' responses for all requests
    server.forAnyRequest().thenReply(200, "Hello world");
    await server.start();

    // Print out the server details:
    const caFingerprint = mockttp.generateSPKIFingerprint(https.cert)
    console.log(`Server running on port ${server.port}`);
    console.log(`CA cert fingerprint ${caFingerprint}`);
})(); // (Run in an async wrapper so we can use top-level await everywhere)

but when I run node index.js I get the following error:

(node:13796) UnhandledPromiseRejectionWarning: /root/mockttp/node_modules/mockttp/dist/server/mockttp-server.js:62
                ((_a = this.webSocketRuleSets)[_b = rule.priority] ?? (_a[_b] = [])).push(rule);
                                                                    ^

SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/root/mockttp/node_modules/mockttp/dist/main.js:14:26)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
(node:13796) 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:13796) [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 tried looking into mockttp-server.js but yeah .. that's beyond me so asking the community :-)

Thanks!


Solution

  • This means your Node.js version is very outdated. ?? is relatively new syntax only supported by modern versions. According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing that means Node.js 14+

    You can check which version you're using with node --version.

    You can see the latest LTS (long-term support) and current versions at https://nodejs.org/en. According to https://endoflife.date/nodejs, at the time of writing all versions of Nodejs before v18 are now officially end-of-life and unsupported.