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 with node 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
Add a ment  | 

1 Answer 1

Reset to default 2

document 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