Home Download Buy Blog Forum Support

No Build System for Javascript

No Build System for Javascript

Postby flebber on Sun Jan 08, 2012 12:49 am

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?
flebber
 
Posts: 2
Joined: Sun Jan 08, 2012 12:45 am

Re: No Build System for Javascript

Postby hibbelig on Sun Jan 08, 2012 9:08 pm

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?
hibbelig
 
Posts: 77
Joined: Wed Dec 21, 2011 11:24 pm

Re: No Build System for Javascript

Postby C0D312 on Sun Jan 08, 2012 9:24 pm

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 <script> tags. Then go to Tools > New Plugin. Paste this into the new plugin file:
Code: Select all
import sublime, sublime_plugin
import webbrowser


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


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:
Code: Select all
{ "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!
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: No Build System for Javascript

Postby senzo on Sun Jan 08, 2012 10:14 pm

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.
senzo
 
Posts: 106
Joined: Tue May 24, 2011 9:17 am

Re: No Build System for Javascript

Postby skaet on Mon Jan 09, 2012 7:14 am

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 :)
skaet
 
Posts: 93
Joined: Thu Sep 16, 2010 3:37 pm

Re: No Build System for Javascript

Postby flebber on Thu Jan 26, 2012 1:51 pm

C0D312 wrote: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 <script> tags. Then go to Tools > New Plugin. Paste this into the new plugin file:
Code: Select all
import sublime, sublime_plugin
import webbrowser


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


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:
Code: Select all
{ "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!


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
Code: Select all
build "node" "$File"
but couldn't get it to go.

PS How do I remove bad build systems I have created?
flebber
 
Posts: 2
Joined: Sun Jan 08, 2012 12:45 am


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 18 guests