Is it possible to simultaneously use imports (import x from y
) and top-level awaits with ts-node? I encountered an issue where changing my tsconfig.compilerOptions.module
to es2017
or higher, as required by top-level awaits, resulted in the following error:
SyntaxError: Cannot use import statement outside a module
The solution suggested by numerous GitHub issues and Stack Overflow questions is to set tsconfig.compilerOptions.module
to commonjs
. However, this then leads to another issue:
Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher
How can both conditions be met simultaneously? There must be a way...
Here's my tsconfig.json:
{
"compilerOptions": {
"declaration": true,
"module": "esnext",
"target": "es2017",
"moduleResolution": "node",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist",
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/*.ts"]
}
And my package.json:
{
"name": "x",
"version": "0.0.1",
"main": "main.js",
"type": "module",
...
}
I am using Node LTS (v16.14.2) and TypeScript 4.6.3.