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 ment 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
:
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:
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 ment 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
:
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:
- 1 What version of JSDOM do you have? – Scimonster Commented Feb 23, 2015 at 14:29
-
I installed and cmd prompt showed me this:
[email protected] node_modules\jsdom
– bcesars Commented Feb 23, 2015 at 14:30
1 Answer
Reset to default 6 +50The 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]