Skip to content

On Linux: How exactly does NVM update the path variable? Doesn't work for GUI apps. #381

Closed
arechsteiner opened this Issue Mar 17, 2014 · 9 comments

5 participants

@arechsteiner

I'm having an issue on Xubuntu with PhpStorm (WebStorm), and that is that apparently the application itself does not have the current node bin directory in the path variable. This is a little odd, since the variable is set properly terminal (even in the terminal that is launched inside PhpStorm).

The specific functionality that is affected by this is configuring node based programs as external tools (such aus autoless, or nodemon). To demonstrate, here is the result of printenv PATH in my bash terminal:

/home/alex/.nvm/v0.10.26/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

And here is the result if I configure printenv PATH as an external tool in PhpStorm and run in that way:

 /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

You see, in the latter case, the NVM path is not present even though it's the same command. I should mention that if I run PhpStorm from the terminal, then NVM is in the path. I'm relatively new to Linux, but it seems to me that somehow the path set by NVM does not make it's way into GUI apps (or at least this particular GUI app).

Maybe if someone could tell me how exactly NVM is setting and updating the path variable on version change, I'd understand why this happens.

@ljharb
Collaborator
ljharb commented Mar 17, 2014

This is related to #313. It's likely your IDE isn't actually source your bashrc (or equivalent), and nvm doesn't modify the root user's PATH.

@arechsteiner

It's likely your IDE isn't actually source your bashrc (or equivalent)

Should it? Would this be more of an IDE issue then?

and nvm doesn't modify the root user's PATH.

Wouldn't there be a way for NVM to change the current users path in a way that affects all programs. Coming from Windows, it's odd to me that the path is not available to all programs.

@ljharb
Collaborator
ljharb commented Mar 18, 2014

See the issue I linked for lots of discussion on it - it's definitely a limitation in your IDE.

There's a number of changes underway that may solve your problem, but for now, you'll have to do which node on your terminal, and manually enter that path into your IDE.

@arechsteiner

you'll have to do which node on your terminal, and manually enter that path into your IDE.

The problem with that is this: I'm not configuring the IDE to run node, I configure it to run autoless or nodemon which in turn contain this line

#!/usr/bin/env node

From what I understand this tells the executor (my IDE?) to take the node program which is in the environment's PATH. Since it's not there, I'll get an error saying node - no such file or directory.

I'm not sure what's the best way to fix this. As a workaround, I added a symlink to /usr/local/bin called node which points to the nvm node binary I'm using. I think this is semi ideal since I'd have to update it manually should I care to switch versions via nvm.

@creationix
Owner

If your IDE allows to to set a custom $PATH variable, simple see what it's set to when you open a nvm enabled terminal session and copy that to the editor. This is what I do for my upstart scripts on production servers.

Also I think you can create a symlink at /usr/local/bin/node that points to the node file in nvm. If I remember right, node will load libraries and npm relative to the actual location after resolving the symlink. Of course you'd have to do the same thing for npm, nodemon and anything else you want globally available.

Remember that you can use nvm and install node globally by copying the binary release to /usr/local/*. When nvm is activated, it will take precedence over the global version.

@mgol
mgol commented Apr 5, 2014

I'm on OS X but this should apply as well. I've just added /usr/local/node-bin to the PATH (at least to the one for GUI apps) and I use this little script to set the link to node inside:

#!/bin/bash

# Create an alias for node binaries for purposes of GUI apps.
NODE_BIN='/usr/local/node-bin'
rm -f "$NODE_BIN"
ln -s "`echo -n $PATH | cut -f1 -d:`" "$NODE_BIN"
unset NODE_BIN

This has the added advantage of being able to switch node versions on-the-fly, without restarting GUI apps.

@ljharb ljharb closed this in 8f66273 Jul 13, 2014
@ljharb
Collaborator
ljharb commented Jul 13, 2014

(#467)

@jcrben
jcrben commented Aug 16, 2015

For those who stumble across this looking for an answer to the question, nvm.sh is sourced in .bash_profile/.bashrc and it appears that nvm.sh changes the path through the nvm_prepend_path function which is called here.

@jcrben jcrben referenced this issue in danielkec/NodeJS Aug 16, 2015
Open

/bin/sh: line 1: node: command not found #3

@ljharb
Collaborator
ljharb commented Aug 16, 2015

Yes, that's correct - but please don't rely on any nvm internal functions, as I'm liable to break them at any time. If there's functionality you need exposed, please file an issue and let's talk about how to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.