RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1567628
Accepted
Vasily
Vasily
Asked:2024-02-23 05:50:56 +0000 UTC2024-02-23 05:50:56 +0000 UTC 2024-02-23 05:50:56 +0000 UTC

如何启用顶级等待

  • 772

大家好。我正在学习打字稿。我读到了有关顶级等待的信息,我试图在节点应用程序中启用它,但它不起作用。互联网上没有任何明智的材料。请帮助。

我做了什么。

  • npm 初始化
  • npm i -D typescript @types/node ts-node
  • 然后他安装了 Nodemon。

打字稿 - 5.3.3

我正在尝试在index.ts 中使用await。

  • 他写信给我:“错误 TS1378:仅当 'module' 选项设置为 'es2022'、'esnext'、'system'、'node16' 或 'nodenext' 时才允许顶级 'await' 表达式,并且‘target’选项设置为‘es2017’或更高。”
  • 在 tsconfig 中,我将目标更改为 es2022,模块更改为 node16。
  • 它开始写“当前文件是 CommonJS 模块,不能在顶层使用‘await’”
  • 我正在尝试将 type: module 插入 package.json 中。
  • 开始写入“未知文件扩展名“.ts”

然后我们就没有想法了。请帮我打败这个废话。

tsconfig.json

  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }

包.json

  "name": "ts-top-await-template",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "dev": "nodemon index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [ ],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^20.11.19",
    "nodemon": "^3.0.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }

我运行 npm run dev

node.js
  • 1 1 个回答
  • 77 Views

1 个回答

  • Voted
  1. Best Answer
    Andrei Khotko
    2024-02-29T06:26:34Z2024-02-29T06:26:34Z

    看起来你和我一样都有Node 20版本。在这里找到了您问题的答案:

    Github ts-node 问题:Node v20.0.0 上的 ERR_UNKNOWN_FILE_EXTENSION

    简要摘录: ts-node 目前与 Node 20 存在兼容性问题。如果您想使用 ts-node 在 Node 20 上运行代码,则必须运行以下命令,这会导致我们无法获得错误发生时的详细堆栈跟踪:

    node --loader ts-node/esm main.ts
    

    因此,要使其正常工作top-level await,您需要执行以下步骤:

    1. 设置tsconfig.json以下选项(其余保持不变):
      "compilerOptions": {
        "target": "es2022",
        "module": "Node16"
      }
      
    2. 安装脚本并将其更改为以下内容 package.json:"type": "module"dev
      "scripts": {
        "dev": "nodemon --exec \"node --loader ts-node/esm index.ts",
      },
      
    3. 执行npm run dev。

    例如,我将在这里留下我的测试index.ts:

    async function waitForCookie(): Promise<string> {
      return new Promise((resolve) => {
        console.log('[INSIDE-FUNC] start cooking a cookie!');
    
        setTimeout(() => {
          console.log('[INSIDE-FUNC] cookie is ready, take it!!');
          resolve('Chocolate COOKIE!');
        }, 3000);
      })
    }
    
    const cookie = await waitForCookie();
    console.log('[TOP-LEVEL] Received a cookie:', cookie);
    

    执行结果:

    > [email protected] dev
    > nodemon --exec "node --loader ts-node/esm index.ts
    
    [nodemon] 3.1.0
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching path(s): *.*
    [nodemon] watching extensions: js,mjs,cjs,json
    [nodemon] starting `node --loader ts-node/esm index.ts`
    (node:26944) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
    --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
    (Use `node --trace-warnings ...` to show where the warning was created)
    [INSIDE-FUNC] start cooking a cookie!
    [INSIDE-FUNC] cookie is ready, take it!!
    [TOP-LEVEL] Received a cookie: Chocolate COOKIE!
    [nodemon] clean exit - waiting for changes before restart
    
    • 7

相关问题

  • 通过掩码 node.js 指定字符串

  • 从节点 js 发布请求声明变量

  • 为什么 BigQuery 查询如此缓慢?

  • 在 Elasticsearch 中使用 Fluentd 进行映射

  • 如何设置 vuejs 和 nodejs 以使用 mssql

  • 如何添加对象而不覆盖它?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5