How to stop Heroku from trying to install husky

Viewed 735

I have a Node app(React) on Heroku and the deploy and build was previous working fine, I have now added Husky, Lint-staged, Craco and WDYR to a new branch which works locally.

Upon trying to deploy and build this branch on Heroku, I am finding that the build fails due to Heroku trying to install Husky despite me not asking it to and it being a development tool. It makes sense that Heroku cannot find Husky as it is a dev dependency.

From what I can see Heroku is running the prepare script from the client package.json, which it shouldn't be.

I would like to know how I can stop Heroku from trying to install Husky?

Please help.

Here are my package.json files (haven't included server package.json as not sure it's relevant)

root

{
"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"
}

}

client

{
"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"
]

}

Here is a screenshot of the error enter image description here

1 Answers

Here is the best solution:

run npm ci --only=production --ignore-scripts

https://github.com/typicode/husky/issues/920

Following is from Heroku support ticket (I did not check this info):

To cover the --only=production flag, you can set the environment variable NPM_CONFIG_PRODUCTION=true https://devcenter.heroku.com/articles/nodejs-support#only-installing-dependencies

While it isn’t possible to add the --ignore-scripts flag to the install command directly, it can be set via npm config with npm config set ignore-scripts true and this will achieve the same thing during installation.

The above command could be included using heroku-specific-build steps. e.g.;

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

Alternative solution (but it has a drawback):

You can direct Heroku to only install dependencies by setting environment variables NPM_CONFIG_PRODUCTION=true or YARN_PRODUCTION=true

See https://devcenter.heroku.com/articles/nodejs-support#only-installing-dependencies

This way you won't install devDependencies, assuming husky is in devDependencies (why would it not be?) meaning husky will never install.

The drawback is it won't install with any other devDependencies and i.e. I have there webpack which is required to run on build step. I don't want to move it from devDependencies to dependencies so I cant use this solution.

Related