如何使用顶级等待将 TypeScript 编译为 JavaScript?
How to compile TypeScript to JavaScript with top-level await?
我想在 NodeJS 应用程序中使用自 TypeScript 3.8 以来可用的顶级等待。
因此,以下 TypeScript 代码:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
编译成这个 JavaScript 代码:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
//# sourceMappingURL=index.js.map
当我尝试在 WebStorm 中 运行 它时,出现以下错误:
(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)
最新的NodeJS 0.13.*支持吗?
我的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"
]
}
和package.json包含"type": "module"
如评论中所述,此功能不仅取决于 TS 的版本,还取决于 WebPack 的版本(如果使用 WebPack)以及程序执行期间是否存在 V8 标志 --js-flags="--harmony-top-level-await"
或在配置中。
在启用此标志的情况下执行此代码可以按照评论中所述进行:
node -r dotenv/config --harmony-top-level-await --es-module-specifier-resolution=node dist/index.js
我想在 NodeJS 应用程序中使用自 TypeScript 3.8 以来可用的顶级等待。
因此,以下 TypeScript 代码:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
编译成这个 JavaScript 代码:
import { getDoctorsPage } from "./utils/axios.provider";
const page = await getDoctorsPage("047", "1", "1");
console.log(page);
//# sourceMappingURL=index.js.map
当我尝试在 WebStorm 中 运行 它时,出现以下错误:
(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)
最新的NodeJS 0.13.*支持吗?
我的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"
]
}
和package.json包含"type": "module"
如评论中所述,此功能不仅取决于 TS 的版本,还取决于 WebPack 的版本(如果使用 WebPack)以及程序执行期间是否存在 V8 标志 --js-flags="--harmony-top-level-await"
或在配置中。
在启用此标志的情况下执行此代码可以按照评论中所述进行:
node -r dotenv/config --harmony-top-level-await --es-module-specifier-resolution=node dist/index.js