如何在打字稿中等待最高级别(从commonjs切换到esnext),而无需更改所有导入以使.js结束
我想在我的打字稿nodejs项目中等待最高水平。
我的tsconfig曾经看起来像这样:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
我现在将其切换到
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
我还添加了“ type”:“ module”
到我的软件包。实际上,我现在有能力进行顶级等待,但是
- 我需要更改所有导入,以添加
- 文件夹的.js文件扩展名,其中添加了index.ts.ts来导出我以前只能导入文件夹名称的所有模块。现在,我需要导入foldername/index.js时
- ,当我自动添加与vscode添加导入时,它会添加它而没有.js
的方式是如此优雅 - 我可以使用Esnext具有相同的行为,或者在某种程度上保持其他方式获得顶级等待?
I would like to have top level await in my typescript nodejs project.
My tsconfig used to look like this:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
And I now switched it to
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
I have also added "type": "module"
to my package.json. Indeed I now have the ability to do top level awaits however
- I need to change every import to add the .js file extension
- For folders where I added an index.ts to export all the modules I could previously just import the folder name. Now I need to import foldername/index.js
- When I auto add an import with vscode it adds it without the .js
The way it is with commonjs is so elegant - can I have the same behaviour with esnext or keep it some other way while gaining top-level await?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您的tsconfig.json文件的外观示例:
Here's an example of what your tsconfig.json file might look like:
我不知道这是否是一个选项,但是我认为最简单的方法是将应用程序的入口包装到某种
main()
函数中,该功能是异步并执行该功能的。在此功能中,您可以像想要的那样使用等待。I don't know if that is an option, but i think the easiest approach would be, to wrap the entrance of your application into some kind of
main()
function that is async and execute that function. Inside this function, you can use await like you want.要在Node.js中使用ES模块的顶级等待,您必须在导入中包括
.js
扩展。但是,可以通过使用webpack bundler
来避免这种情况,以处理无需扩展的导入,并将其添加到webpack.config.js
文件中:希望它会有所帮助!
To use top-level await with ES modules in Node.js, you must include
.js
extensions in imports. But this can be avoided by using awebpack bundler
to handle imports without extensions and add this inwebpack.config.js
file:Hope it helps!