node.js - 使用worker_threads的 typescript 配置

标签 node.js multithreading typescript web-worker worker-thread

我尝试在 Typescript 中使用 WebWorkers,但遇到了问题。 我已经安装了 @types/node 并更新了所有内容。 “worker_threads”不再是实验性的,而是在 Node.js 的稳定版本中。

这是我的 typescript 配置:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "node",
      "dom-mediacapture-record"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

当我尝试像这样导入我的 typescript 文件时:

import { Worker } from 'worker_threads';

我收到错误:


This dependency was not found:

* worker_threads in ./src/SpeechRecognizer/SpeechRecognizer.ts

To install it, you can run: npm install --save worker_threads
Type checking and linting in progress...
No type errors found
No lint errors found
Version: typescript 3.6.3, tslint 5.20.0

我也使用 Vue.js 作为前端。 我已经尝试了我能找到的一切,所以我很高兴得到每一个帮助。 如果这不起作用,我可能会切换到 JavaScript。 谢谢。

最佳答案

您可以使用 npm 包“worker-loader”。转到下面的链接https://www.npmjs.com/package/worker-loader并引用“与 TypeScript 集成”部分

关于node.js - 使用worker_threads的 typescript 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258924/

相关文章:

node.js - 如何在Hostgator VPS上设置基于node.js和socket.io的应用程序

node.js - 通过 Facebook Graph Node JS 发布帖子

javascript - 从javascript代码设置堆内存大小?

javascript - 在nodejs、express和angular1.x中动态上传指定路径的所有文件

c++ - 多线程排挤其他进程

angular - 具有外观轮廓的 mat-form-field 效果不佳

c++ - 如果线程涉及互斥锁定和解锁之间的循环,则在 C++ 中的 2 个线程之间切换执行

c++ - 在并行循环中添加 double - std::atomic<double>

node.js - 如何将 'import' 第三方 Javascript 文件写入 IntelliJ,以便我可以在 Typescript 文件中引用它

CSS 样式 Angular 2+(痛在屁股上)