ReferenceError:窗口未在对象上定义. < anonymous> Node.js [英] ReferenceError : window is not defined at object. <anonymous> Node.js

查看:181
本文介绍了ReferenceError:窗口未在对象上定义. < anonymous> Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了类似的问题,但没有一个与我的情况相符.在我的网站上,我有3个JavaScript文件:client.jsserver.jsmyModule.js.在client.js中,我创建了一个名为windowVar的窗口变量,并添加了一些属性.在myModule.js中,我添加了一些其他属性并在其中使用它们,然后导出文件并在server.js中要求它.

I've seen similar questions that were asked here but none matches my situation. In my web I have 3 JavaScript files : client.js , server.js ,myModule.js . In client.js I create a window variable called windowVar and 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.

client.js:

window.windowVar= {
    func1: function(args) {    
       //some sode here
    },
    counter:0
};

myModule.js:

module.exports={wVar:windowVar, addMessage ,getMessages, deleteMessage};

windowVar.serverCounter = 0;
windowVar.arr1=[];

server.js:

var m= require('./myModule');

在node.js中运行服务器时,出现以下错误:

when running the server in node.js I get the following error:

ReferenceError:未在对象上定义窗口. <anonymous>

据我了解,window是浏览器属性,但是在这种情况下如何解决错误?感谢您的帮助

As I understood window is a browser property ,but how can I solve the error in this case? Any help is appreciated

推荐答案

window是Node上不存在的浏览器.

window is a browser thing that doesn't exist on Node.

如果您确实要创建全局,请改用global:

If you really want to create a global, use global instead:

global.windowVar = /*...*/; // BUT PLEASE DON'T DO THIS, keep reading

global是节点的全局对象标识符,就像window在浏览器中一样.

global is Node's identifier for the global object, like window is on browsers.

但是 ,无需在Node程序中创建真正的全局变量.相反,只需创建一个全局模块:

But, there's no need to create truly global variables in Node programs. Instead, just create a module global:

var windowVar = /*...*/;

...并且由于您将其包含在exports中,因此其他模块可以根据需要访问其引用的对象.

...and since you include it in your exports, other modules can access the object it refers to as necessary.

这篇关于ReferenceError:窗口未在对象上定义. &lt; anonymous&gt; Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆