默认情况下,Heroku将安装 package.json 中 dependencies 和 devDependencies 下列出的所有依赖项. 原始问题 在部署到Heroku时出现 npm run build 错误: 无法加载在"package.json"中声明的插件"cypress":找不到模块"eslint-plugin-cypress" 这是我的 package.json 供参考: {"name":"frontend",版本":"0.1.0&",私人":是的,依赖项":{"@ 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",反应脚本":"^ 4.0.1";},脚本":{"start":"react-scripts start","build":"react-scripts build","test":"react-scripts test",弹出":反应脚本弹出","cypress:open":"cypress open","cypress:run":"cypress run";},"eslintConfig":{扩展":["react-app",插件:柏树/推荐";]},浏览器列表":{生产":[> 0.2%",未死",不是op_mini全部";],发展":[最后1个镀铬版本",最新的1个Firefox版本",最后1个野生动物园版本"]},"devDependencies":{"@ testing-library/cypress":"^ 7.0.2","cypress":"^ 6.0.0&","eslint-plugin-cypress":"^ 2.11.2";}} 该项目是使用Create React App启动的. npm run build 成功在本地完成,没有任何问题,所以我首先想到Heroku正在修剪 devDependencies ,但是回顾其他成功的构建日志,则表明修剪在成功构建后发生如果有帮助,这是我上次成功构建的 package.json . {"name":"frontend",版本":"0.1.0&",私人":是的,依赖项":{"@ 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&",反应脚本":"3.4.3";},脚本":{"start":"react-scripts start","build":"react-scripts build","test":"react-scripts test",弹出":反应脚本弹出","cypress:open":"cypress open"},"eslintConfig":{扩展":["react-app",插件:柏树/推荐";]},浏览器列表":{生产":[> 0.2%",未死",不是op_mini全部";],发展":[最后1个镀铬版本",最新的1个Firefox版本",最后1个野生动物园版本"]},"devDependencies":{"cypress":"^ 5.4.0&","eslint-plugin-cypress":"^ 2.11.2";}} 我认为,尽管自上次成功构建以来,这并没有改变,但该构建仍在抱怨这一点. "eslintConfig":{扩展":["react-app",插件:柏树/推荐";]}, 删除该插件声明当然会在我的IDE中破坏Cypress.有什么方法可以解决构建错误或对Heroku实例本身进行故障排除吗?解决方案我没有设置 NPM_CONFIG_PRODUCTION = false ,而是最终将Cypress eslintConfig 移到了自己的 cypress 目录中的> .eslintrc.json 文件.由于Heroku似乎只是挂在主 package.json 中引用的Cypress插件上,因此完全删除该配置可解决Heroku构建问题,而不会影响我的IDE,而不必依赖于非标准的 NPM_CONFIG_PRODUCTION 设置.每个赛普拉斯的文档, .eslintrc.json 文件只需要使用此规则即可: {扩展":[插件:柏树/推荐";]} Update #2 after some more researchLooking into the build logs some more, it looks like Cypress gets installed via a postinstall hook that downloads a binary that Heroku doesn't pick up in the build process for some reason unless config is set to NPM_CONFIG_PRODUCTION=false. Comparing the failed build log with the successful build log, this postinstall hook doesn't run:> [email protected] postinstall /tmp/build_0453cc7d/frontend/node_modules/cypressWhy this and potentially other devDependencies don't get installed prior to npm run build doesn't appear to be documented and runs contrary to Heroku's documentation that devDependencies are installed by default.Update #1 with more build infoHere's a gist that shows the failing build log and the succeeding build log (after configuring Heroku not to prune devDependencies using heroku config:set NPM_CONFIG_PRODUCTION=false).https://gist.github.com/ddehart/9e0ca72a3f20f104e05d70eed6de6c64The pruning appears to be initiated after the build process installed Cypress, so it's not clear why setting NPM_CONFIG_PRODUCTION=false prompted the Cypress installation, despite Heroku's documentation that says:By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.Original questionI'm getting this npm run build error when deploying to Heroku:Failed to load plugin 'cypress' declared in 'package.json': Cannot find module 'eslint-plugin-cypress'Here's my package.json for reference:{ "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" }}The project was initiated using Create React App. npm run build succeeds without any issues locally, so I first thought maybe Heroku was pruning devDependencies, but looking back at other build logs that succeeded show that pruning happens after a successful build.If it helps, here's my package.json from the last successful build.{ "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" }}I think the build is complaining about this, although that hasn't changed at all from the last successful build. "eslintConfig": { "extends": [ "react-app", "plugin:cypress/recommended" ] },Removing that plugin declaration of course ruins Cypress in my IDE. Is there any way to work around the build error or troubleshoot on the Heroku instance itself? 解决方案 Rather than setting NPM_CONFIG_PRODUCTION=false, I ended up moving the Cypress eslintConfig into its own .eslintrc.json file within the cypress directory. Since Heroku only seems to be getting hung up on the Cypress plugin referenced in the main package.json, removing that configuration altogether fixed the Heroku build issue without affecting my IDE and without having to rely on the non-standard NPM_CONFIG_PRODUCTION setting.Per Cypress's documentation, the .eslintrc.json file just needs this for the recommended rules:{ "extends": [ "plugin:cypress/recommended" ]} 这篇关于Heroku部署失败:无法加载在"package.json"中声明的插件"cypress":找不到模块"eslint-plugin-cypress"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 13:12