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 :
Even a command like
is not working. What am I missing ? How would I execute my first node.js code block ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With