-
-
Save joesepi/11269417 to your computer and use it in GitHub Desktop.
# 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"]) |
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
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
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",
}
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
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.
@davidhq worked
I might have a different version of NVM, but I had to update the path structure as follows: