Frustratingly, the Sublime Text editor doesn't set up a build system for JavaScript the way it does for Ruby and some other languages. Fortunately, it's an easy problem to fix. Assuming your JS interpreter is Node.js, these are the steps to set it up:
- Open the Tools>Build System menu and select "new build system"
- Name it Node, and a new file called node.sublime-build will be created.
- Add the following data to it and save:
{ "cmd": ["node", "$file", "$file_base_name"], "working_dir": "${project_path:${folder}}", "selector": "*.js" }
- Restart Sublime
- Select Node as the build system for your file and hit cmd + b to build from Sublime!
Here's an example JS file to run:
console.log"Hi, I'm your program's output.");
Video walkthrough:
For more Sublime goodies, see:
Notes from my talk at Hack Reactor
Strip trailing whitespace every time you save a file in Sublime Text
Or you could simply use a Makefile. Sublime has support for make. 😉
This works a treat, mate! Exactly what I was looking for. Cheers!
You have to save the file before it actually works, by the way…
This works great, but for some reason ST is not recognizing the build and using it automatically with .js files. I have to manually click into the build menu and change the build system to “Javascript” for it to work. Do you know why Automatic is not recognizing it as valid for .js files?
Nevermind, I actually figured it out. For “selector” you need to to use source.js instead of *.js. This fixed it at least on my machine.
I had a quick question on this.
I am using OSX, and have node.js installed. After setting the build system up I save the file and run it, but it gives me this as the output:
[Errno 2] No such file or directory
[cmd: [u’node’, u’/Users/XXX/test.js’, u’test’]]
[dir: /Users/XXX]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
[Finished]
I copied the build system data exactly as it appears above. Any advice?
Steve,
I know posted some time ago, but did you restart Sublime Text after you made the new build? Should work if you restart.
Thx, node was not working on automatic build!
If you get Errno 2] No such file or directory
then replace “node” with the path to node. ex: /usr/local/bin/node
Some people have errors because path is in other dir, for this peoples use this:
{
“cmd”: [“/usr/local/bin/node”, “$file”, “$file_base_name”],
“working_dir”: “${project_path:${folder}}”,
“selector”: “*.js”
}
This worked for me
{
“cmd”: [“node”, “$file”],
“selector”: “source.js”
}
Thanks man it works for me as the original
Thank you this one is working perfect for me.
For me this was enough as well:
{
“cmd”: [“node”, “$file”],
“selector”: “source.js”
}
And using source.js instead of *.js really makes automatic detection.
Solution for “No such file or directory” if you’re using nvm:
https://gist.github.com/joesepi/11269417#gistcomment-1567508
Consider using nvm alternative https://github.com/isaacs/nave to avoid this problem.
having some issues ? not working as i want it to …
basically it needs a console.log for everything otherwise it won’t output
something that would output when calling the function or variable directly .. without console.log or debug
like
var a = ‘test’
a;
would output test.