Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve 'window is not defined' Error in Node JS on Terminal

I want to include angular JS Using require so I am using below code

var ang = require('angular');

But It displays error

ReferenceError: window is not defined

I Know window object not defined in node console but help me how to avoid this error ?

I hope my question understandable.

like image 492
Relax Avatar asked May 16 '16 08:05

Relax


People also ask

Why is window undefined in next JS?

This is because, Next. js is server-side rendered, and it runs the component code in the server, sends the HTML to the browser, and the HTML gets hydrated in the browser.

How do you define windows in react?

window is not defined on the server, so you can't use it during the render of a component being SSR'd. During a component render, use the useIsSsr hook. If outside of a component render, then directly check typeof window === "undefined" to see if you're on the server or the browser.

What is window in node JS?

The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment. In a browser it is named window , for Node. js it is global , for other environments it may have another name.


1 Answers

The window object is only defined in the browser, and isn't defined within Node.js (which uses process instead).

The reason you're getting this error is because you're trying to require a module (angular.js) that was intended to be used with the DOM from a browser, and not within the Node.js environment.

If you want to include angular in your HTML code, include it like you would any other JS file using a tag.

like image 131
Anthony E Avatar answered Sep 21 '22 14:09

Anthony E