Heroku 部署失败:无法加载 'package.json' 中声明的插件 'cypress':找不到模块 'eslint-plugin-cypress'

Heroku deployment failing: Failed to load plugin 'cypress' declared in 'package.json': Cannot find module 'eslint-plugin-cypress'

经过更多研究后更新#2

进一步查看构建日志,看起来 Cypress 是通过安装后挂钩安装的,该挂钩下载了 Heroku 出于某种原因在构建过程中未获取的二进制文件,除非配置设置为 NPM_CONFIG_PRODUCTION=false.比较 failed build log with the successful build log,这个安装后挂钩没有 运行:

> cypress@6.0.0 postinstall /tmp/build_0453cc7d/frontend/node_modules/cypress

为什么这个和可能的其他 devDependencies 没有在 npm run build 之前安装似乎没有记录并且 运行s 与 Heroku 的文档相反 devDependencies 默认安装。

使用更多构建信息更新 #1

这是显示失败构建日志和后续构建日志的要点(在将 Heroku 配置为使用 heroku config:set NPM_CONFIG_PRODUCTION=false 不 p运行e devDependencies 之后)。

https://gist.github.com/ddehart/9e0ca72a3f20f104e05d70eed6de6c64

p运行ing 似乎是在构建过程安装 Cypress 之后启动的,因此不清楚为什么设置 NPM_CONFIG_PRODUCTION=false 会提示安装 Cypress,尽管 Heroku 的文档说:

By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.

原题

我在部署到 Heroku 时遇到这个 npm run build 错误:

Failed to load plugin 'cypress' declared in 'package.json': Cannot find module 'eslint-plugin-cypress'

这是我的package.json供参考:

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.6",
    "@testing-library/react": "^11.2.2",
    "@testing-library/user-event": "^12.2.2",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "^4.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "cypress:open": "cypress open",
    "cypress:run": "cypress run"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "plugin:cypress/recommended"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@testing-library/cypress": "^7.0.2",
    "cypress": "^6.0.0",
    "eslint-plugin-cypress": "^2.11.2"
  }
}

该项目是使用 Create React App 启动的。 npm run build 在本地成功,没有任何问题,所以我首先想到也许 Heroku 正在 p运行ing devDependencies,但回顾其他成功的构建日志表明 p运行ing 发生了成功构建后。

如果有帮助,这是我上次成功构建的 package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.14.0",
    "react-dom": "^16.14.0",
    "react-scripts": "3.4.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "cypress:open": "cypress open"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "plugin:cypress/recommended"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "cypress": "^5.4.0",
    "eslint-plugin-cypress": "^2.11.2"
  }
}

认为 构建正在抱怨这一点,尽管与上次成功的构建相比根本没有改变。

  "eslintConfig": {
    "extends": [
      "react-app",
      "plugin:cypress/recommended"
    ]
  },

删除该插件声明当然会破坏我 IDE 中的 Cypress。有什么方法可以解决构建错误或对 Heroku 实例本身进行故障排除吗?

我没有设置 NPM_CONFIG_PRODUCTION=false,而是将 Cypress eslintConfig 移动到 cypress 目录中它自己的 .eslintrc.json 文件中。由于 Heroku 似乎只挂在主要 package.json 中引用的 Cypress 插件上,删除该配置完全解决了 Heroku 构建问题,而不会影响我的 IDE 并且不必依赖非标准NPM_CONFIG_PRODUCTION 设置。

根据 Cypress's documentation.eslintrc.json 文件仅需要以下推荐规则:

{
  "extends": [
    "plugin:cypress/recommended"
  ]
}