export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
\. to source files (vs .)? export necessary?_I also don't like how nvm is loaded on startup every time, but that's been covered in other issues._
For reference, here is my current script.
Why use . to source files (vs .)?
Because some people may have aliased the . - see #1278.
Why check for the existence of the file instead of just sourcing it - cleaner and throws an error if you don't have the file.
Because you should be able to delete the file, not be forced to clean up your profile files, and still not see an error.
Is the export necessary?
which export did you have in mind? People might be sourcing nvm.sh multiple times, so for example, NVM_DIR is exported to be shared across those.
As for being loaded on startup every time, if you load with --no-use, it will not be slow, so I'm not sure why else you wouldn't like it being loaded on startup.
@ljharb You're amazingly responsive, thank you!
Because some people may have aliased the . - see #1278.
. vs \.: clarity for new readers trumps individual edge cases IMO but I trust your judgement. I think source could be a decent (clear) alternative. I spent some time making sense of \..
Because you should be able to delete the file, not be forced to clean up your profile files, and still not see an error.
But why? Is it because beginners may not know how to edit their profile if they uninstall nvm?
which export did you have in mind? People might be sourcing nvm.sh multiple times, so for example, NVM_DIR is exported to be shared across those.
I'm not familiar with why/how that works, will let that one go.
As for being loaded on startup every time, if you load with --no-use, it will not be slow, so I'm not sure why else you wouldn't like it being loaded on startup.
I saw you mention --no-use previously but I guess I didn't fully understand how it works, updated my script!
New Questions
--no-use?source could be a decent (clear) alternative
source is not an alternative because it's not available in POSIX - ie, in all the shells nvm supports.
But why? Is it because beginners may not know how to edit their profile if they uninstall nvm?
it's easy to uninstall nvm by simply deleting the file; forcing additional work (to track down where the line(s) are that source nvm) doesn't seem particularly valuable. How would making this change help you?
Why isn't there a built-in command to upgrade nvm (I had to write one myself)?
See #400.
Why doesn't the README mention
--no-use?
Primarily that I only added it as a result of npm config get prefix being slow; and it's not something I want the majority of users using. At some point I'll be able to speed it up, and then nobody would need any workarounds. I suppose I'd accept a PR adding it to the readme.
RE: sourcing nvm
I want alerts of sourcing issues on login, but leaving as is works.
RE: nvm upgrade
I've decided to risk installing nvm via homebrew, I think the README could be more clear as to why brew isn't supported. I like that it keeps track of all my installed software and updates automatically (I run the updates often).
RE: --no-use PR
I'll give it a shot!
Definitely do not install nvm via homebrew; one because then I won't support any issues you have with it, but also because as you update nvm with homebrew, you'll wipe out your node installs every time. Never install software except via an official distribution mechanism (nvm installs node from the only official mechanism, nodejs.org).
You can certainly edit your profile file to remove the file existence check.
PR: #1839
Can you explain more about wiping out installs? I think you're assuming that I'm leaving NVM_DIR unchanged, which I'm not. Checkout brew info nvm:
nvm: stable 0.33.11, HEAD
Manage multiple Node.js versions
https://github.com/creationix/nvm
/usr/local/Cellar/nvm/0.33.11 (7 files, 138.6KB) *
Built from source on 2018-06-16 at 23:46:19
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/nvm.rb
==> Options
--HEAD
Install HEAD version
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.You should create NVM's working directory if it doesn't exist:
mkdir ~/.nvm
Add the following to ~/.bash_profile or your desired shell
configuration file:export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.Type
nvm helpfor further information.Bash completion has been installed to:
/usr/local/etc/bash_completion.d
I tried uninstalling and reinstalling nvm and my .nvm folder remained unchanged (aliases and versions folders). Was able to use my previously installed node versions without any issues.
By "official source", I think your implying that I'm putting my trust in homebrew and therefore at risk that they're tampering with their distributions. Agreed, but it's super convenient.
I wasn't aware there was a way out of the default destructive behavior, TIL.
There's a very large number of bugs filed where users' problems magically vanish when they install nvm properly instead of with homebrew. Obviously you can use it if you like, but I can assure you it won't remain convenient for long.
Are there any other questions to answer? Thanks for filing #1839; otherwise I think I've answered all your questions :-)
Glad I could add some value re: brew! Will monitor any brew related issues.
No more questions. You're awesome, thank you!
There's some solid discussion on stack exchange re: checking for existence of file before sourcing it. Hope it's ok I used nvm example and quoted you.
Most helpful comment
Glad I could add some value re: brew! Will monitor any brew related issues.
No more questions. You're awesome, thank you!