You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nvm安装npm后Git post-receive钩子执行build.sh提示npm command not found的原因解析

Why npm Wasn't Found in Your Git Post-Receive Hook (When Using NVM)

Great question—this is such a common pitfall with environment variables in Git hooks, and I’m glad you’re digging into it for future use with NVM! Let’s break down what went wrong and how to fix it next time.

The Core Issue: Git Hooks Run in a Limited Environment

When you log into your server via SSH, your shell (like Bash or Zsh) loads your profile files (e.g., ~/.bashrc, ~/.profile, or ~/.zshrc) automatically. These files include the NVM initialization script that adds NVM’s Node.js/NPM binaries to your system PATH—that’s why npm works everywhere when you’re manually logged in.

But Git hooks (like post-receive) run in a non-interactive, non-login shell. This means they don’t load your user’s profile files by default. So even though NVM and NPM are installed in ~/.nvm, their paths aren’t in the PATH environment variable that the hook uses. That’s why you got the "npm command not found" error.

Why Moving ~/.npm Didn’t Help

You mentioned moving ~/.npm to /usr/local/bin—but that directory is actually NPM’s cache folder, not where the npm executable lives. The real npm binary is stored in ~/.nvm/versions/node/<your-node-version>/bin/npm. Moving the cache directory doesn’t add the actual executable to a system-wide PATH location, so it didn’t fix the problem.

Fixes to Use NVM with Git Hooks (For Next Time)

If you want to go back to using NVM later, here are three reliable ways to make npm work in your post-receive hook:

1. Manually Load NVM’s Initialization Script

Add this line at the very top of your build.sh (or directly in the post-receive hook):

source ~/.nvm/nvm.sh

This script sets up the correct PATH and environment variables for NVM, just like when you log in via SSH. After this line, running npm run prod will work as expected.

2. Use the Absolute Path to NPM

First, find the full path to your NPM executable by running this in your SSH session:

which npm

You’ll get something like ~/.nvm/versions/node/v18.17.0/bin/npm. Then replace npm in your hook with this absolute path:

~/.nvm/versions/node/v18.17.0/bin/npm run prod

This skips relying on the PATH entirely and calls the executable directly.

3. Load Your User Profile File

If your NVM initialization is in ~/.bashrc, you can source that file at the start of the hook:

source ~/.bashrc

Note: Some ~/.bashrc files have checks to only run in interactive shells (like [[ $- == *i* ]]). If that’s the case, move the NVM initialization code outside of that check, or add a condition to allow it to run in non-interactive shells.

Wrap-Up

Your fix with sudo apt-get install npm worked because the system-wide NPM installs its executable to /usr/bin/npm, which is in the default PATH that Git hooks use. But now you know the root cause of the NVM issue—environment variable differences between interactive and non-interactive shells—and how to work around it next time.

内容的提问来源于stack exchange,提问作者Dmitry_allread_return

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:58:16