New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"nvm: command not found" when in sub-shell #521
Comments
If you re-source Using a true subshell - ie, with |
Hmm, if I try using ~/.nvm/nvm.sh it gives me "permission denied" with both bash and sh. |
What's |
Prints out |
But that's the same location as ~/.nvm/nvm.sh - it gives me "permission denied" either way i do it (with ~ or explicitly with /home/vagrant/..). Sorry I wish I could help debug more, but I'm no linux wizard - more of a js wizard ; ) |
@fresheneesz Can you try rerunning the installation step from the README? Specifically, Hopefully you installed it this way to begin with, and not with something like homebrew, or |
@ljharb That is in fact the way I installed it (specifically with the curl command and not wget). But, I ran it again. Even tho |
Wait, are you trying to execute |
I don't know what you mean by "source it". When I run it with the dot, I don't get an error, but I don't get anything else either. Blank response. Its the same for both normal shell and subshell. |
Yes, that's the exact and intended behavior. After running it with the dot, try
|
Wait, nvm,sh doesn't need to be marked executable. That only confuses people thinking they should execute it instead of source it. |
Yes, sorry, I realized my mistake and pushed for sourcing it later in the thread :-) |
Holy cow it works.
gives me the nvm help notes. Still wondering why "sourcing" it is necessary at all tho. Seems strange to me. Any way to upgrade nvm so that it doesn't need that? Do you think this method would allow me to run nvm after freshly installing it without restarting my console? |
Sourcing is the primary way Yes, that should work without restarting your shell - doing so is just a way to guarantee that nothing weird is going on. |
@ljharb sure, it may be the primary way nvm works, but can we add something that sources nvm at the end of the install script, so people don't have to restart their console? And is there a way to automatically source nvm on creation of new shells? I'm running a shell script from a node.js program, and its very unexpected to see that nvm isn't available in that context. |
@fresheneesz That's how the install script already works on every platform I have available to test. It's possible that it has an issue with CentOS 6.5, however. See #394 for a possible solution. |
@fresheneesz nvm is not a bash script or any kind of shell script. It's a bash function. This means it only lives in your transient shell environment. Most people (and the install script) put a line in their .bashrc or .profile to have it auto-sourced every time a new interactive shell is created, but there is no way to inject itself into newly created sessions that explicitly don't inherit environment from their parent and don't source hooks like .bash_rc. If you want to use nvm in an environment that can't source your .bashrc or .profile, then you'll need to manually source nvm yourself before calling it's functions. I hope this makes sense. |
The reason that nvm is a shell function and not a script is because it works by changing the environment of your current shell. This is a major design decision and the thing that makes nvm different from n and nave. n works by creating global symlinks to different node versions. nave works by creating a new subshell with the desired env modifications. I decided to go the less intrusive route and only modify the current shell. Mostly it just mucks around with $PATH so that the desired version of node is first on the path. Other shells can concurrently use their own local node version unmolested. I do this without the overhead of creating new subshells or changing global paths. Only shell functions can do what I want. The major drawback obviously is it confuses people who think nvm.sh is a shell script meant to be run in a subshell like every other sh file they've seen. I'm sorry for the confusion but it is what it is. Subshells can't modify their parent's environment. |
Got it, thanks for the help guys! |
Edit: Don't do this. |
@daryltucker hmm, how did you install |
Hey @ljharb Using the README. After install, I was running into issues and trying to diagnose why I had to source the file every time I opened a new terminal. I later realized it was that I had to do I assumed Sorry for the confusion, and I just removed the executable flag from that file, making sure it works 100% as expected. |
Great, thanks for the explanation. I'm likely to soon change the default behavior so that the first |
I have found what the issue is. The nvm script has some inconsistencies inside it. here is why: Working. The script:
Using it:
Not working (blank output, error code 3). The script:
Using it:
|
What's |
Breaking when |
@ljharb: The issue is that I'm trying to use it in the middle of shell script forest/stack, which are all coded with |
Yes, noted, but in the meantime, |
@ljharb: yes, was thinking at something similar. I will probably end up wrapping that into its own script, and do all the +e/-e dance in there. So when the upstream fix is in, I will not have to hunt down all occurrences. Thanks for confirming it tho. Greatly appreciated. Feel free to close this one. |
Oh, it is closed :) |
In my case, my VPS host added these lines in
I just comment it, hope it can help. |
I'm sorry to dig up an old thread. I just wanted to say thanks because this helped me solve my issue. That said, it's a problem that I always run into occasionally because enough time passes that I forget the solution. I was wondering, why does nvm have to source itself through my bash profile, or manually as in the case described in this ticket, every single time? Why can't nvm set itself up as an executable script in my existing $PATH like most other programs? |
@chingc because it needs to modify the PATH in the current shell, and that's impossible except via a sourced shell function. |
/var/lib/jenkins/workspace/node.JS-pipeline@tmp/durable-1816b816/script.sh: line 1: nvm: command not found |
@bsreddy23 make sure your script is sourcing nvm.sh, potentially in each stage of the pipeline. |
Correct, we use Bitbucket pipelines and had to source the file like this in order to use v18 inside a folder: |
After installing nvm and restarting my terminal, I can run nvm fine normally. But if I create a new shell (either with
sh
orbash
) nvm gives me back "command not found". Why can't my inner shell see nvm?I'm on Centos 6.5, and installed nvm with the basic curl install.
The text was updated successfully, but these errors were encountered: