A quick one, more for my benefit next time I have to set this up in the future. Sometimes, I’m writing some JavaScript that can be divorced from a web page. Maybe it’s a weird bit of code, maybe I’m experimenting with (say) functional programming, maybe it’s just a small self-contained function, but I’d really like to test it right there and then, rather than copy/paste it and use the developer tools in my browser.
For Sublime Text, we can set up a “build system” to do this. First install node.js on your PC, you’ll be using it to run arbitrary JavaScript files. To do this via the command line, you’d simply enter node myJavaScriptFile.js
, and so you’ll be mimicking that within Sublime Text.
In Sublime Text 2 now, navigate to Tools | Build System | New Build System. You’ll get a bare bones JSON configuration file called untitled.sublime-build
with a single command in it.
{ "cmd": ["make"] }
Change this to
{ "cmd": ["node", "$file"], "selector": "source.js" }
and save as node.sublime-build
in the folder it suggests (C:\Users\<username>\AppData\Roaming\Sublime Text 2\Packages\User
). The command there specifies to call node
with the current file as the parameter. The selector
property tells Sublime Text that this build system should automatically apply to any source files with a .js
extension. (I have seen this recommended as *.js
but it certainly didn’t work for me. Looking at the default build system files installed with Sublime Text, that’s just wrong.)
After saving that, hitting the Build key (F7 or Ctrl+B) in Sublime Text when editing a JavaScript file will execute that file with node.js. The output will appear in the Output wIndow.
Now playing:
Deacon Blue - He Looks Like Spencer Tracy Now
(from Raintown)
7 Responses
#1 clr said...
26-Mar-16 11:31 AMThanks for the steps! Seems like it should work but am getting errors trying to run any js. I performed steps above, made a function in a js file with nothng more then an alert() call, then ran it as directed above. I get an error about "alert is not defined"?
#2 julian m bucknall said...
26-Mar-16 1:31 PM@clr: the
alert()
function is provided by a browser's globalwindow
object. It is not available with node.js since it is not a browser and has no UI. Useconsole.log()
instead.Cheers, Julian
#3 clr said...
27-Mar-16 10:56 AMThanks! I figured that out right after I posted. D'oh. At any rate, this works great!
#4 Stas said...
27-Sep-16 12:58 AMI prefer to use Codelobster for it: http://www.codelobster.com
#5 julian m bucknall said...
01-Oct-16 5:36 PMStas: Er... /scratches his head/ Why would an IDE for PHP (no matter how good) help me run stand-alone JavaScript files using node.js from my editor? Yes, I get that it supports highlighting and autocomplete for JavaScript, but that's not what this post is about. And my blog engine is not written in PHP either.
Here's a webinar I did on Functional JavaScript where I make use of being able to execute code from within Sublime Text.
Cheers, Julian
#6 Christine Doole said...
16-Apr-18 6:01 AMHow do I do this in sublime text 3 on windows 10
#7 julian m bucknall said...
16-Apr-18 10:37 AM@Christine: Shortly after writing this post, I switched to Visual Studio Code. I no longer use Sublime Text, and in fact I no longer have it installed.
Cheers, Julian
Leave a response
Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.
_emphasis_
**strong**
[text](url)
`IEnumerable`
* an item
1. an item
> Now is the time...
Preview of response