我想通過命令行運行下面這個腳本使用與無瀏覽器的jQuery的node.js通過命令行
var argv = require('optimist').argv,
$ = require('jquery'),
fs = require('fs');
var file = argv._[0];
var html = fs.readFileSync(file, 'UTF-8');
$(html).find('p').each(function(index) {
var content = $(this).html();
console.log('Paragraph ' + (index + 1) + ': ' + content);
}); //script.js
的命令是$ node script.js page.html
其中page.html
是參數
我得到的錯誤是:
./node_modules/jquery/dist/jquery.js:29
throw new Error("jQuery requires a window with a document");
Error: jQuery requires a window with a document
at module.exports (./node_modules/jquery/dist/jquery.js:29:12)
...
我正在使用jquery-2.1.3。我知道這曾經是一次工作,但看起來事情已經改變了。
此外,我也按照http://rkulla.blogspot.com/2014/04/using-browserify-with-jquery-backbonejs.html的指示,但我仍然得到相同的錯誤。
任何幫助解決這個錯誤將不勝感激。非常感謝你!