Javascript Node.js 警报导致崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11618456/
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
Node.js Alert Causes Crash
提问by gtmtg
I'm trying to create a node.js app and
我正在尝试创建一个 node.js 应用程序和
alert('Sample Alert');
is causing my program to crash. Node says
导致我的程序崩溃。节点说
ReferenceError: alert is not defined
参考错误:警报未定义
and then quits. I can use the alert
function when running javascript on a regular html page, so I'm at a loss to understand why this is... Is this a separate module that I have to use with node.js?
然后退出。我可以alert
在常规 html 页面上运行 javascript 时使用该函数,所以我无法理解为什么会这样......这是我必须与 node.js 一起使用的单独模块吗?
回答by Pointy
The alert()
function is a property of browser window
objects. It is not really part of JavaScript; it's just a facility available to JavaScript code in that environment.
该alert()
函数是浏览器window
对象的一个属性。它实际上并不是 JavaScript 的一部分;它只是该环境中 JavaScript 代码可用的工具。
Try console.log("Hello World");
尝试 console.log("Hello World");
回答by sampathsris
alert()
function is only available when you execute JavaScript in the special context of browser windows. It is available through the window
object.
alert()
函数仅在您在浏览器窗口的特殊上下文中执行 JavaScript 时可用。它可以通过window
对象获得。
Node.js is not intended for writing desktop applications (directly). It is mainly intended for writing server-side JavaScript applications. You can use following frameworks/packages (and many more) if you want to develop true desktop applications.
Node.js 不用于(直接)编写桌面应用程序。它主要用于编写服务器端 JavaScript 应用程序。如果您想开发真正的桌面应用程序,您可以使用以下框架/包(以及更多)。
- Electron
NW.js(previously, node-webkit)
NW.js is an app runtime based on
Chromium
andnode.js
. You can write native apps in HTML and JavaScript with NW.js. It also lets you call Node.js modules directly from the DOM and enables a new way of writing native applications with all Web technologies.Available as an standalone distributable and an npm package
- 电子
NW.js(以前是 node-webkit)
NW.js 是一个基于
Chromium
和的应用运行时node.js
。您可以使用 NW.js 用 HTML 和 JavaScript 编写本机应用程序。它还允许您直接从 DOM 调用 Node.js 模块,并支持使用所有 Web 技术编写本机应用程序的新方法。可作为独立的分发包和 npm 包使用
Meanwhile, you can use console.log()
to output a message in Node.js.
同时,您可以console.log()
在 Node.js 中使用来输出消息。
console.log('hello');
回答by ggb667
While these answers are "correct", as there is no alert function available outside of the browser, there's no reason you can't create one and then use it:
虽然这些答案是“正确的”,但由于在浏览器之外没有可用的警报功能,因此您没有理由不能创建一个然后使用它:
node -e "function alert(x){
x === 'undefined' ? console.log('undefined') : console.log(x); return;
};
alert('x'); alert();"
results:
结果:
x
undefined
Then you might not need to change your existing code or example or whatever.
那么您可能不需要更改现有代码或示例或其他任何内容。
回答by tomarone
You'll also need code to wait for a key. Here's a start:
您还需要代码来等待密钥。这是一个开始:
process.stdin.on('char', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write('data: ' + chunk + 'got?\n');
}
});
回答by Adnan Khan
alert function is for browsers. means front end..in nodejs for printing in cmd or bash you should use this one..
警报功能适用于浏览器。意味着前端..在 nodejs 中用于在 cmd 或 bash 中打印,你应该使用这个..
console.log("Sample alert");
you can print any variable or constant here... for printing variables just remove quotes
您可以在此处打印任何变量或常量...打印变量只需删除引号
回答by Rubin bhandari
The alert() property is only allowed by browsers not javascript.
alert() 属性只被浏览器允许,而不是 javascript。