相关文章推荐
热情的皮带  ·  QChart 和QChartView ...·  3 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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');

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

ReferenceError : window is not defined at object. <anonymous>

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

Well, that file seems to be running on the server, not the browser, and there is no window on the server? Node does have a global object, but why would you use it. – adeneo Aug 30, 2017 at 15:20

window is a browser thing that doesn't exist on Node.js, but ES2020 introduced globalThis, which (being part of the JavaScript specification) is available on both compliant browser engines and in Node.js.

If you really want to create a global in Node.js, use globalThis or (for older versions) global:

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

global is Node's identifier for the global object (defined in their API before globalThis existed), like window is on browsers. For code that may run in a wide range of environments, including older ones:

const g = typeof globalThis === "object"
    ? globalThis
    : typeof window === "object"
        ? window
        : typeof global === "object"
            ? global
            : null; // Causes an error on the next line
g.windowVar = /*...*/;

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

let /*or `const`*/ windowVar = /*...*/;

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

hi , thanks for helping me one more time .. the problem is that I am allowed to use only one global variable – user8244016 Aug 30, 2017 at 15:23 @user8244016: There's only one global variable above. But I can't imagine why you'd need any global variables. – T.J. Crowder Aug 30, 2017 at 15:25 @user8244016: "this is a requirement in my project" I very much doubt it; and if it is, then this must be for a class, and it won't have come out of nowhere. Review your class notes, etc. In any case, if you actually did the above, it would create a global. So clearly, if it's disappearing, you didn't do the above. I suggest debugging to find out what's wrong. – T.J. Crowder Aug 30, 2017 at 15:31 The reason to have window in node.js is to emulate for scripts that expect to run in a browser. – jgmjgm Sep 11, 2019 at 9:32

I used something like this and it protects against the error:

let foo = null;
if (typeof window !== "undefined") {
  foo = window.localStorage.getItem("foo");

window object is present only in the context of browser. When running application on nodejs no window object is available. If you want to share your variables or functions across multiple files then you have to use require and exports

client.js

module.exports = {
    fun1: function(){
    counter: 0 

and something like in myModule.js

var client = require('./client');

Sometimes, when you are using custom-project, and don't need "window" object at all, just define it with blank, before all other contents:

window = {};
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.