最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - TypeError: Cannot read property 'document' of undefined - Stack Overflow

programmeradmin3浏览0评论

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:

Share Improve this question edited Feb 23, 2015 at 14:33 bcesars asked Feb 23, 2015 at 14:24 bcesarsbcesars 1,0101 gold badge17 silver badges36 bronze badges 2
  • 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
Add a ment  | 

1 Answer 1

Reset to default 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]
发布评论

评论列表(0)

  1. 暂无评论