5
votes

I am learning Node JS and I am getting Object expected at line number 1 Microsoft Jscript Runtime Error while running the code

var fs = require('fs');

function FileObject () {
this.filename = null;
this.exists = function(callback) {
var self = this;
fs.open(this.filename, 'r', function(err, handle){
if(err){
console.log(self.filename+  'does Not exist');
callback(false);
}
else {
console.log(self.filename+ 'does Exist Indeed');
callback(true);
fs.close(handle);
}
});
};
}
var fo = new FileObject();
fo.filename = 'doesnotexist';
fo.exists(function(does_it_exist) {
console.log('results from exists:' + does_it_exist);
});
2
How are you running this script? - apsillers
I've tried to run the code from command prompt - SankarSV4791
Can you show exactly how you tried to execute your script from the command line? - mscdex
e:\Bckup\D\TCS\Node\MarkNodeLiveLessons>"solve the problem.js" - This command didn't work When I tried the command- e:\Bckup\D\TCS\Node\MarkNodeLiveLessons>node "solve the problem.js" I gt the output.. Thanks a lot :) - SankarSV4791

2 Answers

14
votes

node.js file name is reserved in NodeJS. I too got the same error. I renamed my file to "test.js" and it worked...

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World OKKK");
response.end();
}).listen(8888);

command prompt> node test.js
URL => http://localhost:8888 and it prints "Hello World OKKKK" on the browser.

-1
votes

You can't double-click the source file to run it, you need to execute the script from the command line (e.g. C:\> node foo\bar.js assuming your script bar.js is in C:\foo).