MacOS下VS Code集成终端无法运行nodemon问题求助
Hey there, I’ve dealt with this exact frustration before on macOS—let’s get nodemon working in your VS Code terminal right away. The root issue almost always boils down to VS Code not loading the same PATH environment variables as your system terminal, so it can’t find the globally installed nodemon executable.
Here’s how to fix it step by step:
1. Find your global npm bin path first
Open your regular system terminal (the one where nodemon works) and run this command to get the folder where npm installs global binaries:
npm get prefix
You’ll get output like /usr/local or ~/npm-global. Append /bin to this path—so it becomes something like /usr/local/bin or ~/npm-global/bin. This is where nodemon is installed.
2. Check which shell VS Code is using
In VS Code, press Cmd + Shift + P to open the command palette, then search for Select Default Shell. Note whether it’s set to zsh (macOS’s default now) or bash.
3. Update your shell’s config file
VS Code’s integrated terminal might not be loading your shell’s full configuration. Let’s add the npm bin path to your shell’s startup file:
- If you’re using zsh: Open
~/.zshrcin VS Code (you can useCmd + Oand type~/.zshrcto open it). Add this line at the bottom:export PATH="$(npm get prefix)/bin:$PATH" - If you’re using bash: Open
~/.bash_profileor~/.bashrc(whichever exists on your system) and add the same line above.
Save the file, then either restart your VS Code terminal or run this command in the integrated terminal to load the changes immediately:
# For zsh source ~/.zshrc # For bash source ~/.bash_profile
4. Quick alternative: Set PATH directly in VS Code settings
If you don’t want to mess with shell config files, you can set the PATH directly in VS Code:
- Press
Cmd + ,to open settings. - Search for Terminal > Integrated: Env: Osx and click Edit in settings.json.
- Add this line (replace
/usr/local/binwith your actual npm bin path from step 1):"terminal.integrated.env.osx": { "PATH": "$PATH:/usr/local/bin" } - Save the settings and restart the VS Code terminal.
Verify the fix
Run echo $PATH in both your system terminal and VS Code’s integrated terminal—you should now see your npm bin path listed in VS Code’s output. Then try running nodemon again—it should work!
内容的提问来源于stack exchange,提问作者Thuần Nguyễn

