Skip to content

Instantly share code, notes, and snippets.

@joesepi
Forked from joaoneto/node_env.py
Last active May 26, 2022 07:22
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save joesepi/11269417 to your computer and use it in GitHub Desktop.
Save joesepi/11269417 to your computer and use it in GitHub Desktop.
Updated python script to manage nvm and ST3 pathing for OSX
# Sublime package NVM node path configuration
# Save this file in:
# ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/node_env.py
import os
os.environ["PATH"] = "/Users/YOUR_USERNAME/.nvm/v0.10.26/bin:/Users/cranemes/.nvm/v0.10.26/lib:/Users/YOUR_USERNAME/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
print("PATH=" + os.environ["PATH"])
@felix1m
Copy link

felix1m commented Jun 27, 2014

Great, thanks for sharing. A Few improvements:

  • not hardcoding the version, instead use the current nvm default alias:
  • not hardcoding user
import os
import getpass

user = getpass.getuser()

nvm_default_file_path = '/Users/%(user)s/.nvm/alias/default' % {'user': user}

with open(nvm_default_file_path, 'r') as content_file:
    content = content_file.read()

version = content.strip()

path = "/Users/%(user)s/.nvm/%(version)s/bin:/Users/cranemes/.nvm/%(version)s/lib:/Users/%(user)s/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" % {'version':version, 'user':user}

os.environ["PATH"] = path

@joesepi
Copy link
Author

joesepi commented Jul 14, 2014

thanks for the updates @felix1m !

@fijiaaron
Copy link

couldn't you just do something like this:

!/bin/bash

nvm_path=~/.nvm/current
if [ -d $nvm_path/bin ]; then
export PATH="$nvm_path/bin":$PATH
fi

@tconroy
Copy link

tconroy commented Nov 12, 2014

This doesn't work for me in Sublime Text 3 (after adjusting for my Node version).. Trying to get my NVM node to be detected by Sublime Text is a real pain :/

@pherris
Copy link

pherris commented Apr 22, 2015

I had to also run nvm alias default v0.10.36 to set the default that this script is looking for. Much thx, frustrating to try to get these to work together.

@heyvito
Copy link

heyvito commented Apr 23, 2015

Looks like @felix1m forgot to replace one username instance.
Updated code:

import os
import getpass

user = getpass.getuser()

nvm_default_file_path = '/Users/%(user)s/.nvm/alias/default' % {'user': user}

with open(nvm_default_file_path, 'r') as content_file:
    content = content_file.read()

version = content.strip()

path = "/Users/%(user)s/.nvm/%(version)s/bin:/Users/%(user)s/.nvm/%(version)s/lib:/Users/%(user)s/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" % {'version':version, 'user':user}

os.environ["PATH"] = path

@echenley
Copy link

I might have a different version of NVM, but I had to update the path structure as follows:

import os
import getpass

user = getpass.getuser()

nvm_default_file_path = '/Users/%(user)s/.nvm/alias/default' % {'user': user}

with open(nvm_default_file_path, 'r') as content_file:
    content = content_file.read()

version = content.strip()

path = "/Users/%(user)s/.nvm/versions/node/v%(version)s/bin:/Users/%(user)s/.nvm/versions/node/v%(version)s/lib:/Users/%(user)s/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" % {'version':version, 'user':user}

os.environ["PATH"] = path

@davidhq
Copy link

davidhq commented Sep 5, 2015

These solutions don't work in newer versions of nvm because in /.nvm/alias/default file there is only "node" written (as opposed to "iojs"), so no versions...

I updated the solution so that it uses nvm version

Put this in ~/bin/node_ver (replace USER with your user), make it executable

#!/bin/bash
export NVM_DIR="/Users/USER/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
nvm version

Then copy this script:

# Sublime package NVM node path configuration
# Save this file in:
# ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/node_env.py
import os
import getpass
import subprocess

def runProcess(exe):
  p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  while(True):
    retcode = p.poll() #returns None while subprocess is running
    line = p.stdout.readline()
    yield line
    if(retcode is not None):
      break

user = getpass.getuser()

version = None

for line in runProcess('/Users/%(user)s/bin/node_ver' % {'user': user}):
  if not version:
    version = line.rstrip().decode("utf-8")
    break

path = "/Users/%(user)s/.nvm/versions/node/%(version)s/bin:/Users/%(user)s/.nvm/versions/node/%(version)s/lib:/Users/%(user)s/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" % {'version':version, 'user':user}
os.environ["PATH"] = path

to ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/node_env.py

Hint: you can also use https://github.com/isaacs/nave to avoid these problems - I think, someone on SO mentioned it, I haven't tried yet...

Hint 2: it's good to use babel-node inside build system:

{
  "cmd": ["babel-node", "$file"],
  "selector": "source.js"
}

should work - no other changes neccessary

Enjoy

@airtonix
Copy link

airtonix commented Aug 25, 2016

Put an .nvmrc in your project root.

# <sublimetext-root>/Packages/node_env.py

import os
import sublime

home = os.path.expanduser("~")
nvm_default_file_path = '/%(home)s/.nvm/alias/default' % {'home': home}
window = sublime.active_window()
project_root = os.path.dirname(window.project_file_name())
nvmrc_file_path = os.path.join(project_root, '.nvmrc')

if (os.path.exists(nvmrc_file_path)):
    with open(nvmrc_file_path, 'r') as content_file:
        content = content_file.read()

elif (os.path.exists(nvm_default_file_path)):
    with open(nvm_default_file_path, 'r') as content_file:
        content = content_file.read()

if content:
    version = content.strip()
    print('Version', version)
    path = "%(home)s/.nvm/%(version)s/bin:/%(home)s/.nvm/%(version)s/lib:/%(home)s/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" % {'version':version, 'home':home}
    print('Path', path)
    os.environ["PATH"] = path

Copy link

ghost commented Sep 18, 2016

On mac osx, this solution works perfectly for me (see last comment)
http://stackoverflow.com/questions/36250662/sublime-text-use-node-from-nvm-for-build

{
    "shell_cmd": "bash -c \"source ~/.nvm/nvm.sh && nvm run default $file\"",
    "selector": "source.js",
}

@naholyr
Copy link

naholyr commented Nov 4, 2016

Just threw this updated version in my ST3 to enable TernJS. Using Linux, and not having "node" but "6" as my default alias, this works well (not supporting local .nvmrc, I was just interested in having a working TernJS):

# $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

@brenthosie
Copy link

brenthosie commented Feb 7, 2017

epok75 I'm using oh-my-zsh and I think I have the right conversion but it isn't working for me still.

I put a node_env.sublime-build in /Users/{user}/Library/Application Support/Sublime Text 3/Packages/User with this in it:

{
    "shell_cmd": "zsh -c \"source ~/.nvm/nvm.sh && nvm run default $file\"",
    "selector": "source.js",
}

Sublime still tells me:

env: node: No such file or directory

Any ideas? The docs for zsh say:

 -c     Take the first argument as a command to execute, rather than reading commands from  a  script  or
              standard input.  If any further arguments are given, the first one is assigned to $0, rather than
              being used as a positional parameter.

looks similar to me how bash works.

@gblazex
Copy link

gblazex commented Jul 4, 2017

@davidhq worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment