Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

javascript - Node.js build system in Sublime Text 2

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.js
in my console, I get:
Hello World
However, 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.ext
Like 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! See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
{
    "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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...