I justed started learning JavaScript. While doing that, I got tired of embedding my JavaScript code into an HTML document in order to run it in the browser. I thought it would be nice to just run my scripts right in Sublime's console, so I wouldn't have to leave the editor. Therefore I was trying to create a JavaScript build system, since Sublime doesn't come with one.
My idea was to use Node.js as the JavaScript interpreter. I installed it with the package manager of Linux Mint. As far as I can say it works just fine. Let's say I have a file test.js containing the following line of JavaScript code:
console.log("Hello World");
When I run
nodejs /path/to/test.jsin my console, I get:
Hello WorldHowever, I don't get this to work with Sublime. I created a new Build system by clicking Tools / Build System / New Build System. I then typed in the following lines:
{
"cmd": ["nodejs", "$file"]
} As far as I know, this one line is the JSON representation of the following command: nodejs /path/to/current/file.extLike I said, if I run this manually in the console, it works just fine. If I press F7 in Sublime, which is the shortcut for Build, Sublime's console shows up. It's empty though.
There's another weird thing. Even though the (non-existing) output of Sublime's console indicates that the build system isn't configured to correctly work with Node.js, I got some Node.js errors displayed when I accidentally tried to run non-JS files such as the Node.sublime-build file. This is the output displayed in Sublime's console:
/home/baerenfaenger/.config/sublime-text-2/Packages/User/Node.sublime-build:2
"cmd": ["nodejs", "$file"]
^
module.js:434
var compiledWrapper = runInThisContext(wrapper, filename, true);
^
SyntaxError: Unexpected token :
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:32)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:39)
[Finished in 0.1s with exit code 1]
So why do I not get any output when executing actual JavaScript code? Thank you in advance!
{
"cmd": ["node","$file"]
}
works perfectly for me in windows and it shows the output in the sublime window. Although I dont know how to stop the server after from sublime, I have to search and kill the node process.
Also, check node is in your PATH ;)
Hope it works for you.
Mac users, type which node in terminal. It outputs the path of the node executable, which in most cases is /usr/local/bin/node. Now create a new build system (Tools > Build System > New Build System) with the following settings in Sublime Text.
{
"cmd": ["/usr/local/bin/node", "$file"],
"selector": "source.js"
}
Tested on Ubuntu Linux. Use a file selector as below:
{
"cmd": ["node", "$file"],
"selector": "*.js"
}
The method #2 of the link below solved for me:
http://www.wikihow.com/Create-a-Javascript-Console-in-Sublime-Text
Here is a tip for Windows,
Most of the answers from this thread is correct but none is complete and at least no answer tells about how to kill existing Node process even if you are able to run node in sublime. So eventually if either of above approach worked for you, You will end up manually killing Node always.
Here is What I have done for 'Windows':
taskkill /F /IM "node.exe" "c:/Program Files/nodejs/node.exe" %1
Enjoy noding.
I can only see the console output only if the file exists/saved on disk. (for anonymous file I don't see the output either)
Hope this helps
May be it depends on environment variables -- try to use full path to node instead of just "nodejs". It worked for me on Mac OS (e.g. /usr/local/node instead of just node)
If all else fails, check Node Supervisor....... you only need to change the code and the server will reboot it self so you can see the changes live =)
Normally
"cmd":["node",$file]
would work. However there is no way to stop the server than. I've found out the best solution which opens server in new console and the closes it automatically. I also works fine with supervisors or node-inspector
"cmd" : ["start", "cmd.exe", "/C", "node", "$file"]
I hope It'll help
On OSX/Linux, to first kill the running server, create a build system with the following line:
{
"shell_cmd": "pkill node ; cd '<path_to_app_root>' ; node bin/www"
}