Javascript Microsoft JScript 运行时错误:代码:800A1391 在运行 node.js 程序时未定义“控制台”

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

Microsoft JScript runtime error : Code : 800A1391 'console' is undefined while running node.js program

javascriptnode.js

提问by StrugglingCoder

I am new to node.js. I have created a file named myFirst.js after installing node.js in Windows machine. The file looks like following :

我是 node.js 的新手。在 Windows 机器上安装 node.js 后,我创建了一个名为 myFirst.js 的文件。该文件如下所示:

console.log("Sweet .. Welcome to Node.js"); 

But when I try to navigate to that directory and execute the file it throws an error like the following : enter image description here

但是当我尝试导航到该目录并执行该文件时,它会引发如下错误: 在此处输入图片说明

Even a command like

甚至像这样的命令

enter image description here

在此处输入图片说明

is not working. What am I missing ? How would I execute my first node.js code block ?

不管用。我错过了什么?我将如何执行我的第一个 node.js 代码块?

回答by HALAR

You must prefix your execution of the *.Js file with the node executable to add context, as if to say, "open this file with the node scripting engine, rather than the default Microsoft scripting engine, which does not recognize the 'console' object."

您必须在 *.Js 文件的执行前添加节点可执行文件以添加上下文,就像在说,“使用节点脚本引擎打开此文件,而不是默认的 Microsoft 脚本引擎,它无法识别‘控制台’目的。”

Therefore, instead of simply providing the name of the .Js file, like so:

因此,不要简单地提供 .Js 文件的名称,如下所示:

myFirst.js

Change it to:

将其更改为:

node myFirst.js

The name of the *.js file acts as a parameter to the node executable, which is then executed, providing you with your "Sweet.. Welcome to Node.js" statement.

*.js 文件的名称充当节点可执行文件的参数,然后执行该文件,为您提供“Sweet.. Welcome to Node.js”语句。

回答by mklement0

HALAR's helpful answer:

HALAR 的有用回答

  • explains that *.jsfile are by default executed by Microsoft's obsolescent WSH(Windows Script Host) JavaScript engine (JScript) rather than Node.js,

  • and that passing a Node.js *.jsscript directly to the node.exeexecutable ensures its proper execution.

  • 解释了该*.js文件默认由 Microsoft 过时的WSHWindows 脚本主机)JavaScript 引擎(JScript)而不是 Node.js 执行

  • 并且将 Node.js*.js脚本直接传递给node.exe可执行文件可确保其正确执行

Alternatively, you can reconfigurefile-extension associations so as to make *.jsfiles execute with the Node.js engine by default:

或者,您可以重新配置文件扩展关联,以便*.js默认使用 Node.js 引擎执行文件

  • Open a PowerShell console window.

  • Execute the following, which changes the association of the .jsextension in the Windows registry (which takes effect for the currentuser account only, but, conversely, doesn't require elevation):

  • 打开 PowerShell 控制台窗口。

  • 执行以下操作,更改.jsWindows 注册表中扩展的关联(仅对当前用户帐户生效,但相反,不需要提升):

New-Item -Force HKCU:\SOFTWARE\Classes\NodeJSFile\shell\Open\Command -Value "`"$((Get-Command node.exe).Source)`" `"%1`" %*"
New-Item -Force HKCU:\SOFTWARE\Classes\.js -Value 'NodeJSFile'

From that point on, invoking *.jsfiles directly will execute them with Node.js (node.exe).

从那时起,*.js直接调用文件将使用 Node.js ( node.exe)执行它们。

Note, however, that you'll have to do this on every machine that you want to behave this way.

但是请注意,您必须在每台希望以这种方式运行的机器上执行此操作。

回答by Arjun

To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host.

要回答这个问题,首先我们需要了解错误是由Windows Script Host执行引起的。

Now, run the code from your cmd promtwith the following syntax:

现在,使用以下语法从cmd 提示符运行代码:

>node myFirst.js

or

或者

>node <application_name>.js

this will allow the Node.js application to open through V8 JavaScript engine(Google's).

这将允许 Node.js 应用程序通过V8 JavaScript 引擎(Google 的)打开。

P.S: Please reply back if this has helped in resolving your issue else post the problem you are facing after tryingthis.

PS:如果这有助于解决您的问题,请回复,否则请在尝试此操作后发布您面临的问题。

回答by TAHA SULTAN TEMURI

I faced this error when was trying to run NodeJs Script,I saved the script with name similar to "node" command like

我在尝试运行NodeJs 脚本时遇到了这个错误,我用类似于“node”命令的名称保存了脚本

node.js

节点.js

and when I tried to run

当我试图跑步时

node node.js

throw the error Jscript runtime error

抛出错误 Jscript 运行时错误

so I just replace the name with some other like,also delete the script with name node.js

所以我只是用其他类似的名称替换名称,同时删除名称为 node.js 的脚本

nodescript.js

nodescript.js

then it worked.

然后它起作用了。

回答by Srinivas Mahindrakar

Don't use node js as file name, just remane node.js file name to other and try

不要使用 node js 作为文件名,只需将 node.js 文件名重新命名为其他并尝试

回答by user3079829

I have the same problem in windows 8 with my example hello_word.js:

我的示例在 Windows 8 中遇到了同样的问题hello_word.js

  1. So I install the node js 7.7.2 vesionin disk D, because there is question of rights and System sécurity when you install in disk c, so it is more difficult to work in disk c
  2. when I finished the install wizard, I lunch "cmd" and then I change my position to the d:/ nodejsby using cd d:/nodejs
  3. then there is two issues:

    1. execute the node hello_word.js
    2. or at first execute node, so now you are in the node session, then execute requiere('./hello_word.js')
  1. 所以我把node js安装7.7.2 vesion在磁盘上D,因为安装在磁盘上存在权限和系统安全问题c,所以在磁盘上工作比较困难。c
  2. 当我完成安装向导后,我吃午饭“cmd”,然后d:/ nodejs使用以下命令将我的位置更改为cd d:/nodejs
  3. 那么有两个问题:

    1. 执行 node hello_word.js
    2. 或者首先执行node,所以现在你在节点会话中,然后执行requiere('./hello_word.js')

good luck

祝你好运

回答by jOshT

Please see this post What exactly does "/usr/bin/env node" do at the beginning of node files?

请参阅这篇文章在节点文件的开头“/usr/bin/env node”究竟做了什么?

You will need to add #!/usr/bin/env nodeto the beginning of your script. This works even on windows.

您将需要添加#!/usr/bin/env node到脚本的开头。这甚至适用于 Windows。

回答by GUTTA K C

Please remove if any node.jsfile in the program file path.

请删除node.js程序文件路径中的任何文件。

In the below directory or folder(file path) node.jsfile exists.

在下面的目录或文件夹(文件路径)node.js文件中存在。

C:/Education/Node/node.js 

Remove node.js

消除 node.js

Then run in command prompt in file myFirst.js file path

然后在文件 myFirst.js 文件路径中的命令提示符下运行

node myFirst.js