Microsoft Jscript运行时错误:对象期望-Node.js

5

我正在学习Node JS,运行代码时出现了“Microsoft Jscript Runtime Error”错误,提示第1行期望对象。

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);
});

你是如何运行这个脚本的? - apsillers
我已经尝试从命令提示符中运行代码。 - SankarSV4791
你能展示一下你如何在命令行中执行你的脚本吗? - mscdex
"solve the problem.js"
  • 这个命令行没有生效
当我尝试了这个命令行- e:\Bckup\D\TCS\Node\MarkNodeLiveLessons>node "solve the problem.js"我得到了输出...非常感谢 :)
- SankarSV4791
2个回答

14

node.js 文件名在 NodeJS 中被保留。我也遇到了同样的错误。我将文件重命名为 "test.js" ,然后它就可以工作了...

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);

命令提示符> node test.js
URL => http://localhost:8888 并在浏览器中打印 "Hello World OKKKK"。


把我所有的互联网都献给你,先生。 - m02ph3u5

-1
你不能双击源文件来运行它,你需要从命令行执行脚本(例如:C:\> node foo\bar.js,假设你的脚本 bar.jsC:\foo 中)。

即使我从命令提示符中执行代码,仍然会出现此错误。 - SankarSV4791
请勿复制并粘贴已有答案。 - Pushp Singh
1
@PushpSingh 你在指什么?我的回答并不是从任何地方“复制和粘贴”的。此外,这个答案是在此写作时7个月之前留下的,因此它甚至不是那个答案的“复制”。 - mscdex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接