How to Create a Javascript Console in Sublime Text
Two Methods:Using JSC (Mac OS X)Using Node.js
Javascript consoles are very handy for debugging and getting live results from your script. Although Sublime Text comes with build systems for many other scripting languages, it does not come with a built-in Javascript build system. Many sources will tell you to create a .html page with a link to the .js file, then use a web browser console to see the results of your code. This equates to constant window-switching and browser reloading; leading to frustration, heartache, and ultimately inefficiency.
Fortunately, constructing your own Javascript build system for Sublime Text is quick and easy!
Steps
Method 1 of 2: Using JSC (Mac OS X)
JSC is a command-line Javascript runner, cooked directly into Mac OS X. Because most Macs already contains everything you need to run the script, creating the build system in Sublime Text is incredibly easy. (If you have a Windows computer, see the directions for Node.js below.)
Creating The Build System
-
1Launch Sublime Text.Ad
-
2Go to "Tools > Build System > New Build System" in the top bar.
-
3Paste this code into the resulting new tab that Sublime Text opened, replacing anything else in it:
{ "cmd": ["/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc", "$file"], "selector": "source.js" }
-
4Save the file as "JSC.sublime-build" in the default "user" folder. Now you have created your build system!Ad
Usage
-
1Open the Javascript file that you want to run in Sublime Text.
-
2Use
debug()
instead ofconsole.log()
in your script. -
3Go to "Tools > Build System" in the top bar and select "JSC". This is the build system that you just created.
-
4Build the Javascript file, using either the shortcut (Ctrl+B for Windows, and ⌘ Command+B for Mac), or by choosing "Build" from the "Tools" menu. A console will now appear in a pane at the bottom of the window, showing the results of your script!Ad
Method 2 of 2: Using Node.js
Node.js (Node) is a platform built to allow Javascript to run on a server. However, it can also be installed on your local computer, providing a relatively simple way to run Javascript and get the results without using a browser.
-
1Download the Node installer from the project's homepage and run it. Simply use the default settings.
-
2Go to "Tools > Build System > New Build System" in the top bar.
-
3Paste this code into the resulting new tab that Sublime Text opened, replacing anything else in it:
{ "cmd": ["node", "$file"], "selector": "source.js" }
- If you receive a "[Errno 2]" error, then you'll need to change "
node
" in the code above to the path where node is located. To do this, open terminal and run "which node
". This will print out the path to the node binary.
- If you receive a "[Errno 2]" error, then you'll need to change "
-
4Save the file as "node.sublime-build" in the default "user" folder. Now you have created your build system!Ad
Usage
-
1Open the Javascript file that you want to run in Sublime Text.
-
2Go to "Tools > Build System" in the top bar and select "node". This is the build system that you just created.
-
3Build the Javascript file, using either the build shortcut (Ctrl+B for Windows, and ⌘ Command+B for Mac), or by choosing "Build" from the "Tools" menu. A console will now appear in a pane at the bottom of the window, showing the results of your script!Ad
Things You’ll Need
- A computer
- Sublime Text (a programming text editor)
- An internet connection and browser (for Node.js method)
Article Info
Categories: JavaScript
Thanks to all authors for creating a page that has been read 59,284 times.
About this wikiHow