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

NX工作区搭配TypeScript使用cucumber-js测试时找不到共享库模块

问题根因

ts-node 默认不会解析 tsconfig.json 中配置的 paths 路径别名,NX 开发运行时内置了别名解析逻辑,所以直接运行项目正常,但手动调用 cucumber-js 时加载的 ts-node/register 没有对应配置,导致无法识别 @proj/* 格式的共享库导入路径。

解决方案

步骤1:安装路径解析依赖

安装 tsconfig-paths 用于支持 ts-node 解析 tsconfig 中的路径别名:

npm install -D tsconfig-paths
# 或用yarn
yarn add -D tsconfig-paths

步骤2:修改cucumber配置

修改根目录下的 cucumber.js 文件,在 proj-api 配置段中新增两项配置:

  • 加载 tsconfig-paths/register 模块启用别名解析
  • 指定 ts-node 使用根目录的 tsconfig 配置

修改后的完整配置如下:

module.exports = {
  'proj-api': [
    'apps/proj-api-e2e/features/**/*.feature',
    '--require-module ts-node/register',
    // 新增以下两行
    '--require-module tsconfig-paths/register',
    '--compiler-options {"tsconfig": "./tsconfig.json"}',
    '--require apps/proj-api-e2e/step-definitions/**/*.ts',
    '--format progress-bar',
    '--publish-quiet',
    '--parallel 0',
    '--exit',
  ].join(' '),
};

可选:兼容ESModule配置

如果修改后仍报错,检查根目录 tsconfig.json 的 compilerOptions.module 配置,若为 esnext,可添加环境变量到运行命令前(你已安装cross-env可直接使用):

cross-env TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}' cucumber-js -p proj-api

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 04:06:02