Javascript ReferenceError : 窗口未在对象中定义。<匿名> Node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45964178/
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
ReferenceError : window is not defined at object. <anonymous> Node.js
提问by user8244016
I've seen similar questions that were asked here but none matches my situation. In my web I have 3 JavaScriptfiles : client.js, server.js,myModule.js. In client.jsI create a window variable called windowVarand I add to it some atrributes. In myModule.js,I add some other attributes and use them there and I export the file and require it in server.js.
我看过这里提出的类似问题,但没有一个符合我的情况。在我的网络中,我有 3 个JavaScript文件:client.js, server.js, myModule.js. 在client.js我创建一个名为的窗口变量windowVar,并向其添加一些属性。在 中myModule.js,我添加了一些其他属性并在那里使用它们,然后导出文件并在server.js.
client.js:
client.js:
window.windowVar= {
func1: function(args) {
//some sode here
},
counter:0
};
myModule.js:
myModule.js:
module.exports={wVar:windowVar, addMessage ,getMessages, deleteMessage};
windowVar.serverCounter = 0;
windowVar.arr1=[];
server.js:
server.js:
var m= require('./myModule');
when running the server in node.js I get the following error:
在 node.js 中运行服务器时,出现以下错误:
ReferenceError : window is not defined at object.
<anonymous>
ReferenceError : 窗口未在对象中定义。
<anonymous>
As I understood window is a browser property ,but how can I solve the error in this case? Any help is appreciated
据我了解 window 是一个浏览器属性,但是在这种情况下我该如何解决错误?任何帮助表示赞赏
回答by T.J. Crowder
windowis a browser thing that doesn't exist on Node.
window是 Node.js 上不存在的浏览器。
If you really want to create a global, use globalinstead:
如果您真的想创建一个global,请global改用:
global.windowVar = /*...*/; // BUT PLEASE DON'T DO THIS, keep reading
globalis Node's identifier for the global object, like windowis on browsers.
global是 Node 的全局对象标识符,就像window在浏览器上一样。
But, there's no need to create truly global variables in Node programs. Instead, just create a module global:
但是,没有必要在 Node 程序中创建真正的全局变量。相反,只需创建一个全局模块:
var windowVar = /*...*/;
...and since you include it in your exports, other modules can access the object it refers to as necessary.
...并且由于您将它包含在您的 中exports,因此其他模块可以根据需要访问它所引用的对象。
回答by CommonSenseCode
I used something like this and it protects against the error:
我使用了这样的东西,它可以防止错误:
let foo = null;
if (typeof window !== "undefined") {
foo = window.localStorage.getItem("foo");
}
回答by Arun Redhu
windowobject is present only in the context of browser. When running application on nodejsno windowobject is available. If you want to share your variables or functions across multiple files then you have to use requireand exports
window对象仅存在于浏览器的上下文中。在nodejs没有window可用对象上运行应用程序时。如果要跨多个文件共享变量或函数,则必须使用require和exports
client.js
客户端.js
module.exports = {
fun1: function(){
},
counter: 0
}
and something like in myModule.js
和类似在 myModule.js 中的东西
var client = require('./client');

