简体   繁体   English

window is not defined error in js node.js

[英]window is not defined error in js node.js

I have this simple code:我有这个简单的代码:

do {
    var SioNo = window.prompt("Nicholas ha il cancro, si o no?");
} while (SioNo.toLowerCase() != "si" && SioNo.toLowerCase() != "no");

if (SioNo.toLowerCase() == "si") {
    console.log("Hai il cancro lol");
}
else {
    console.log("ahahahah si come no");
}

It's my first time coding in js so i don't really know what to do, i'm using node.js last version and VSCode这是我第一次用 js 编码所以我真的不知道该怎么做,我正在使用 node.js 最新版本和 VSCode

This is the error:这是错误:

ReferenceError: window is not defined参考错误:window 未定义

at Object.在 Object。 <anonymous (i:.Documenti\Scuola\as_20_21\Informatica\JavaScript\Esercizio1\HelloWorld.js:2:17) <匿名 (i:.Documenti\Scuola\as_20_21\Informatica\JavaScript\Esercizio1\HelloWorld.js:2:17)

at Module._compile (internal/modules/cjs/loader.js:1068:30)在 Module._compile (internal/modules/cjs/loader.js:1068:30)

at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)

at Module.load (internal/modules/cjs/loader.js:933:32)在 Module.load (internal/modules/cjs/loader.js:933:32)

at Function.Module._load (internal/modules/cjs/loader.js:774:14)在 Function.Module._load (internal/modules/cjs/loader.js:774:14)

at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

at internal/main/run_main_module.js:17:47在内部/main/run_main_module.js:17:47

window is only found in Browsers (Chrome, Firefox, Safari etc). window仅在浏览器中找到(Chrome、Firefox、Safari 等)。

Executing the above script using node without extra packages will not work as the window variable simply does not exist in the current context (that is your terminal window).使用没有额外包的node执行上述脚本将不起作用,因为window变量在当前上下文(即您的终端窗口)中根本不存在。

If you want to do a prompt in NodeJS, which will accept user input from the terminal you can use a native package readline .如果您想在 NodeJS 中进行提示,它将接受来自终端的用户输入,您可以使用本机 package readline

Here's a solution using recursion instead of a do...while loop.这是使用递归而不是do...while循环的解决方案。

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

function askQuestion() {
  rl.question("Nicholas ha il cancro, si o no?", function (result) {
    if (result.toLowerCase() === "si") {
      console.log("Hai il cancro lol");
      process.exit(1);
    } else {
      console.log("ahahahah si come no");
      askQuestion();
    }
  });
}

askQuestion();

Since it is your first time using JavaScript / NodeJS, I would recommend going through tutorials and reading the documentation first.由于这是您第一次使用 JavaScript / NodeJS,我建议您先阅读教程并阅读文档。 https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps . https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps

Actually I am new in Node js but I have search for the reason.实际上我是 Node js 的新手,但我已经在寻找原因。 I founded that "window" does not recognized in Node.js, so try this solution by import this library "react" and then use the variable "window" and see if it is recognized:我发现 Node.js 中无法识别“window”,因此通过导入此库“react”尝试此解决方案,然后使用变量“window”并查看它是否被识别:

import React from "react";从“反应”导入反应;

read this for more details: https://morioh.com/p/f7b08fe33a67阅读本文了解更多详情: https://morioh.com/p/f7b08fe33a67

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM