2

I'm facing an issue in nodejs when I try to execute and script file that I've created for testing purposes.
I need to test it in my localhost (Windows OS) before I can implement webservice in webserver (Linux OS).

I installed jsdom and jquery right after installing nodejs but still having the following message error:

TypeError: Cannot read property 'document' of undefined

This happened after I tried to rum this JS script named index.js:

// Carrega a biblioteca HTTP do Node.js.
var http = require('http');
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);
//var $ = require('jquery');

// Cria um serviço web para tratar a requisição de resposta da mensagem Hello World.
var server = http.createServer(function (request, response) {
// Define os parâmetros de cabeçalho de resposta.

// usage  
response.writeHead(200, {'Content-Type': 'text/html'});
// Escreve uma mensagem de resposta do servidor.
response.write('<html><body><h1>Hello World</h1></body></html>');

// usage
$("body").append("<div>TEST</div>");

// Envia uma resposta para o cliente
response.end();
});

// Define a porta e IP que serviço executado a aplicação.
server.listen(3000);
// Imprime mensagem no terminal do servidor.
console.log('Servidor Node.js em execução.');

If It comment the following line, it gonna work without recognize jquery functions:

var $ = require('jquery')(require("jsdom").jsdom().parentWindow);

I need to use jquery with nodejs, but I've tried to google this problem but no success.

Following bellow, there is an image in Command prompt when I rum node index.js:

enter image description here

Is there anything else should I try to make node.js running successfully with jquery?

If I need to post any further information, please, advise me.

UPDATE

I tried to rum the same script inside Node.js path from other Programmer's Computer. We faced another TypeError problem:

enter image description here

2
  • 1
    What version of JSDOM do you have?
    – Scimonster
    Feb 23, 2015 at 14:29
  • I installed and cmd prompt showed me this: [email protected] node_modules\jsdom
    – bcesars
    Feb 23, 2015 at 14:30

1 Answer 1

6
+50

The latest versions of jsdom require Node.js v8 or newer. (Versions of jsdom below v12 still work with Node.js v6, but are unsupported.)™

From JSDOM's NPM page.

Try making sure you install JSDOM 3.x.

npm install [email protected]
5
  • No way. I'll try to install jsdom 3.x and see if I can rum this script.
    – bcesars
    Feb 23, 2015 at 14:37
  • @Scimonster Error: Cannot find module 'jsdom' =/ Feb 23, 2015 at 17:59
  • Throw new error, jquery require a window with a document =[ Feb 23, 2015 at 18:01
  • @ErasmoOliveira Um, odd.
    – Scimonster
    Feb 23, 2015 at 18:05
  • @Scimonster I got the same problem here =(
    – bcesars
    Feb 23, 2015 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.