Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :

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 ?

like image 456
StrugglingCoder Avatar asked Nov 28 '15 19:11

StrugglingCoder


1 Answers

HALAR's helpful answer:

  • explains that *.js file 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 *.js script directly to the node.exe executable ensures its proper execution.

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

  • Open a PowerShell console window.

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

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 *.js files directly will execute them with Node.js (node.exe).

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

like image 123
mklement0 Avatar answered Nov 15 '22 05:11

mklement0