DEV Community

Vijay
Vijay

Posted on

Struggling with 'command not found' on Mac?

You open a new terminal window. You type a command.

But your Mac says, "command not found." It's super annoying.

What’s going on here?


The Problem: Your Terminal Forgot Everything

You're trying to use a command like nvm. It's a handy tool, right? But your computer has no idea what you're talking about.

This happens with tools you install with Homebrew. The nvm command only works after you run a specific command.

You have to tell your terminal about it again.


The Explanation: Shell Configuration

Think of your terminal like your buddy. When you first say hi, he's got a fresh memory. He doesn't remember anything from your last chat.

The special source command is how you remind him. It's a command that tells the shell to run a file's commands. It's like you're loading a script.

That script tells your shell where to find nvm. Now your terminal remembers what to do.


The Solution: Make It Automatic

We need to make sure your terminal remembers this automatically. We do that by editing a special file. This file runs every time you open a new terminal.

You just need to put that source command inside.


Step 1: Find your shell's config file.

Most modern Macs use zsh. If you're using zsh, you'll want to edit the .zshrc file. If you have an older Mac, it might be bash.

Then you'll need to edit the .bash_profile file. Not sure? Just type echo $SHELL.


Step 2: Add the fix to the file.

Open your file with a simple command. Use nano ~/.zshrc or nano ~/.bash_profile. Scroll to the bottom of the file.

Paste this little snippet of code there.

export NVM_DIR="$HOME/.nvm"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && \. "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
Enter fullscreen mode Exit fullscreen mode

Step 3: Save and restart your terminal.

Hit Ctrl + X to exit and save. Then open a new terminal window. You can also run source ~/.zshrc or source ~/.bash_profile to try it right away.

Now, your terminal should remember your nvm command every time. No more "command not found" errors.


It’s a simple fix for a common headache.

You've just automated your terminal's memory. It’s a small change. But it makes a big difference.

Now you can get back to coding without the hassle. Pretty cool, right?

Top comments (0)