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

Appium+WebdriverIO v8配置加载失败:ESM模块错误求助

解决WebdriverIO v8配置加载失败(require is not defined in ES module scope)问题

问题描述

使用Appium搭配WebdriverIO v8及Mocha框架搭建自动化测试,启动Android模拟器后执行npx wdio命令,出现配置加载失败错误:

2023-01-05T10:40:45.674Z ERROR @wdio/config:ConfigParser: Failed loading configuration file: file:///Users/xxx/Documents/webdriverio-appium-v8/wdio.conf.js: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/xxx/Documents/webdriverio-appium-v8/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/xxx/Documents/webdriverio-appium-v8/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///Users/xxx/Documents/webdriverio-appium-v8/wdio.conf.js:1:14
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

当前capabilities配置片段:

const path = require ('path');

capabilities: [{    
        'appium:platformName': 'Android',
        'appium:platformVersion': '12.0',
        'appium:deviceName:': 'Pixel 6 Android ver.12',
        'appium:automationName': 'UiAutomator2',
        'appium:app':path.join (process.cwd(), 'webdriverio-appium-v8/app/android/ApiDemos-debug.apk')
        
    }],

解决方案

方案1:切换为ES模块语法

项目package.json中设置了"type": "module",所有.js文件默认以ES模块解析,需将require替换为import:

import path from 'path';

capabilities: [{    
        'appium:platformName': 'Android',
        'appium:platformVersion': '12.0',
        'appium:deviceName:': 'Pixel 6 Android ver.12',
        'appium:automationName': 'UiAutomator2',
        'appium:app':path.join(process.cwd(), 'webdriverio-appium-v8/app/android/ApiDemos-debug.apk')
        
    }],

方案2:修改配置文件后缀为.cjs

若想保留CommonJS语法,直接将wdio.conf.js重命名为wdio.conf.cjs,Node.js会自动将其识别为CommonJS模块。执行命令时需指定新的配置文件:

npx wdio run wdio.conf.cjs

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 22:16:39