Reputation: 1527
also when i run node script.js page.html i have this error, i dont know why, i did put jquery in my dependencies and it installed it
C:\Users\NEBELYN\Desktop\projects1\paragraph\node_modules\jquery\dist\jquery.js:29
throw new Error( "jQuery requires a window with a document" );
^ Error: jQuery requires a window with a documentat module.exports (C:\Users\NEBELYN\Desktop\projects1\paragraph\node_modules\jquery\dist\jquery.js:29:1 at Object.<anonymous> (C:\Users\NEBELYN\Desktop\projects1\paragraph\script.js:9:1) 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) at node.js:902:3
here is the code am running
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);
});
and here is the page.html
<html>
<body>
<p>Apple</p>
<span>Unrelated</span>
<p>Orange</p>
<div>
Steak
</div>
<p>Banana</p>
</body>
</html>
Upvotes: 1
Views: 2418
Reputation: 13943
I had code that worked fine a month ago, but started to display this error when I upgraded to the latest jquery via npm. If I only knew how to get back that older version, then the problem would go away.
This seems to have fixed it:
npm install jquery
npm install jsdom
// Define window
var jsdom = require("jsdom");
var window = jsdom.jsdom().createWindow();
var $ = require("jquery")( window );
Upvotes: 2
Reputation: 887657
jQuery requires a full DOM implementation, which you don't have on the server.
To parse HTML text, you should use a simpler library called Cheerio, which has a jQuery-like API but does not use a DOM.
Alternatively, you could use JSDOM to fake a browser DOM on the server (this is slower).
Upvotes: 2