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

使用Appium运行Android Studio模拟器时遇Object.hasOwn报错求助

Node v16下运行WDIO命令报错:TypeError: Object.hasOwn is not a function

我在JavaScript项目中使用Appium运行Android Studio模拟器,项目基于Node v16.14.0(npm v8.3.1)。更新依赖后执行命令./node_modules/.bin/wdio ./config/wdio.android.app.conf.js时,出现TypeError: Object.hasOwn is not a function错误,报错源自deepmerge-ts模块。系统环境为MacOS Ventura 13.5(Apple M1 Pro)。

完整报错栈

warning ../../../../package.json: No license field
$ ./node_modules/.bin/wdio ./config/wdio.android.app.conf.js
file:///Users/XXX/Documents/123/repositorios/app-ui-automation/node_modules/deepmerge-ts/dist/node/index.mjs:245
                .filter(([key, option]) => Object.hasOwn(defaultMergeFunctions, key))
                                                  ^

TypeError: Object.hasOwn is not a function
    at file:///Users/XXX/Documents/123/repositorios/app-ui-automation/node_modules/deepmerge-ts/dist/node/index.mjs:245:51
    at Array.filter (<anonymous>)
    at getUtils (file:///Users/XXX/Documents/123/repositorios/app-ui-automation/node_modules/deepmerge-ts/dist/node/index.mjs:245:18)
    at deepmergeCustom (file:///Users/XXX/Documents/123/repositorios/app-ui-automation/node_modules/deepmerge-ts/dist/node/index.mjs:223:19)
    at file:///Users/XXX/Documents/123/repositorios/app-ui-automation/node_modules/webdriver/build/utils.js:10:19
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Process finished with exit code 1

package.json依赖配置

"dependencies": {
    "@babel/cli": "^7.22.10",
    "@babel/core": "^7.22.10",
    "@babel/node": "^7.22.10",
    "@babel/polyfill": "^7.12.1",
    "@babel/preset-env": "^7.22.10",
    "@babel/register": "^7.22.5",
    "@popperjs/core": "^2.10.0",
    "@types/cucumber": "^6.0.1",
    "@types/fibers": "^3.1.1",
    "@types/node": "^12.20.55",
    "@types/webdriverio": "^5.0.0",
    "@wdio/allure-reporter": "^8.14.0",
    "@wdio/cli": "^8.14.4",
    "@wdio/cucumber-framework": "^8.14.0",
    "@wdio/local-runner": "^8.14.3",
    "@wdio/spec-reporter": "^8.14.0",
    "@wdio/sync": "^7.27.0",
    "allure-commandline": "^2.13.0",
    "aws-sdk": "^2.1010.0",
    "axios": "^0.24.0",
    "bootstrap": "^5.1.0",
    "bufferutil": "^4.0.1",
    "bundled-dependencies": "^2.1.0",
    "card-validator": "^8.1.1",
    "chai": "^4.2.0",
    "creditcard-generator": "^0.0.7",
    "cucumber": "^6.0.4",
    "cucumber-html-reporter": "^5.5.0",
    "debug": "^4.1.1",
    "dotenv": "^10.0.0",
    "eslint": "^8.12.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-config-prettier": "^6.5.0",
    "eslint-plugin-import": "^2.24.2",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.25.1",
    "eslint-plugin-react-hooks": "^4.2.0",
    "fibers": "^5.0.3",
    "fs": "^0.0.1-security",
    "husky": "^3.0.9",
    "ipfs-http-client": "^51.0.0",
    "native-fetch": "^3.0.0",
    "nconf": "^0.10.0",
    "node": "^16.14.0",
    "node-fetch": "^3.0.0",
    "node-fetch-npm": "^2.0.4",
    "npm": "^7.21.1",
    "package-json": "^2.3.1",
    "prettier": "^1.19.1",
    "react": "^17.0.2",
    "request": "^2.88.2",
    "swiper": "^6.8.4",
    "utf-8-validate": "^5.0.2",
    "webdriverio": "^7.11.1",
    "ws": "^7.4.6",
    "xmlhttprequest": "^1.8.0"
  },
  "devDependencies": {
    "npm-bundle": "^3.0.3"
  }
解决方案
  • 问题根源:Object.hasOwn是Node v16.9.0新增的API,你的Node版本理论上支持,但当前项目存在两个核心问题:一是WDIO系列依赖版本不统一(webdriverio用v7,而@wdio/cli等用v8),导致引入了不兼容的deepmerge-ts新版本;二是Babel未正确配置polyfill覆盖该API。

  • 修复步骤:

    1. 统一WDIO版本:将所有@wdio/*依赖版本统一为v7.x(与webdriverio@7.11.1匹配),比如把@wdio/cli、@wdio/cucumber-framework等都改为^7.27.0(和@wdio/sync版本一致),避免跨版本冲突。
    2. 锁定兼容的deepmerge-ts版本:如果不想降级WDIO,直接在package.json中添加"deepmerge-ts": "^4.3.0"(该版本不使用Object.hasOwn,兼容Node v16),然后重新安装依赖。
    3. 配置Babel polyfill:修改babel.config.json,确保@babel/preset-env启用corejs polyfill:
      {
        "presets": [
          ["@babel/preset-env", {
            "useBuiltIns": "usage",
            "corejs": 3
          }]
        ]
      }
      
      同时安装corejs:npm install corejs@3
    4. 清理依赖缓存:删除node_modules和package-lock.json/yarn.lock,执行npm install或yarn install重新安装依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:30:53