Cypress 10+集成@badeball/cucumber插件后与cy-verify-downloads冲突报错
解决Cypress 10+中@badeball/cypress-cucumber-preprocessor与cy-verify-downloads的模块解析冲突
问题根源
报错中的path、fs、crypto是Node.js内置模块,而@badeball/cypress-cucumber-preprocessor依赖的esbuild预处理器默认以浏览器环境打包代码,会尝试解析这些仅存在于Node环境的模块,从而触发解析错误。同时你的配置里存在冗余代码(重复引入createBundler、错误的module.exports块),进一步加剧了冲突。
解决步骤
1. 清理冗余配置代码
删掉setupNodeEvents内重复的createBundler引入和多余的module.exports代码块,这部分内容完全冗余,且会覆盖正确的预处理器配置。
2. 配置esbuild忽略Node内置模块
在创建bundler时,通过external选项标记Node.js内置模块为外部依赖,让esbuild不尝试打包它们,留到运行时由Node环境直接解析。
3. 修正后的完整配置代码
import { defineConfig } from 'cypress'; const { verifyDownloadTasks } = require('cy-verify-downloads'); import createBundler from "@bahmutov/cypress-esbuild-preprocessor"; import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor"; import createEsbuildPlugin from "@badeball/cypress-cucumber-preprocessor/esbuild"; export default defineConfig({ projectId: '7emkc5', reporter: 'mochawesome', reporterOptions: { reportDir: 'cypress/report/mochawesome-report', overwrite: false, html: true, json: true, timestamp: 'dd-mm-yyyy_HH-MM-ss', }, chromeWebSecurity: false, e2e: { async setupNodeEvents( on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions ): Promise<Cypress.PluginConfigOptions> { // 注册文件下载验证任务 on('task', verifyDownloadTasks); // 添加Cucumber预处理器插件 await addCucumberPreprocessorPlugin(on, config); // 配置esbuild预处理器,忽略Node内置模块 on( "file:preprocessor", createBundler({ plugins: [createEsbuildPlugin(config)], // 标记Node内置模块为外部依赖,避免esbuild尝试解析 external: ['fs', 'path', 'crypto'] }) ); return config; }, specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx,feature}', }, })
额外说明
external选项的作用是告诉esbuild,这些模块不需要被打包进浏览器端代码,而是在运行时由Node.js环境提供,刚好匹配cy-verify-downloads在Node端执行任务的需求。- 确保所有依赖版本与当前Cypress 10+版本兼容,若仍有问题可尝试更新
@badeball/cypress-cucumber-preprocessor、@bahmutov/cypress-esbuild-preprocessor、cy-verify-downloads到最新兼容版本。
内容的提问来源于stack exchange,提问作者Ivan Markov
相关产品推荐
相关产品推荐

