sh: husky: command not found - node.js

I've setup a node project with husky but when my collegue tries to run npm install on his Mac he gets the following error :
noa-be#1.0.0 prepare
husky install
sh: husky: command not found
npm ERR! code 127
npm ERR! path /Users/X/Desktop/Workspace/project
npm ERR! command failed
npm ERR! command sh -c husky install
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/X/.npm/_logs/2021-04-12T13_07_25_842Z-debug.log
These are the relevant package.json parts:
{
"scripts": {
"prepare": "husky install"
},
"devDependencies": {
"husky": "^5.2.0",
}
}
I thought this would be enough for husky to be installed when running npm install, but it's not. What am I missing?

If you are using nvm, you might want to create a file called .huskyrc in your home directory and add the following lines of code to it:
~/.huskyrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

I was struggling with the same exact problem for hours. Finally, I could install dependencies and start working on my project by doing this:
Temporarily remove the "prepare": "husky install" script from the package.json file.
Run npm i (npm install). Dependencies installed successfuly.
Add again the "prepare" script that you removed in step 1.
Run again npm i to install the husky git hooks, so husky can do its job from now on.

This error is also thrown by npm ci if the NODE_ENV is set to "production" pre-install

I've been able to solve the problem by upgrading to latest Husky version (7.0.1, from 5.2.0).
Git was also helpful, and told me that the files weren't executables. (Git V 2.24.1)
So I give them executable rights :
chmod +x PATH_TO_HUSKY_FILE
You'll need to execute this command for every hooks

I believe it could be version specific issue. Install version 6, npm i husky#6.0.0 --save-dev, and it should work as the husky doc says.
Apparently, when I did npm i husky --save-dev, it was installing "husky": "^0.8.1" for me for some strange reason, giving me the exact same error: sh: husky: command not found.
Method 1:
Update manually, in your package.json:
{
"scripts": {
"prepare": "husky install",
"create-hook": "husky add .husky/pre-commit \"npm test\"",
}
}
Then, run npm run prepare && npm run create-hook.
It should create .husky directory with .pre-commit file in it.
Method 2:
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npm test"

It worked in my terminal but not in VSCode version control. So had to force quite the vscode app and restarting it worked.

Faced this issue in Github Desktop.
solved it by quit Github Desktop and re-open it.

I was able to fix this by providing an explicit location for husky
"scripts": {
"prepare": "node_modules/.bin/husky-run install"
},

Using Lerna
When I upgraded husky from version 4 to 8 there was information todo first pre commit manually. For this purpose pre-commit bash script was generated in .husky directory.
What I had todo was simply run the command included in this file:
lerna run precommit --concurrency 2 --stream

Related

NPM 7 workspaces - how to install new package in workspace?

If I have a NPM 7 workspace like this:
root
- submodule0
- submodule1
- submodule2
and I navigate to the submodule0 directory and run npm i somepackage it seems to "break" the workspace by creating a new package-lock.json in the submodule0 directory and installing all the dependencies there. In other words, it just does the old behavior that existed before I created the workspace.
I was hoping for a command similar to lerna where I can install a new package in submodule0 from the root. Something like:
npm i somepackage --scope submodule0
So far, the only workaround I can find is to edit the submodule0 package.json and add the somepackage manually. Then run npm i from the root. Obviously this is not ideal because I need to look up the #latest version, navigate to the subdirectory, open the package.json, etc. etc. as opposed to just typing one line in the root.
Workspace support for npm install and npm uninstall was added in npm v7.14.0. You can now just do:
npm i somepackage --workspace=submodule0
Uninstalling modules has been the biggest pain, so this is really exciting. The npm team seems to be slowly adding support to commands one by one. Follow updates here: https://github.com/npm/cli/blob/latest/CHANGELOG.md.
I'm also baffled with why npm workspaces has been released without this functionality.
My current workaround uses the add-dependencies package, which adds dependencies to a declared package.json file, whilst skipping the installation process.
npm i add-dependencies -g
Then, from top level of the monorepo, you can run:
npx add-dependencies ./submodule0/package.json somepackage && npm i
Hopefully a --workspace argument will be added to npm i soon to avoid this faff.
Please refer to the answer of mattwad above if you have NPM v7.14.0 or above
Original answer
I wasn't quite happy with the suggestions, but combined all of them to use it in a npm script without any dependencies:
{
"add": "npm install --package-lock-only --no-package-lock --prefix",
"postadd": "npm install"
}
This can be used like following: npm run add -- submodule0 somepackage
Add only into package.json
U can use this to install package only into package.json ( you don't need external dependencies )
npm i --prefix packages/test --save --package-lock-only --no-package-lock express
followed by npm i to install specified dependency into mono repository root node_modules
Lerna
Also can use lerna to use workspace name to install dependency into
package.json
{
"name": "mono",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"wsi": "function workspaceinstall() { ( scope=$1; shift; lerna exec --scope \"$scope\" -- npm install --package-lock-only --no-package-lock \"$#\") }; workspaceinstall"
},
"author": "",
"license": "ISC",
"workspaces": {
"packages": [
"packages/**"
]
}
}
lerna.json
{
"version": "1.0.0",
"npmClient": "npm",
"packages": ["packages/**"]
}
npm run wsi [workspace name] [dependency name to install]
npm run wsi #workspace/test express
npm run wsi #workspace/test express --save-prod
npm run wsi #workspace/test #types/express --save-dev
wsi script only modify package.json for provided workspace name, to actually install dependencies u have to run npm i
In my case, which is similar to yours, I deleted all dependencies from all the inner projects, deleted also the package-lock.json, and installed everything in the root.
/
node_modules
package.json >> all dependencies
package-lock.json >> the only lock file that exists in the repo
/packages
/A
package.json >> no dependencies
-- no package-lock.json
/B
package.json >> no dependencies
-- no package-lock.json
/C
package.json >> no dependencies
-- no package-lock.json
This way, the node_modules folder ONLY resides on the root, and also the package-lock.json file is in the root.
If I allowed to have each project it's own package-lock.json I started seeing installation and runtime errors (because each project could have its own node_modules and its own version of a dependency).
This is the best way I see it works.
After trying to use the npm install with the --prefix --save --package-lock-only --no-package-lock options, npm always give the the error E404 - Not Found for my own packages of the monorepo that are not yet published to a registry. So even when trying to install external packages it fails because of my current dependencies in the package.json.
To workaround this issue I ended up with a mix of the previous suggestions:
"scripts": {
"add": "add-dependencies $npm_config_scope/package.json",
"postadd": "npm i",
},
"devDependencies": {
"add-dependencies": "^1.1.0"
},
Then I can do:
npm run add --scope=packages/app express
npm run add --scope=packages/core eslint jest -D
This works fine for installing external packages. To install my own packages that lives inside the monorepo, I still have to manually edit the package.json, otherwise I get the package not found error.

npm install everything except named package

An extract from my Dockerfile which builds my Angular app:
# Do other stuff
RUN npm install
# next line executes ng build --prod
RUN npm run build:prod
The npm install is for build purposes. Some of the packages in devDependencies - particularly cypress - take ages to install and are not needed for the build. However, some packages in devDependencies are needed.
Can I, for example, do an npm install everything except cypress?
npm install --only=prod is not an option because some devDependencies are needed.
Thanks in advance for your thoughts.
Just a suggestion. Use install-subset, and can be installed globally with npm install -g install-subset
To use it, you build inclusion lists and exclusion lists for named installation subsets in your package.json like this:
"subsets": {
"build": {
"include": [
"babel-cli",
"dotenv"
]
},
"test": {
"exclude": [
"eslint",
"lint-rules",
"prettier"
]
}
}
Then run install-subset test
This will temporarily rewrite your package.json to not install those packages excluded, then restore it (very similar to how lerna operates), which depending on the packages can save a lot of time and bandwidth.

npm-force-resolutions not working when installing a new package

I'm using the scripts section of the package.json to force resolutions:
"preinstall": "npx npm-force-resolutions"
in the resolutions section, I have entered graceful-fs with a specified version:
"resolutions": {
"graceful-fs": "^4.2.4",
},
When i run npm i everything is installed correctly, the set versions are taken in to account. But later on when I install an additional module, e.g. npm i random-package, my set versions are being thrown away and I endup with graceful-fs#1.2.3 and other low versions in some dependencies.
If I clear the node_modules folder and run npm i again, everything is alright again.
I also tried setting the resolution more specific, like
"resolutions": {
"glob/**/graceful-fs": "^4.2.4",
},
but this doesn't help.
I also tried:
adding the module as dependency, devDependency or peerDependency
using a shrinkwrap and overriding it there
but no luck.
what am I missing?
The best solution for me to automate this was modifying preinstall script as above:
"preinstall": "npm install --package-lock-only --ignore-scripts && npx npm-force-resolutions",
Best way is to change the preinstall script to this:
"preinstall": "([ ! -f package-lock.json ] && npm install --package-lock-only --ignore-scripts --no-audit); npx npm-force-resolutions"
This will only run npm install to create your initial package-lock.json when it does not exist yet.
This is much faster than always running both (npm + npx).
As of npm 8.3.0, you can also use npm's override:
{
"overrides": {
"graceful-fs": "^4.2.4"
}
}
in the resolutions section, you must fix version
"resolutions": {
"graceful-fs": "4.2.4",
},
Hi #NthDegree the only way which worked for me was to first run the normal npm install and then add the packages-lock.json file to git. After doing that when you add "preinstall": "npx npm-force-resolutions", it always updates the dependency resolution to the version mentioned.
I am not sure if adding packages-lock.json file to git is good or bad but by using this method the CI/CD pipeline works as well.
If all of the above answers don't work and you still get sh: npm-force-resolutions: command not found
try the following:
Just change:
"preinstall": "npx npm-force-resolutions"
To:
"preinstall": "npx force-resolutions"
npx force-resolutions does not run when no package-lock.json is detected, and allows the next command inline to be executed as normal
Credit to: https://github.com/rogeriochaves/npm-force-resolutions/issues/10#issuecomment-885458937

"npm run build" = "react-scripts: Permission denied"

I'm trying to deploy my working Windows 10 Spring-Boot/React app on Ubuntu 18.04 but keep getting "react-scripts: Permission denied" error despite numerous attempts to fix. Hopefully one of you react experts can spot what I'm doing wrong.
My package.json looks like this
{
"name": "medaverter-front",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"bootstrap": "^4.4.1",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"react-table-6": "^6.11.0",
"react-validation": "^3.0.7",
"reactstrap": "^6.5.0",
"validator": "^12.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I'm logged in as root and used nvm to install node and lts. I installed nvm like this:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
then did this:
nvm install node
nvm use node
nvm install --lts
nvm use --lts
Then I cd to /var/lib/jenkins/workspace/MedAverter/medaverter-front and install node_modules like this:
npm install -g
and then change the permissions to 777 recursively, like this:
chmod -R 777 node_modules
I also changed all the /root/.nvm permissions to 777 recursively, like this:
chmod -R 777 /root/.nvm
I can get it build once using
npm run build
but then I run a "Build Now" from Jenkins and it fails with the same
[[1;34mINFO[m] Running 'npm run build' in /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] [[1;34mINFO[m] > medaverter-front#0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] > react-scripts build [[1;34mINFO[m]
[[1;31mERROR[m] sh: 1: **react-scripts: Permission denied**
[[1;31mERROR[m] npm ERR! code ELIFECYCLE
[[1;31mERROR[m] npm ERR! errno 126
[[1;31mERROR[m] npm ERR! medaverter-front#0.1.0 build: `react-scripts build`
[[1;31mERROR[m] npm ERR! Exit status 126
Then I cd to /var/lib/jenkins/workspace/MedAverter/medaverter-front and run
npm run build
And also get the same error again:
> root#ubuntu-s-1vcpu-1gb-nyc1-01:/var/lib/jenkins/workspace/MedAverter/medaverter-front#
> npm run build
>
> > medaverter-front#0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
> > react-scripts build
>
> sh: 1: **react-scripts: Permission denied** npm ERR! code ELIFECYCLE
> npm ERR! errno 126 npm ERR! medaverter-front#0.1.0 build:
> `react-scripts build` npm ERR! Exit status 126
I've literally spent days trying to figure this out. Suggestions?
Solution 1:
I think you have react-script globally installed. so try this command
npm install react-scripts --save
and then run the application again.
Solution 2:
try this command
sudo chmod +x node_modules/.bin/react-scripts
and then run the application again.
Solution 3;
I think your npm not have permission. you can try to run by sudo
sudo npm run build
and you can fix this problem like this
Step 1:
check path of npm if you are using npm by
which npm
you will "/usr/local/bin/npm" this type of path
OR
check path of yarn if you are using yarn by
which yarn
you will "/usr/local/bin/npm" this type of path
Step 2:
give permission 777 to this path and try to run project
sudo chmod -R 777 /usr/local/bin/npm
Solution for macOS
From the root of your project run:
chmod +x node_modules/.bin/react-scripts
Instead of the default "start": "react-scripts start" specified in your package.json file, try changing it to the following and then try: "scripts": { "start": "node ./node_modules/react-scripts/bin/react-scripts.js start" }
I had modified the permissions on the folders earlier and undid the changes. I believe that is it.
I just deleted the node_modules folder and reinstalled the dependencies with the yarn command.
sudo rm -rf node_modules
yarn
yarn start
In my case it was giving at npm run.
this is how i solved.
rm -rf /node_modules
npm i
npm run android
It seems you don't have the right permission to build or execute the react app,
What you could do is try changing the owner to the current user using
sudo chown -R username:username /var/lib/jenkins/workspace/MedAverter/medaverter-front/
you can check your username by typing whoami in the terminal this is done because the current user does not have access to to the var folder or anything inside it by default. Furthermore you want to change the access mode to read and execute for everyone using
sudo chmod 755 /var/lib/jenkins/workspace/MedAverter/medaverter-front/
then you can want to change to root user using
sudo su
and then build the app using
npm run build
This has worked for me when i was facing somewhat a similar problem , hopefully it works for you as well,Cheers :)
I finally figured out a solution to this problem. It took days of effort. First, I deleted the Jenkins project and created a new one using Pipeline, rather than Freestyle. Then I added a Jenkinsfile with a script. That continued to fail but now I had the flexibility to add in additional commands to handle the errors.
One new error was something about jest-worker. I ran the following command on the DigitalOcean server to get past that one:
yarn add jest-worker
Then I had the old permissions error again. I think all these permission errors are due to user jenkins trying to run things owned by user root, even though everything had 777 permissions. I don't understand that but here's how I got around it. Modify the /etc/sudoers file and add the following line:
jenkins ALL=(ALL) NOPASSWD:ALL
Then modify the Jenkins file script to include recursive chmod and chown. Here is the full script that FINALLY worked:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Checkout...'
sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
checkout scm
sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
sh 'sudo chown -R jenkins /var/lib/jenkins/workspace/MedAverter/medaverter-front'
stash 'sources'
}
}
stage('Build') {
steps {
echo 'Build...'
unstash 'sources'
sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
sh 'sudo chown -R jenkins /var/lib/jenkins/workspace/MedAverter/medaverter-front'
sh 'mvn clean package -DskipTests'
stash 'sources'
}
}
}
}
I also had to increase the memory of the DigitalOcean droplet from 1GB to 2GB to get past another error. If someone knows a better way to get past that frustrating permissions error, please comment.
That happens because of lack of a package.json file
Just run this command and the issue will be fixed:
npm audit fix
Sometimes npm need root permission. Try using sudo in the command. It resolved a similar issue for me. This might help.
sudo npm run build
I have the same issue and I sole it with: version in package.json and npm i -g react-scripts
"react-scripts": "5.0.0"
It was : "react-scripts": "5.0.1" and I change it to "react-scripts": "5.0.0"
And again did npm i
If it's not solving issue please check versions of react-scripts form current version of React that you are using.
The following worked for me:
rm -rf node_modules
npm install // or yarn install
npm start // or yarn start

Husky not getting triggered on git events

I have created a react app which implements husky to capture lint errors:
Environment
git version 2.21.0 (Apple Git-122), node v8.16.2, npm v6.4.1
Lint implementation
Created a react using npx create-react-app my-app-name
Implemented eslint using eslint --init
Added the script to the package.json file:
“scripts”: {“lint”: “eslint src/**/*.js”,}
On running eslint src/**/*.js or npm run lint the lint errors are captured perfectly
Husky implementation
Installed husky npm install husky --save-dev
Added the husky hook to package.json:
"husky": {
"hooks": {
"pre-commit": "npm run lint:fix",
"pre-push": "npm run lint"
}
}
Testing git commits
Ran git commit -m "test commit"
Problem
The lint is never called when the commit is triggered. What is wrong here? Btw, I have tried solutions proposed here.
husky requires node > v10. Otherwise, it will skip with a warning message in the console.
Your node version is v8.16.2, please upgrade the same.

Resources