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);
});
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);
});
Share
Improve this question
asked Sep 30, 2014 at 19:45
SankarSV4791SankarSV4791
631 gold badge1 silver badge5 bronze badges
4
- How are you running this script? – apsillers Commented Sep 30, 2014 at 19:47
- I've tried to run the code from mand prompt – SankarSV4791 Commented Oct 5, 2014 at 16:44
- Can you show exactly how you tried to execute your script from the mand line? – mscdex Commented Oct 5, 2014 at 17:29
- e:\Bckup\D\TCS\Node\MarkNodeLiveLessons>"solve the problem.js" - This mand didn't work When I tried the mand- e:\Bckup\D\TCS\Node\MarkNodeLiveLessons>node "solve the problem.js" I gt the output.. Thanks a lot :) – SankarSV4791 Commented Oct 5, 2014 at 18:01
2 Answers
Reset to default 14node.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);
mand prompt> node test.js
URL => http://localhost:8888 and it prints "Hello World OKKKK" on the browser.
You can't double-click the source file to run it, you need to execute the script from the mand line (e.g. C:\> node foo\bar.js
assuming your script bar.js
is in C:\foo
).