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

部署Camunda Modulator的JS Worker时Node编译报错求助

解决Camunda JS Worker运行时的ES模块错误及代码问题

运行nodejs ./CheckRequestFeasibilityWorker.js时出现以下错误:

(node:27539) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `nodejs --trace-warnings ...` to show where the warning was created)
/JsWorkers/CheckRequestFeasibilityWorker/CheckRequestFeasibilityWorker.js:1
import { Client, logger } from "camunda-external-task-client-js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.15.0

一、解决ES模块语法错误

错误提示已明确给出两种解决方式,任选其一即可:

方式1:修改package.json

在项目根目录的package.json中添加"type": "module"字段:

{
  "type": "module",
  "dependencies": {
    "camunda-external-task-client-js": "^..."
  }
}

方式2:重命名文件扩展名

将CheckRequestFeasibilityWorker.js改为CheckRequestFeasibilityWorker.mjs,然后运行:

nodejs ./CheckRequestFeasibilityWorker.mjs

二、修复代码中的其他问题

代码还有两处错误,不修复会导致运行失败:

  1. Math对象拼写错误:JavaScript中随机数方法是Math.random(),需首字母大写
  2. Variables未导入:Variables需要从camunda-external-task-client-js包中引入

修正后的完整代码:

import { Client, logger, Variables } from "camunda-external-task-client-js";

// 客户端配置
//  - 'baseUrl': 流程引擎地址
//  - 'logger': 自动记录关键事件的工具
const config = { baseUrl: "http://localhost:8080/engine-rest", use: logger };

// 创建自定义配置的客户端实例
const client = new Client(config);

// 订阅主题:'check-request-feasibility'
client.subscribe("check-request-feasibility", async function({ task, taskService }) {
  // 业务逻辑:随机生成可行性结果
  const Is_feasible = Math.random() >= 0.5;
  const processVariables = new Variables();
  processVariables.set("Is_feasible", Is_feasible);
  // 完成任务
  await taskService.complete(task);
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 17:35:33