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
:
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:
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.)™
Try making sure you install JSDOM 3.x.
npm install jsdom@3.x
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.