如何阻止Heroku尝试安装Husky

56lgkhnf  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(139)

我在Heroku上有一个节点应用程序(React),部署和构建以前工作正常,现在我已经将Husky、Lint阶段、Craco和WDYR添加到一个在本地工作的新分支中。
当我尝试在Heroku上部署和构建这个分支时,我发现构建失败,因为Heroku试图安装Husky,尽管我没有要求它安装,而且它是一个开发工具。Heroku找不到Husky是有道理的,因为它是一个开发依赖项。
从我所看到的,Heroku正在运行客户端package.json中的prepare脚本,这是不应该的。
如何阻止Heroku安装Husky?
这里是我的package.json文件(没有包括服务器package.json,因为我不确定它是否相关)。

{
    "engines": {
        "node": "12.x"
    },
    "scripts": {
        "start": "cd server && node server.js",
        "build": "cd client && npm run build",
        "install-server": "cd server && npm ci",
        "install-client": "cd client && npm ci",
        "heroku-postbuild": "npm run install-server && npm run install-client && npm run build"
    }
}

委托人

{
    "name": "qupp",
    "version": "1.0.0",
    "description": "App that allows users to create playlists using Spotify's API",
    "engines": {
        "node": "12.x"
    },
    "scripts": {
        "build:css": "postcss src/App.scss -o src/index.css",
        "watch:css": "postcss src/App.scss -o src/index.css -w",
        "start_new": "PORT=3002 craco start",
        "start": "npm run watch:css & PORT=3002 craco start",
        "build": "npm run build:css && craco build",
        "test": "craco test",
        "eject": "react-scripts eject",
        "prepare": "cd ../ && husky install ./client/.husky"
    },
    "proxy": "http://127.0.0.1:8082/",
    "author": "Daniel Blythe",
    "license": "ISC",
    "dependencies": {
        "@craco/craco": "^6.3.0",
        "@types/jest": "^24.0.17",
        "@types/materialize-css": "^1.0.6",
        "@types/node": "^12.7.1",
        "@types/react": "^16.9.1",
        "@types/react-dom": "^16.8.5",
        "axios": "^0.18.0",
        "babel-preset-es2015-node6": "^0.4.0",
        "classnames": "^2.2.6",
        "dotenv": "^6.1.0",
        "firebase": "^8.2.4",
        "http2": "^3.3.7",
        "jwt-decode": "^2.2.0",
        "morgan": "^1.9.1",
        "node-pre-gyp": "^0.11.0",
        "node-sass": "^4.9.4",
        "postcss-cli": "^6.1.3",
        "prop-types": "^15.6.2",
        "ramda": "^0.27.1",
        "re-base": "^4.0.0",
        "react": "^16.8.4",
        "react-dom": "^16.8.4",
        "react-dropzone": "^10.0.0",
        "react-materialize": "^3.3.3",
        "react-redux": "^6.0.0",
        "react-router-dom": "^4.3.1",
        "react-scripts": "4.0.3",
        "react-spotify-player": "^1.0.4",
        "react-transition-group": "^2.5.0",
        "redux": "^4.0.1",
        "redux-thunk": "^2.3.0",
        "source-map-support": "^0.5.9",
        "typescript": "^3.5.3"
    },
    "devDependencies": {
        "@types/classnames": "^2.2.9",
        "@types/react-redux": "^7.1.2",
        "@welldone-software/why-did-you-render": "^6.2.1",
        "autoprefixer": "^9.8.6",
        "enzyme": "^3.9.0",
        "enzyme-adapter-react-16": "^1.13.0",
        "enzyme-to-json": "^3.3.5",
        "eslint-config-prettier": "^8.3.0",
        "eslint-plugin-no-autofix": "^1.1.2",
        "eslint-plugin-prettier": "^3.4.1",
        "husky": "^7.0.2",
        "jest-dom": "^3.2.2",
        "lint-staged": "^11.1.2",
        "postcss": "^7.0.36",
        "postcss-cli": "^6.1.2",
        "prettier": "^2.3.2",
        "pretty-quick": "^3.1.1",
        "react-test-renderer": "^16.8.6",
        "react-testing-library": "^7.0.0",
        "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.14"
    },
    "jest": {
        "snapshotSerializers": [
            "enzyme-to-json/serializer"
        ]
    },
    "browserslist": [
        ">0.2%",
        "not dead",
        "not ie <= 11",
        "not op_mini all"
    ]
}

以下是错误的屏幕截图:

ppcbkaq5

ppcbkaq51#

以下是最佳解决方案:
运行npm ci --only=production --ignore-scripts
https://github.com/typicode/husky/issues/920
以下是从Heroku支持票(我没有检查这个信息):
要覆盖--only=production标志,可以设置环境变量NPM_CONFIG_PRODUCTION=true https://devcenter.heroku.com/articles/nodejs-support#only-installing-dependencies
虽然不可能直接将--ignore-scripts标志添加到install命令中,但可以通过npm config将npm config set ignore-scripts设置为true来设置它,这将在安装过程中实现相同的效果。
可以使用heroku特定的构建步骤来包括上述命令。

"scripts": {
  "heroku-prebuild": "npm config set ignore-scripts true",
  ...
}

替代方案(但它有一个缺点):
通过设置环境变量NPM_CONFIG_PRODUCTION=trueYARN_PRODUCTION=true,可以指示Heroku仅安装依赖项
请访问https://devcenter.heroku.com/articles/nodejs-support#only-installing-dependencies
这样你就不会安装devDependencies,假设husky在devDependencies中(为什么不呢?),这意味着husky永远不会安装。
缺点是它不会与任何其他devDependencies安装,也就是说,我有一个webpack,这是需要运行的构建步骤。我不想把它从devDependencies到依赖关系,所以我不能使用这个解决方案。

相关问题