You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript Worker线程报错:无法重声明块级变量lodash_1

TypeScript Worker线程报错:Cannot redeclare block-scoped variable 'lodash_1'

我在TypeScript项目中实现Worker线程时遇到了无法解决的错误,项目整体编译运行正常,共享函数和服务类都能正常使用,但Worker线程部分报错。

错误信息:

Cannot redeclare block-scoped variable 'lodash_1'.

实现细节

main.ts 启动Worker的代码

worker = new Worker(path.resolve(__dirname, './workerStart.js'), {
            workerData: {
              path: './worker.ts',
              ...dataNeeded
            }
          });

workerStart.js 代码

/* eslint-disable semi */
const path = require('path');
const { workerData } = require('worker_threads');
require('ts-node').register();
require(path.resolve(__dirname, workerData.path));

worker.ts 代码

import { CONFIG_SERVICE_FROM_FILE_KEY } from '@server/common/config/CommonConfiguration';
import { loadConfiguration } from '@server/common/config/loadConfiguration';
import { initializeAllDaos } from '@server/common/dao/initializeDao';
import { parentPort, workerData } from 'worker_threads';
import { ProfilerService } from '../../profiler.service';

runThread();

async function runThread() {
  const {
    serverConfig
  } = workerData;

  await initDao(serverConfig);
}

async function initDao(config) {
  // 调用此函数时出错
  const daoList = await initializeAllDaos(config.dao);
  console.log(config); // 在此之前抛出错误
  return daoList;
}

tsconfig.json 相关配置

{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "es6",
    "module": "commonjs",
    "esModuleInterop": true,
    "sourceMap": true,
    "moduleResolution": "node",
    "outDir": "dist",
    "resolveJsonModule": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "typeRoots": ["node_modules/@types"],
    "paths": {
      "@environment": ["./environment.ts"],
      "@server/*": ["./server/*"],
      "@swaggerAPIConfig": ["./swaggerConfig.json"],
      "@dao-core-db/*": ["./structures/dao/db/*"],
      "@dao-db-models/*": ["./structures/dao/db/models/*"],
      "@dao-db-models-sets/*": ["./structures/dao/db/models-sets/*"],
      "@dao-db-models-doc/*": ["./server/api/types/*"],
      "@docs-tools/*": ["./docs/tools/*"],
      "@tests/*": ["tests/*"]
    }
  }
}

我推测问题出在Worker没有加载我的tsconfig配置,请问如何让Worker正确加载tsconfig来解决这个错误?

内容的提问来源于stack exchange,提问作者Thibaud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:45:35