nvm: which nvm returns nothing

Since nvm is a shell function and not a shell executable file, the shell command which nvm returns no results. This means that you are unable to use nvm in any npm scripts commands.

Use case, I’m trying to run my tests using node v0.11.x and run my actual app as a subshell process with node 0.10. Reason: Company has standardized production on 0.10 because it is stable, but I would like to use generators for my front-end integration testing (via a project like yiewd

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 5
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Ah, I see now, thanks for the explanation. Yes, I found it in the docs, when started to examining them closely. However its a single sentence only. Worth moving to the its own top section I think. Something like:

Architecture
=========

Please note, that `nvm` is implemented as a shell function and not an executable file. This is because executable file can not change the PATH in the current shell. Do not expect `which nvm` to return anything - again, its a shell function. To "bring" this function into the current shell scope you need to include the source with it, which can be done with the `source file.sh` or `. file.sh`. 

If you don't need to modify the current shell and just want to run some command in a specific Node version, you can also use the `nvm-exec`.

Something like that, at least thats what I’d do.

I see. Just not sure, why this extra effort of googling why which nvm returns nothing and why I need to use nvm-exec instead of nvm when using tool called nvm. I understand that you probably have some reasoning behind this choice, but from user perspective its unexpected and feels unnecessary.

At the very least, it should noted in the bold in the docs - “NVM is implemented as a shell function, not as an executable script. Do not expect which nvm to return any value. Instead, you need to manually source the script which declares the nvm function and then you can use it.”

Usually a subshell would run the same sourcing logic, so you could just call nvm normally.

You could maybe do something like $NVM_DIR/nvm.sh && nvm use 0.11 && npm test?

So is there a way to used sourced shell functions with npm run-script?

i just want to test if NVM is installed and move on with more important tasks. i don’t know really care how it works frankly.

@justinTM that’s because nvm itself won’t work inside subshells, because it’s a sourced shell function. In other words, command -v nvm is working perfectly correctly.

I’m sorry your expectations are limited to “everything is a binary, and which works with everything”, but that’s just not how shells work.