Javascript 如何解决终端上 Node JS 中的“窗口未定义”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37250121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:04:40  来源:igfitidea点击:

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

javascriptangularjsnode.js

提问by Relax

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

我想包含 angular JS Using require 所以我使用下面的代码

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.

我希望我的问题可以理解。

回答by Anthony E

The windowobject is only defined in the browser, and isn't defined within Node.js (which uses processinstead).

window对象仅在浏览器中定义,而不在 Node.js 中定义(而是使用process)。

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.

您收到此错误的原因是因为您试图要求一个模块 (angular.js),该模块旨在与来自浏览器的 DOM 一起使用,而不是在 Node.js 环境中。

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

如果您想在 HTML 代码中包含 angular,请像使用标签的任何其他 JS 文件一样包含它。