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 ?
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.
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."
Therefore, instead of simply providing the name of the .Js file, like so:
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.
To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host .
Now, run the code from your cmd promt with the following syntax:
>node myFirst.js
or
>node <application_name>.js
this will allow the Node.js application to open through V8 JavaScript engine (Google's).
PS: Please reply back if this has helped in resolving your issue else post the problem you are facing after trying this.
I faced this error when was trying to run NodeJs Script ,I saved the script with name similar to "node" command like
node.js
and when I tried to run
node node.js
throw the error Jscript runtime error
so I just replace the name with some other like,also delete the script with name node.js
nodescript.js
then it worked.
I have the same problem in windows 8 with my example hello_word.js
:
7.7.2 vesion
in 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
d:/ nodejs
by using cd d:/nodejs
then there is two issues:
node hello_word.js
node
, so now you are in the node session, then execute requiere('./hello_word.js')
good luck
Please see this post What exactly does "/usr/bin/env node" do at the beginning of node files?
You will need to add #!/usr/bin/env node
to the beginning of your script. This works even on windows.
Please remove if any node.js
file in the program file path.
In the below directory or folder(file path) node.js
file exists.
C:/Education/Node/node.js
Remove node.js
Then run in command prompt in file myFirst.js file path
node myFirst.js
不要使用 node js 作为文件名,只需将 node.js 文件名重新命名为其他并尝试
if you still get "'node' is not recognized as an internal or external command, operable program or batch file." error after node yourjsfile.js command on Windows
type node and enter first then type .load .\\yourjsfile.js and enter again
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.