admin管理员组文章数量:1334133
I have searched and found a way to make JavaScript works on SublimeText. The way is like that in steps:
- Sublime Text > File > New File > Type: console.log("Hello World");
- Press Ctrl + S (Save) > script.js
Install Node.js
Sublime Text > Tools > Build System > New Build System > Type:
{
"cmd": ["node", "$file"],
"selector": "source.js"
}
I have searched and found a way to make JavaScript works on SublimeText. The way is like that in steps:
- Sublime Text > File > New File > Type: console.log("Hello World");
- Press Ctrl + S (Save) > script.js
Install Node.js
Sublime Text > Tools > Build System > New Build System > Type:
{
"cmd": ["node", "$file"],
"selector": "source.js"
}
- Save As Node.sublime-build
- Sublime Text > Tools > Build System > Node
That worked well and I can get the console results well
Now how can I use or refer to the index.html file?
To be more specific I have two files: index.html and script.js and I need to run the JS on sublime text but I got
ReferenceError: document is not defined
on such a line
var output = document.getElementById('output');
Share
Improve this question
edited May 8, 2020 at 14:46
YasserKhalil
asked May 8, 2020 at 14:26
YasserKhalilYasserKhalil
9,56813 gold badges59 silver badges130 bronze badges
4
-
1
There is no
document
object in Node.js since it's only available in browser runtimes. – Tsvetan Ganev Commented May 8, 2020 at 14:48 -
1
Why don't you simply write your file like
script.js
then execute it in a terminal withnode script.js
? Also, not sure what you're trying to do, because Node is a server-side technology, there is no DOM, no elements by ID to select. – Jeremy Thille Commented May 8, 2020 at 14:52 -
1
I need to run the JS on sublime text
Why do you need to run Javascript from within a text editor? Text editors are meant to edit text, not to run javascript. Why don't you just drop your HTML file in a browser? – Jeremy Thille Commented May 8, 2020 at 14:54 - Thanks a lot for replies. The idea is to have all in one container. I mean to have the index.html and the script.js in one folder and use the Sublime Text as IDE :) – YasserKhalil Commented May 8, 2020 at 15:27
1 Answer
Reset to default 2document object. relates to the DOM(Document Object Model) in the Web Browsers. Node.js
is desktop JavaScript
runtime environment, It'll not give to access to DOM objects.
If you want to access the document, object in Node.js you can use browserify
本文标签: Run JavaScript in Sublime TextStack Overflow
版权声明:本文标题:Run JavaScript in Sublime Text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358319a2459860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论