I run a piece of code with first.js
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8124);
// Console will print the message
console.log('Server running at http://127.0.0.1:8124/');
while i m running it with gitbash its giving desire output. but with node js command prompt its giving microsoft jscript compilation error.
I run a piece of code with first.js
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8124);
// Console will print the message
console.log('Server running at http://127.0.0.1:8124/');
while i m running it with gitbash its giving desire output. but with node js command prompt its giving microsoft jscript compilation error.
Share Improve this question edited Apr 11, 2016 at 6:15 Priya 1,3556 gold badges21 silver badges41 bronze badges asked Apr 11, 2016 at 5:37 PrayerPrayer 831 gold badge1 silver badge5 bronze badges 1- 1 node.js has nothing to do with JScript so you are running your script using a wrong interpreter. – Anatol - user3173842 Commented Apr 11, 2016 at 6:18
4 Answers
Reset to default 21Looks like you've just typed script's name into the terminal window therefore asked windows to find proper interpreter for your script. You should try and interpret the script with nodejs instead:
node your-script.js
not just
your-script.js
The issue here could stem from several places. Most likely installation-based.
The following code (Taken from above)
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n'); }).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
Yields positive results when I attempt to load it in the browser. I therefore recommend the following:
- Try run what I put into the code snippet window into a brand new first.js file in a brand new directory and from command line navigate to the directory and run the file with the command "Node first.js" (Without quotes naturally).
1.1 If using any editor aside from a plain text editor (notepad++, notepad, sublime etc) ensure the file is in-fact set to output plain-text (bone-head move but often overlooked)
Reinstall node (Worth a shot)
If all else fails and you have npm installed, type "npm install http" and re-run your first.js application
I was getting the same error repeatedly on my nodejs app and then realized I wrote only "index.js"(the script file name on the package.json file). So whenever I ran the command npm start
it was giving me that error. I had nodemon installed on my app so I changed it to nodemon index.js
after that it worked perfectly. I am attaching the screenshot here:
What helped me is -
I removed --exec
after nodemon command in my script section of package.json
file.
Previously it was -
"dev": "nodemon --exec src/index.js"
I changed it to -
"dev": "nodemon src/index.js"