#ts-node-esm not working with top level await

3 messages · Page 1 of 1 (latest)

ebon wedge
#

whenever i run ts-node with this snippet the code immediately stops after a promise, it doesnt wait for it to be resolved

this is how im running it:
ts-node -P tsconfig.ts-node.json scripts/prerender.ts

the tsconfig:

{
  "compilerOptions": {
    "target": "ESNext",
    "esModuleInterop": true,
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "module": "ESNext",
    "moduleResolution": "nodenext",
  },
  "include": ["server.ts","framework.config.ts"],
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
  }
}

the code

import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

console.log('test')
//logging works here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//@ts-ignore

const config = await import("../framework.config.js").then((r) => r.default);
//execition stops immediately after this promis
// thsi log doesnt work
console.log(config)
const { render } = await import(
  //@ts-ignore
  "../dist/server/entry-server.js"
);
const template = `//placeholder`;

async function main() {
  console.log("starting");
  for await (const { route, component: name } of config.flow) {
    const url = route ?? `/${name}`;
    const { body } = await render({
      url,
      template,
      config,
    });
    const filePath = path.resolve(
      __dirname,
      `../dist/static/${
        url === "/" ? "index.html" : `${url.replace("/", "")}.html`
      }`
    );
    console.log(filePath);
    await fs.writeFile(filePath, body);
  }
  console.log("done");
}

console.log("starting");
await main();
edgy jetty
#

works for me

#

which node version are you using? I'm using 18.16.1