I would like to utilize top-level await available since TypeScript 3.8 in a NodeJS application.
So, the following TypeScript code:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
compiles to this JavaScript code:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
//# sourceMappingURL=index.js.map
And when I try to run it within WebStorm I get the following error:
(node:95053) ExperimentalWarning: The ESM module loader is experimental.
file:///Users/anatoly/Documents/git/maccabi-parser/dist/index.js:2
const page = await getDoctorsPage("047", "1", "1");
^^^^^
SyntaxError: Unexpected reserved word
at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
at async link (internal/modules/esm/module_job.js:37:21)
Does the latest NodeJS 0.13.* support it?
My tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "esnext",
"pretty": true,
"sourceMap": true,
"outDir": "dist",
"importHelpers": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"rootDir": "src",
"noImplicitAny": false,
"strictNullChecks": false,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist"
]
}
and package.json contains "type": "module"
moduleto becommonjs.--js-flags="--harmony-top-level-await"node -r dotenv/config --harmony-top-level-await --es-module-specifier-resolution=node dist/index.jsit began to work. I was need to add another flag as well--es-module-specifier-resolution=nodeotherwise it didn't work withERR_MODULE_NOT_FOUND. Do you want to write an answer, so I will be able to accept it. It looks that this question could be quite popular