Node.js文档未定义

时间:2022-11-05 14:45:10

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 not defined'。我能做什么?

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

4 个解决方案

#1


13  

document relates to the DOM (Document Object Model) in a web browser.

文档涉及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 to include Node.js modules in your client-side code.

你可以得到的最接近的是使用像browserify这样的东西在你的客户端代码中包含Node.js模块。

#2


4  

You could use JSDom to add Dom support to Node. To make a variable global you can use either

您可以使用JSDom向Node添加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.

我希望这是回答你的问题。

#3


0  

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...

好的,简短的回答是你想访问仅在窗口和前端侧可用的文件对象,不要忘记在服务器和节点端没有访问权限的文件=== 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获取root元素会抛出错误,而是尝试从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 document and window object in node.js as they are not available in node.js...

简短的回答是不要在node.js中使用document和window对象,因为它们在node.js中不可用...

Using Domino could 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.

顾名思义,多米诺的目标是在Node中提供DOM。

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.

与原始的dom.js项目相比,domino不是为运行不受信任的代码而设计的。因此,它不必隐藏代理外观背后的内部,这使得代码不仅更简单,而且性能更高。

Domino currently doesn't use any harmony features like proxies or WeakMaps and therefore also runs in older Node versions.

Domino目前不使用代理或WeakMaps等任何和声功能,因此也可以在较旧的Node版本中运行。

For more info, visit here...

欲了解更多信息,请访问此处...

#4


0  

  • "npm install npm -g"
  • “npm install npm -g”
  • after that "npm install -g typescript"
  • 之后“npm install -g typescript”

this command helped me for this problem

这个命令帮助我解决了这个问题

#1


13  

document relates to the DOM (Document Object Model) in a web browser.

文档涉及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 to include Node.js modules in your client-side code.

你可以得到的最接近的是使用像browserify这样的东西在你的客户端代码中包含Node.js模块。

#2


4  

You could use JSDom to add Dom support to Node. To make a variable global you can use either

您可以使用JSDom向Node添加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.

我希望这是回答你的问题。

#3


0  

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...

好的,简短的回答是你想访问仅在窗口和前端侧可用的文件对象,不要忘记在服务器和节点端没有访问权限的文件=== 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获取root元素会抛出错误,而是尝试从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 document and window object in node.js as they are not available in node.js...

简短的回答是不要在node.js中使用document和window对象,因为它们在node.js中不可用...

Using Domino could 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.

顾名思义,多米诺的目标是在Node中提供DOM。

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.

与原始的dom.js项目相比,domino不是为运行不受信任的代码而设计的。因此,它不必隐藏代理外观背后的内部,这使得代码不仅更简单,而且性能更高。

Domino currently doesn't use any harmony features like proxies or WeakMaps and therefore also runs in older Node versions.

Domino目前不使用代理或WeakMaps等任何和声功能,因此也可以在较旧的Node版本中运行。

For more info, visit here...

欲了解更多信息,请访问此处...

#4


0  

  • "npm install npm -g"
  • “npm install npm -g”
  • after that "npm install -g typescript"
  • 之后“npm install -g typescript”

this command helped me for this problem

这个命令帮助我解决了这个问题