已配置package.json仍报错:wdio-allure-reporter依赖安装问题排查
你提到已经在package.json里手动添加了wdio-allure-reporter依赖,但还是碰到了这个恼人的报错:
Error: reporter "wdio-allure-reporter" is not installed. Error: SyntaxError: Unexpected token
结合我帮其他开发者排查过的类似问题,这个报错基本逃不出下面几个原因,你可以挨个排查:
依赖根本没装进去
手动改package.json只是把依赖写进了配置文件,并没有实际下载包到本地。你先去项目根目录的node_modules文件夹里找找有没有wdio-allure-reporter这个目录——如果没有,赶紧执行安装命令补装:npm install wdio-allure-reporter --save-dev # 用yarn的话就跑这个 yarn add wdio-allure-reporter --dev版本不兼容踩了坑
这是最常见的原因!WebdriverIO从v8版本开始,官方的Allure reporter已经换成了@wdio/allure-reporter(带组织前缀的包),旧版的wdio-allure-reporter只支持v7及以下版本。
先查下你的WebdriverIO版本:npx wdio --version如果是v8+,那你得换掉依赖包:
npm uninstall wdio-allure-reporter npm install @wdio/allure-reporter --save-dev别忘了同步修改
wdio.conf.js里的reporter配置,把名称改成allure就行,不用带前缀了。配置文件有语法错误
报错里的SyntaxError: Unexpected token已经提示你了——你的wdio.conf.js里肯定有语法问题,比如少了逗号、引号没配对,或者用了当前Node版本不支持的ES6+语法。
重点检查reporter的配置块,比如正确的写法应该是这样:reporters: [ ['allure', { outputDir: 'allure-results', disableWebdriverStepsReporting: true, disableWebdriverScreenshotsReporting: false, }] ]仔细核对括号、逗号,别漏了或者多了。
Node.js版本太老
新版的wdio-allure-reporter(或者@wdio/allure-reporter)对Node版本有要求,如果你的Node还是v14以下的老版本,很可能会出现语法解析错误。建议升级到LTS版本(比如v18或v20),然后重新安装所有依赖。缓存搞的鬼
npm或yarn的缓存有时候会抽风,导致依赖安装不完整。你可以清掉缓存重新装:# npm的清理步骤 npm cache clean --force rm -rf node_modules package-lock.json npm install # yarn的清理步骤 yarn cache clean rm -rf node_modules yarn.lock yarn install
内容的提问来源于stack exchange,提问作者shruti gupta

