Node.js 文档未定义 [英] Node.js document is not defined
问题描述
为什么 node.js 无法识别 document.GetElementById?它说参考错误:文档未定义".我能做什么?
Why node.js does not recognize document.GetElementById? It says 'ReferenceError: document is not defined'. What can I do?
ReferenceError: document is not defined
at Object.<anonymous> (C:UsersDesktopmain.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
推荐答案
document
与 Web 浏览器中的 DOM(文档对象模型)相关.
document
relates to the DOM (Document Object Model) in a web browser.
然而,Node.js 不是浏览器环境.它是一个服务器环境,很像 PHP 或 Perl,因此,您无法访问浏览器的 DOM 或执行任何特定于浏览器托管的 JavaScript.
Node.js, however, is not a browser environment. It is a server environment, much like PHP or Perl, and as such, you can’t access the browser’s DOM or do anything specific to browser-hosted JavaScript.
最接近的是使用类似 browserify 在客户端代码中包含 Node.js 模块.
The closest you could get is using something like browserify to include Node.js modules in your client-side code.
这篇关于Node.js 文档未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!