Setting up paths in Sublime with NVM

MacOS

Question or issue on macOS:

I am using NVM as my node version manager on OSX, which is great except that it creates multiple problems with my IDE. I am using Sublime Text and most of the plugins that I have ever used look for nodejs at /usr/local/bin and since I am using NVM node is obviously not available at that location, instead available at /Users/${User}/.nvm/${NodeVersion}/bin/node.

I have an option of updating the path of nodejs in that plugin’s configuration but then when I install another plugin I have to do the same thing. Same applies to using CoffeeScript, LESScss etc.

Moreover when I update my node version via NVM, I have to update the paths in all the configs again. NVM has quite essentially created more problems while trying to solve one for me.

UPDATE:
After a very long time I was able to finally resolve the problem using isaacs brilliant solution – nave. The nave usemain stable command, is just enough to understand you sentiments 🙂

How to solve this problem?

Solution no. 1:

I was having a similar problem yesterday. I found a python script that exports my Node path (the nvm node path) for Sublime. I updated it for OSX and ST3. You can find it here:
https://gist.github.com/joesepi/11269417

Drop that script in your Packages dir, the path for ST3 is in the comments in the script. You may have to restart ST as well. And if you update your node version, you will need to update that script too. I hope that helps!

Solution no. 2:

you can use command “nvm which node-version ” such as

nvm which 0.12.0

Solution no. 3:

New Answer for 2020!

You can now add the following one-liner to your .bashrc, .zshrc, etc.

export NVM_SYMLINK_CURRENT=true

Then, anywhere you need your node path, it’s always in the same place:

~/.nvm/current/bin/node

#OR

$HOME/.nvm/current/bin/node

Further reading: https://medium.com/@danielzen/using-nvm-with-webstorm-or-other-ide-d7d374a84eb1

Solution no. 4:

CURRENT_NODE_VERSION=$(nvm current); nvm which $CURRENT_NODE_VERSION

Solution no. 5:

Install NVM from github.com/xtuple/nvm

Run this command to install NVM as global and you’ll see everything works fine

wget -qO- https://raw.githubusercontent.com/xtuple/nvm/master/install.sh | sudo bash

Solution no. 6:

I’m using n. I also tried nave but I was having trouble with it with nvm installed. Hope this helps.

Solution no. 7:

For Linux users,

By default nvm write some node paths in your .bashrc and there are loaded only if you’r in bash mode.

So just you need to move the two paths-line to the bottom on the file like that :

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# Set the NVM path for my Sublime before return.
export NVM_DIR="/home//.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Solution no. 8:

I am using ubuntu 18 and this was how i was able to solve it

open your command prompt and access /home/{username}/.config/sublime-text-3/Packages create a new file and put this code

# $HOME/.config/sublime-text-3/Packages/node-env.py

import os
import getpass

nvm_path = '/home/%(user)s/.nvm' % {'user': getpass.getuser()}
nvm_default_file_path = '%(root)s/alias/default' % {'root': nvm_path}
nvm_node_root = '%(root)s/versions/node' % {'root': nvm_path}

# Grab default alias
with open(nvm_default_file_path, 'r') as content_file:
   content = content_file.read()

# Prepend 'v' to match folder names
version = content.strip()
if version[0] != 'v':
version = 'v' + version

# Take highest valid folder name
versions = os.listdir(nvm_node_root)
found = sorted([v for v in versions if v.startswith(version)])[-1]

if found == None:
  print("Failed to configure node: no valid version found for %(version)s" %{'version': version})
else:
  print("Configure node: %(version)s" % {'version': found})
  node_path = "%(root)s/%(version)s" % {'root': nvm_node_root, 'version': found }
  print("Node path: %(root)s" % {'root': node_path})
  path = "%(root)s/bin:%(root)s/lib:%(path)s" % {'root':node_path,'path':os.environ["PATH"]}
  os.environ["PATH"] = path

save this file as “node-env.py”.

It helps solve issues with most JavaScript libraries like Typescript plugging and others.

Hope this helps!