Error: ReferenceError: window is not defined in NodeJs

Dung Do Tien May 28 2022 229

Hello Guys. I started learning Nodejs yesterday. And I used express server that helps create a server with port 5000. I want to detect the device of the end-user to return something different. Like this:

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    let height = 0;
    height = window.innerHeight; 
    if(height <= 768){
        res.send('Hello Mobile');
    }

    if(height > 768 && height <= 920){
        res.send('Hello Tablet');
    }

    if(height > 920){
        res.send('Hello Desktop');
    }
});

var server = app.listen(5000, function () {
    console.log('Server is running..');
})

But when I ran the command "node index.js" I got an exception throw  ReferenceError: window is not defined.

ReferenceError: window is not defined
    at C:\Users\conta\source\repos\NodeJs\Demo1\index.js:6:5
    at Layer.handle [as handle_request] (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\index.js:280:10)
    at expressInit (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\Users\conta\source\repos\NodeJs\Demo1\node_modules\express\lib\router\layer.js:95:5)

I understand that the Nodejs is also in javascript. If true why window object is not defined?

Anyone can help me solve this issue?

Thank you in advance.

Have 2 answer(s) found.
  • Ö

    Ömer BAYRAM May 28 2022

    You are running on the server and the window object only exists on the browser. 

    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 = /*...*/; 

    The 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 globally:

    let windowVar = /*...*/;

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

  • V

    Vener Guevarra May 28 2022

    Sure, you are in the server so the window object will not exist. You can check null before using it: 

    if (typeof window !== "undefined") {
      // Write some thing here
    }

    But you still need to call this on the client-side, not the server-side.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close