Cannot find module 'fs-extra' 启动测试时出现错误,尽管该模块存在于 cypress 中

Cannot find module 'fs-extra' error when launching tests despite the module is present in cypress

刚遇到这个问题。启动我的测试后,此错误出现在 cypress 上下文中 window。

 Can't start server

The plugins file is missing or invalid.

Your pluginsFile is set to /Users/alexanderti/WebstormProjects/klickly-autotests-js/cypress/plugins/index.js, but either the file is missing, it contains a syntax error, or threw an error when required. The pluginsFile must be a .js or .coffee file.

Please fix this, or set pluginsFile to false if a plugins file is not necessary for your project.

Error: Cannot find module 'fs-extra'
    at Function.Module._resolveFilename (module.js:543:15)
    at Function.Module._load (module.js:473:25)
    at Module.require (module.js:586:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/alexanderti/WebstormProjects/klickly-autotests-js/cypress/plugins/index.js:51:12)
    at Object.<anonymous> (/Users/alexanderti/WebstormProjects/klickly-autotests-js/cypress/plugins/index.js:165:3)
    at Module._compile (module.js:642:30)
    at Object.Module._extensions..js (module.js:653:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:504:12)
    at Function.Module._load (module.js:496:3)
    at Module.require (module.js:586:17)
    at require (internal/module.js:11:18)
    at module.exports (/Users/alexanderti/Library/Caches/Cypress/3.4.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:118:15)
    at Object.<anonymous> (/Users/alexanderti/Library/Caches/Cypress/3.4.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/index.js:9:25)
    at Object.<anonymous> (/Users/alexanderti/Library/Caches/Cypress/3.4.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/index.js:11:3)
    at Module._compile (module.js:642:30)
    at Object.Module._extensions..js (module.js:653:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:504:12)
    at Function.Module._load (module.js:496:3)
    at Function.Module.runMain (module.js:683:10)
    at startup (bootstrap_node.js:196:16)
    at bootstrap_node.js:622:3

一切正常,直到我不小心通过 Webstorm 中的 UI 提示在 package.json 中安装了 fs-exta 模块,然后将其卸载。还删除了 ./node_modules 并重新安装。 但是错误依旧。

我已经通过 npm ls fs-extra 检查了模块的可用性,它显示它存在

─┬ cypress@3.4.0
│ └── fs-extra@5.0.0 
├─┬ mochawesome-merge@2.1.0
│ └── fs-extra@7.0.1 
└─┬ mochawesome-report-generator@4.1.0
  └── fs-extra@7.0.1 

只需将此添加到您的 package.json:

"browser": {
    "fs": false
  }

帮我解决了这个问题。我还添加了两个以避免更多问题:

"browser": {
    "fs": false,
    "path": false,
    "os": false
  }

由 re-installing node_modules 目录修复,但删除了 package-lock.json,也许正因为如此,它正在寻找旧版本。现已修复