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"])
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