Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm: not found when using a git client and nvm #178

Closed
leettaylor opened this issue May 4, 2021 · 5 comments · Fixed by #391
Closed

npm: not found when using a git client and nvm #178

leettaylor opened this issue May 4, 2021 · 5 comments · Fixed by #391
Labels
help wanted Extra attention is needed

Comments

@leettaylor
Copy link

leettaylor commented May 4, 2021

Since v0.7.3 it appears Lefthook is struggling to find npm when using a git client (e.g. GitHub Desktop/SmartGit) while using nvm.

It works as expected when making a commit directly from the command line or when using v0.7.2.

OS: Ubuntu

Error

Commit failed - exit code 1 received, with output: 
Lefthook v0.7.3
RUNNING HOOKS GROUP: pre-commit
⠋ waiting 
1_stylelint (SKIP. NO FILES FOR INSPECTION)
  EXECUTE > 2_eslint
 sh: 1: npm: not found
 3_prettier (SKIP BY BROKEN PIPE)
SUMMARY: (done in 0.01 seconds)
🥊  2_eslint

lefthook.yml

pre-commit:
  piped: true
  commands:
    1_stylelint:
      tags: frontend linting styles
      glob: "*.{scss,vue}"
      run: npm exec -- stylelint {staged_files} --quiet --fix
    2_eslint:
      tags: frontend linting javascript
      glob: "*.{js,vue}"
      run: "npm exec -- eslint {staged_files} --quiet --fix"
    3_prettier:
      tags: frontend formatting styles javascript
      glob: "*.{scss,css,js,vue}"
      run: "npm exec -- prettier --check {staged_files} --write --ignore-unknown"

NPM

We are currently using nvm to manage our npm installation.

❯ which npm
/home/lee/.nvm/versions/node/v15.14.0/bin/npm

If we use the standard npm package (sudo apt install nodejs npm) it works fine.

❯ which npm
/usr/bin/npm
@leettaylor leettaylor changed the title npm: not found when using a git client npm: not found when using a git client and nvm May 4, 2021
@Envek Envek added the help wanted Extra attention is needed label May 4, 2021
@Envek
Copy link
Member

Envek commented May 5, 2021

Can you please tell us a bit more details?

  • Which git client you're using and (that's important) how you have installed it (using what package manager: deb, snap, etc).
  • How you have installed lefthook itself? (npm, brew, direct download?)
  • Add one more step to your configto debug things and tell what it outputs:
      commands:
        0_debug:
          run: echo $PATH 
          # Also try this:
          run: sh -c 'echo $PATH'

I believe that problem is in environment set up by your git client when it executes git commands (probably with PATH)

@Envek Envek added the waiting for response We need more details or confirmation label May 5, 2021
@leettaylor
Copy link
Author

leettaylor commented May 5, 2021

  • Which git client you're using and (that's important) how you have installed it (using what package manager: deb, snap, etc).

Using a Linux fork of GitHub Desktop v2.5.3. Using the debian package.

  • How you have installed lefthook itself? (npm, brew, direct download?)

Lefthook is a dependancy in our package.json file so is installed locally via npm.

  • Add one more step to your configto debug things and tell what it outputs:
Lefthook v0.7.4
RUNNING HOOKS GROUP: pre-commit
⠋ waiting 
  EXECUTE > 0_debug
/usr/lib/github-desktop/resources/app/git/libexec/git-core:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

1_stylelint (SKIP. NO FILES FOR INSPECTION)
  EXECUTE > 2_eslint
 sh: 1: npm: not found

 3_prettier (SKIP BY BROKEN PIPE)

SUMMARY: (done in 0.04 seconds)
✔️  0_debug
🥊  2_eslint

GitHub Desktop $PATH output:

/usr/lib/github-desktop/resources/app/git/libexec/git-core:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

When just using the command line $PATH output:

/usr/lib/git-core:/usr/lib/git-core:/home/lee/.nvm/versions/node/v15.14.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

@Envek Envek removed the waiting for response We need more details or confirmation label May 5, 2021
@Envek
Copy link
Member

Envek commented May 5, 2021

GitHub Desktop $PATH output:

Oh, I was afraid of that 😞


It works as expected when making a commit directly from the command line or when using v0.7.2.

I wonder whether it could be caused by #146

@leettaylor
Copy link
Author

It works as expected when making a commit directly from the command line or when using v0.7.2.

I wonder whether it could be caused by #146

I've done some playing about and it didn't actually work with v0.7.2. It looks as though on v0.7.2 it just skips the hooks all together and then commits even if the hooks should fail 🤷.

GitHub Desktop $PATH output:

Oh, I was afraid of that

It seems that launching the git client via the command line correctly populates the PATH. (Relevant issue)

It looks as though Husky also suffers the same issue due to PATH problems (#639 & #390).

The workaround for Husky involves a ~/.huskyrc file that is invoked before running any commands where we can manually configure the PATH. I'm wondering if something similar can happen here and we can use a ~/.lefthookrc?

@defunctl
Copy link

defunctl commented Nov 3, 2021

Had this problem while trying to run a global PHP composer command, which is in my path but not found when run via lefthook.

Fortunately, composer has a way to get the bin directory, so I can dynamically call it and set the path before running my command and then it works.

x-phpcs: &phpcs
  run: export PATH="$PATH:$(composer global config bin-dir --absolute --quiet)" && phpcs -s --standard=./phpcs.xml.dist {staged_files}

pre-commit:
  parallel: true
  commands:
    phpcs:
      glob: "app/*.php"
      <<: *phpcs
    # etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants