Mar 15
2013

Adding a JS build-system to Sublime

Frustratingly, the Sublime Text editor doesn’t set up a build system for JavaScript the way it does for Ruby and some other languages. Fortunately, it’s an easy problem to fix. Assuming your JS interpreter is Node.js, these are the steps to set it up:

  • Open the Tools>Build System menu and select “new build system”
  • Name it Node, and a new file called node.sublime-build will be created.
  • Add the following data to it and save:
  • {
        "cmd": ["node", "$file", "$file_base_name"],
        "working_dir": "${project_path:${folder}}",
        "selector": "*.js"
    }
    
    • Restart Sublime
    • Select Node as the build system for your file and hit cmd + b to build from Sublime!

    Here’s an example JS file to run:

    console.log("Hi, I'm your program's output.");
    

    Video walkthrough:

    For more Sublime goodies, see:
    Notes from my talk at Hack Reactor
    Strip trailing whitespace every time you save a file in Sublime Text

This entry was tagged , .
Bookmark the permalink.

12 Responses to Adding a JS build-system to Sublime

  1. Hippo says:

    Or you could simply use a Makefile. Sublime has support for make. ;-)

  2. Pingback: Adding a JS build-system to Sublime | Logic Mason | Lucas Kraus: Blog

  3. Bad Andy says:

    This works a treat, mate! Exactly what I was looking for. Cheers!

  4. Phil says:

    You have to save the file before it actually works, by the way…

  5. George says:

    This works great, but for some reason ST is not recognizing the build and using it automatically with .js files. I have to manually click into the build menu and change the build system to “Javascript” for it to work. Do you know why Automatic is not recognizing it as valid for .js files?

  6. George says:

    Nevermind, I actually figured it out. For “selector” you need to to use source.js instead of *.js. This fixed it at least on my machine.

  7. Steve says:

    I had a quick question on this.

    I am using OSX, and have node.js installed. After setting the build system up I save the file and run it, but it gives me this as the output:

    [Errno 2] No such file or directory
    [cmd: [u'node', u'/Users/XXX/test.js', u'test']]
    [dir: /Users/XXX]
    [path: /usr/bin:/bin:/usr/sbin:/sbin]
    [Finished]

    I copied the build system data exactly as it appears above. Any advice?

  8. Mai says:

    Steve,

    I know posted some time ago, but did you restart Sublime Text after you made the new build? Should work if you restart.

  9. Branislav Dinic says:

    Thx, node was not working on automatic build!

  10. Ron says:

    If you get Errno 2] No such file or directory
    then replace “node” with the path to node. ex: /usr/local/bin/node

  11. Some people have errors because path is in other dir, for this peoples use this:
    {
    “cmd”: ["/usr/local/bin/node", "$file", "$file_base_name"],
    “working_dir”: “${project_path:${folder}}”,
    “selector”: “*.js”
    }

  12. RJNY86 says:

    This worked for me

    {
    “cmd”: ["node", "$file"],
    “selector”: “source.js”
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>