Using Node to run JavaScript from Sublime Text

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.

Serpents

Album cover for RaintownNow playing:
Deacon Blue - He Looks Like Spencer Tracy Now
(from Raintown)


Loading similar posts...   Loading links to posts on similar topics...

7 Responses

 avatar
#1 clr said...
26-Mar-16 11:31 AM

Thanks 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"?

julian m bucknall avatar
#2 julian m bucknall said...
26-Mar-16 1:31 PM

@clr: the alert() function is provided by a browser's global window object. It is not available with node.js since it is not a browser and has no UI. Use console.log() instead.

Cheers, Julian

 avatar
#3 clr said...
27-Mar-16 10:56 AM

Thanks! I figured that out right after I posted. D'oh. At any rate, this works great!

 avatar
#4 Stas said...
27-Sep-16 12:58 AM

I prefer to use Codelobster for it: http://www.codelobster.com

julian m bucknall avatar
#5 julian m bucknall said...
01-Oct-16 5:36 PM

Stas: 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

 avatar
#6 Christine Doole said...
16-Apr-18 6:01 AM

How do I do this in sublime text 3 on windows 10

julian m bucknall avatar
#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.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response