Sublime Forum

No Build System for Javascript

#1

Hi I am using Sublime text version 2 build 2139.

Currently learning javascript and wanted to run a javascript file to see the output. However every time I select ‘build’ (assume this is what I should be doing) to get the output of the file all I receive is an error that there is no build system.

is there an easy way to execute javascript files in sublime?

0 Likes

#2

If you know how to “run” a JS file from the command line, you can add your own build system to do the same from ST. It’s not hard.

I put “run” in quotes because I am not sure what it means to run a JS file. With Rhino? With cscript on Windows? In a browser?

0 Likes

#3

Well, if you are looking to open javascript in your web browser, your best bet would be to save the javascript in an html file and surround it in tags. Then go to Tools > New Plugin. Paste this into the new plugin file: [code]import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self,edit):
url = self.view.file_name()
webbrowser.open_new(url)[/code]

Save the file as openinbrowser.py (it really doesn’t matter what you name it though).

Then go to “Preferences > Key Bindings - User”, and add the following: { "keys": "ctrl+shift+b"], "command": "open_browser" }

Now when you are working on the javascript, you can press “control + shift + b” to open the file in your browser.

Hope that helps!

0 Likes

#4

On OS X you have a Javascript command-line Interface pre installed here: /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc
Other options are Spidermonkey, Rhino or V8
There is however no DOM available, which will most likely make this approach unusable. There are solutions to simulate a browser environment like env.js but i am not sure how this works.

0 Likes

#5

If you’re learning Javascript, you’re more than likely learning it in context of the web browser and HTML. For this purpose, C0D312’s solution will do it simply and elegantly. I understand the workflow of using the “build” system in ST2 however it seems overly fancy when alt-tab + F5 will do the trick :smile:

0 Likes

#6

[quote=“C0D312”]Well, if you are looking to open javascript in your web browser, your best bet would be to save the javascript in an html file and surround it in tags. Then go to Tools > New Plugin. Paste this into the new plugin file: [code]import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
def run(self,edit):
url = self.view.file_name()
webbrowser.open_new(url)[/code]

Save the file as openinbrowser.py (it really doesn’t matter what you name it though).

Then go to “Preferences > Key Bindings - User”, and add the following: { "keys": "ctrl+shift+b"], "command": "open_browser" }

Now when you are working on the javascript, you can press “control + shift + b” to open the file in your browser.

Hope that helps![/quote]

This is a brilliant solution.

After playing around though I would also like the option of executing the build with node. I have tried to add versions to the build system but they have not worked. How could I execute with node? Was reading this http://www.sublimetext.com/docs/build
so I tried a few different versions of

build "node" "$File" but couldn’t get it to go.

PS How do I remove bad build systems I have created?

0 Likes

#7

Still no default build system for JavaScript afaik. I know we can add a build system, but why isn’t Sublime simply making it trivial to run/debug code (of all major languages) for less sophisticated users?

0 Likes

#8

Hi, I use this plugin for JS in ST. Hope it works out for you. It is the best thing you can use to learn javascript.

0 Likes

#9

I would imagine it’s tricky to get a build that will work for everyone because perhaps you just node blah.js or perhaps you npm dothething or you might yarn makeathingdoathing. So, whichever one you pick you’re excluding someone, and even for cases where you launch via npm or yarn is there a standardization on exactly what script is used to launch?

0 Likes

#10

He could use Quokka.js in ST. I seen Travesty Media and James Quick covering it on youtube. Its a javascript scratchpad plugin.

0 Likes