我在网上找不到这样的错误,所以我决定在这里发帖。
首先,这是我的package.json代码
{
"name": "learning_nodejs",
"version": "0.0.0",
"description": "",
"main": "script.js",
"dependencies": {
"optimist": "~0.6.1",
"jquery": "~2.1.3",
"jsdom": "*"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD-2-Clause"
}
接下来,这是我试图在script.js中运行的javascript
var argv = require('optimist').argv;
var jsdom = require("jsdom");
var $ = require('jquery')(jsdom.jsdom().parentWindow);
var fs = require('fs');
var file = argv._[0];
var html = fs.readFileSync(file, 'UTF-8');
console.log(html);
当我进入这两个文件的目录时,我运行nodejs script.js page.html并返回此错误:
daniel@daniel-VirtualBox:~/Desktop/virtual_share/javascript_stuff/learning_nodejs$ nodejs script.js page.html
/home/daniel/Desktop/virtual_share/javascript_stuff/learning_nodejs/node_modules/jsdom/lib/jsdom/level1/core.js:287
!vm.isContext(this._ownerDocument._global)) {
^
TypeError: Object function Script(code, ctx, filename) {
if (!(this instanceof Script)) {
return new Script(code, ctx, filename);
}
var ns = new binding.NodeScript(code, ctx, filename);
// bind all methods to this Script object
Object.keys(binding.NodeScript.prototype).forEach(function(f) {
if (typeof binding.NodeScript.prototype[f] === 'function') {
this[f] = function() {
if (!(this instanceof Script)) {
throw new TypeError('invalid call to ' + f);
}
return ns[f].apply(ns, arguments);
};
}
}, this);
} has no method 'isContext'
at Object.core.DOMImplementation._addFeature (/home/daniel/Desktop/virtual_share/javascript_stuff/learning_nodejs/node_modules/jsdom/lib/jsdom/level1/core.js:287:15)
at Object.exports.applyDocumentFeatures (/home/daniel/Desktop/virtual_share/javascript_stuff/learning_nodejs/node_modules/jsdom/lib/jsdom/browser/documentfeatures.js:45:30)
at Object.exports.jsdom (/home/daniel/Desktop/virtual_share/javascript_stuff/learning_nodejs/node_modules/jsdom/lib/jsdom.js:62:12)
at Object.<anonymous> (/home/daniel/Desktop/virtual_share/javascript_stuff/learning_nodejs/script.js:3:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
此外,如果重要,我在虚拟机上运行Ubuntu 14.04。提前谢谢!
答案 0 :(得分:0)
这里有两个错误,它们都是相关的。
首先,我使用nodejs而不是node来运行我的应用程序。我不确定为什么,但我认为这是因为apt-get,所以我使用nvm来安装nodejs并解决了这个问题,但导致了另一个非常类似的问题。
显然jsdom在jsdom版本4.0+之后不能与nodejs一起使用,所以我得到了一个旧版本,现在一切正常。