javascriptnode.jstypescriptpromise

Warning: Detected unsettled top-level await


The following code results in error Warning: Detected unsettled top-level await. I don't understand why all of a sudden this block is throwing error when it was working just an hour ago. Didn't make any changes to my dev environment.

I think that the dynamic import promise or startTunnel promise is unsettled that's why this error is thrown, but what I don't understand why an hour later this code is throwing error when it was working perfectly fine before.

I know that the message says Warning, but the process exits right after printing this warning.

let appUrl: string | undefined = isProd
  ? process.env.APP_URL
  : await (async () => {
      const { default: startTunnel } = await import("./lib/startTunnel.js");
      const url = await startTunnel(PORT);
      return url;
    })();

Environment:

// tsconfig.json

// https://www.totaltypescript.com/tsconfig-cheat-sheet
{
  "compilerOptions": {
    /* Base Options: */
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,

    /* Strictness */
    "strict": true,
    "noUncheckedIndexedAccess": true,

    /* If transpiling with TypeScript: */
    "module": "NodeNext",
    "outDir": "dist",
    "sourceMap": true,

    /* If your code doesn't run in the DOM: */
    "lib": ["es2022"]
  }
}

startTunnel.ts

import { startTunnel } from "untun";


export default async (port: number) => {
  try {
    const tunnel = await startTunnel({ port });

    if (!tunnel) throw new Error("unable to start tunnel");

    const tunnelUrl = await tunnel.getURL();

    return tunnelUrl;
  } catch (error) {
    console.error((error as Error).message);
  }
};

Edit: added screenshot

enter image description here


Solution

  • Ok after spending some time with this I found that the error was occuring because: