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

使用npx wdio执行测试时遇ts-node/esm钩子错误,求解决方法

解决npx wdio运行时ts-node ESM钩子错误

方案1:调整ts-node版本

  • 卸载当前ts-node:npm uninstall ts-node
  • 安装稳定兼容版本:npm install ts-node@10.9.1 --save-dev

方案2:配置ts-node的shortCircuit参数

在项目的package.json中添加ts-node专属配置,明确开启短路标识:

{
  "ts-node": {
    "esm": true,
    "resolve": {
      "shortCircuit": true
    }
  }
}

也可以在tsconfig.json中添加相同配置:

{
  "ts-node": {
    "esm": true,
    "resolve": {
      "shortCircuit": true
    }
  },
  "compilerOptions": {
    // 保留原有编译配置
  }
}

方案3:修改WDIO配置文件

如果使用ESM格式的WDIO配置文件,在wdio.conf.ts(或wdio.conf.js)中添加ts-node的配置项:

export const config = {
  // 其他原有配置
  autoCompileOpts: {
    tsNodeOpts: {
      esm: true,
      resolve: {
        shortCircuit: true
      }
    }
  }
}

方案4:切换为CommonJS模块格式

若无需使用ESM,可将项目切换回CommonJS规范:

  • 修改package.json中的type字段为commonjs(若之前设为module)
  • 将相关配置文件的后缀改为.cjs,或保持.js后缀即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 00:55:17