I messed up my npm configuration by cleaning my machine, and have been having issues with npm on MacOs since.
I tried fully uninstalling and reinstalling npm and nvm, first, but the issue persists.
When I use npm -v or nvm -v in bash terminal I get:
-bash: npm: command not found
and
-bash: nvm: command not found
So I go try to download nvm with:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Which returns:
=> nvm is already installed in /Users/user/.nvm, trying to update using git
=> => Compressing and cleaning up git repository
=> nvm source string already in /Users/user/.bashrc
=> bash_completion source string already in /Users/user/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Ok, so as instructed I run:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Now npm and nvm are accessable in that instance of the terminal, but in a new instance I'm back to square one. I thought that this was solved by including the nvm source string in my .bashrc but it looks like that is already done.
I have also checked my bash version with
bash --version
which returned:
GNU bash, version 5.1.8(1)-release (x86_64-apple-darwin20.3.0)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3 : GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
How can I maintain access to nvm and npm accross bash instances?
CodePudding user response:
Zsh is a very capable shell, although much less popular.
Since npm and nvm commands are not found, it may be that you did a bit too much cleanup. You'd probably be looking how to reinstall these on MacOS with homebrew. Now I am not sure you will get the installed modules database back.
CodePudding user response:
Sounds like the exports suggested by the download are not available globally in your bash terminal:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
This means that in theory just by running these exports everytime you need to use npm and nvm this will prove as a temporary solution. Unfortunately, I can tell you the theoretical problem (lack of global knowledge of the path to npm), but I do not actually know how to solve this given the bash terminal is used. Maybe you can continue with some further searching and find a solution with this theory.
Alternatively, a much easier solution is to switch to a Homebrew managed version of Node, which will manage pathing issues for you (allowing global access to npm once again in all terminals by default). This would require installing Homebrew (make sure to follow instructions carefully once you run the installation command in the bash Terminal) and then the Node formula:
brew install node
Keep in mind Homebrew will always install the latest version available (Not LTS).
I have not managed to find a source where the bash terminal is used for node.
If you are not interested in using Homebrew, you can take a look at things I found while trying to help you:
- Install using zsh terminal with migration from bash (if you decide this is the way you want to go)
- Node website vs Homebrew vs Using nvm
