Javascript Node.js 文档未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32126003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js document is not defined
提问by Ela
Why node.js does not recognize document.GetElementById? It says 'ReferenceError: document is not defined'. What can I do?
为什么 node.js 无法识别 document.GetElementById?它说“参考错误:文档未定义”。我能做什么?
ReferenceError: document is not defined
at Object.<anonymous> (C:\Users\Desktop\main.js:9:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
采纳答案by brandonscript
document
relates to the DOM (Document Object Model) in a web browser.
document
与 Web 浏览器中的 DOM(文档对象模型)有关。
Node.js, however, is not browser Javascript. It is a server, much like PHP or Perl, and as such, you can't access the browser's DOM or do anything specific to browser-based Javascript.
然而,Node.js 不是浏览器 Javascript。它是一个服务器,很像 PHP 或 Perl,因此,您无法访问浏览器的 DOM 或执行任何特定于基于浏览器的 Javascript 的操作。
The closest you could get is using something like browserifyto include Node.js modules in your client-side code.
您可以获得的最接近的是使用诸如browserify 之类的东西在您的客户端代码中包含 Node.js 模块。
回答by KOsimo
You could use JSDomto add Dom support to Node. To make a variable global you can use either
您可以使用JSDom向Node.js添加 Dom 支持。要使变量成为全局变量,您可以使用
GLOBAL.document = new JSDOM(html).window.document;
or
或者
global.document = new JSDOM(html).window.document;
where html
is your website as a string.
html
您的网站在哪里作为字符串。
To use JSDom include it in your project with:
要使用 JSdom,请将其包含在您的项目中:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
or in plain js with:
或在普通 js 中:
var jsdom = require("jsdom");
var JSDOM = jsdom.JSDOM;
I hope this is answering your question.
我希望这能回答你的问题。
回答by Chris Bao
To understand the answer, it's necessary to know the relationship of: Javascript Engine, Browserand Node.js.
要理解这个答案,有必要知道:Javascript Engine、Browser和Node.js之间的关系。
Javascript Engine: is Javascript compiler which turns JS into machine code. For example, V8, is a great one. Technically V8 is developed by C++ (you can regard it as a C++ program).
Javascript 引擎:是将 JS 转换为机器代码的 Javascript 编译器。例如,V8就是一个很好的例子。从技术上讲,V8 是由 C++ 开发的(您可以将其视为 C++ 程序)。
V8 implements ECMAScript, which a standard of Javascript language defining the features and functionalities of JS.
V8 实现了ECMAScript,这是一种定义 JS 特性和功能的 Javascript 语言标准。
But DOMoperation is not defined by ECMAScript. So V8 doesn't support it.
但是DOM操作不是由 ECMAScript 定义的。所以V8不支持它。
Browser: And developers can use document
for DOM operation in browser, because DOM operation is provided by browser, for example: Chrome.
浏览器:开发者可以document
在浏览器中进行DOM操作,因为DOM操作是由浏览器提供的,例如:Chrome。
Chrome is also developed by C++ and V8(as mentioned abvoe, which is developed by C++ as well) is embedded into Chrome to interpret Javascript. So Chrome expends or adds features to Javascript by binding JS command and C++ implementation together.
Chrome 也是由 C++ 开发的,并且 V8(如前所述,它也是由 C++ 开发的)被嵌入到 Chrome 中来解释 Javascript。因此,Chrome 通过将 JS 命令和 C++ 实现绑定在一起来扩展或添加功能到 Javascript。
Nodejs: different from the Chrome, it is a server side program. But the same thing is that Nodejs is developed by C++ and V8 is embedded into Nodejs to handle js. Nodejs expands features of Javascript in the similar way with Chrome. But since server side doesn't need to handle DOM, so you can not access such functions inside Nodejs.
Nodejs:不同于Chrome,它是一个服务器端程序。但同样的事情是,Nodejs 是用 C++ 开发的,V8 被嵌入到 Nodejs 中来处理 js。Nodejs 以与 Chrome 类似的方式扩展了 Javascript 的功能。但是由于服务端不需要处理DOM,所以你不能在Nodejs内部访问这些功能。
回答by Alireza
OK, shorter answer is you wanna access document
Object which is only available in the window and front end side, don't forget that document
=== window.document
which you don't have access in the server and node side...
好的,简短的回答是您想访问document
仅在窗口和前端可用的对象,不要忘记document
===window.document
在服务器和节点端您无权访问...
So never try something like this on your node side for example getting root element by ID which will throw the error, instead try to access it from FrontEnd:
所以永远不要在你的节点端尝试这样的事情,例如通过 ID 获取根元素会抛出错误,而是尝试从 FrontEnd 访问它:
document.getElementById('root');
will throw an Error:
会抛出错误:
ReferenceError: document is not defined
at Object.<anonymous> (C:\Users\Desktop\app.js:12:50)
at Module._compile (my.js:490:34)
at Object.Module._extensions..js (my.js:518:10)
at Module.load (my.js:555:42)
at Function.Module._load (my.js:610:12)
at Function.Module.runMain (my.js:701:10)
at startup (node.js:899:16)
at node.js:901:3
The short answer is don't use documentand windowobject in node.js
as they are not available in node.js
...
简短的回答是不要使用文档和窗口对象,node.js
因为它们在node.js
...
Using Dominocould help in some cases for accessing the dom...
在某些情况下,使用Domino可以帮助访问 dom...
As the name might suggest, domino's goal is to provide a DOM in Node.
In contrast to the original dom.js project, domino was not designed to run untrusted code. Hence it doesn't have to hide its internals behind a proxy facade which makes the code not only simpler, but also more performant.
Domino currently doesn't use any harmony features like proxies or WeakMaps and therefore also runs in older Node versions.
顾名思义,domino 的目标是在 Node.js 中提供一个 DOM。
与最初的 dom.js 项目相比,domino 并不是为运行不受信任的代码而设计的。因此,它不必将其内部隐藏在代理外观后面,这使代码不仅更简单,而且性能更高。
Domino 目前不使用任何像代理或 WeakMaps 这样的和谐特性,因此也可以在较旧的 Node 版本中运行。
For more info, visit here...
欲了解更多信息,请访问这里...
回答by Radhika
- "
npm install npm -g
" - after that "
npm install -g typescript
"
- “
npm install npm -g
” - 在那之后“
npm install -g typescript
”
this command helped me for this problem
这个命令帮助我解决了这个问题