TypeError: require(...).jsdom is not a function

ssj5 picture ssj5 · Sep 22, 2017 · Viewed 11.8k times · Source

I have used visual studio 2015 for nodejs web development.
I have following packages for nodejs like.

  • body-parser 1.18.1
  • express 4.15.4
  • jquery 1.7.4
  • html 1.0.0
  • jsdom 11.2.0

and my server.js is

var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var jsdom = require('jsdom');

var path = require('path');
var port = process.env.port || 1337;
var app = express();
app.use(bodyParser());

app.use('/bootstrap', express.static(__dirname + '/public/bootstrap'));
app.use('/css', express.static(__dirname + '/public/css'));
app.use('/jquery', express.static(__dirname + '/public/jquery'));
app.use('/pages', express.static(__dirname + '/public/pages'));
app.use('/popper', express.static(__dirname + '/public/popper'));
app.use('/scripts', express.static(__dirname + '/public/scripts'));

app.get('/', function (request, response) {
var $ = require('jquery');
var title = $(html).find('title').text();
console.log(title);
response.send("Hello node");
});
app.listen(port);

Unable to use jquery getting error. "TypeError: require(...).jsdom is not a function"

TypeError: require(...).jsdom is not a function
at create (c:\users\XYZ12\documents\visual studio 2015\Projects\NodeMovieList\NodeMovieList\node_modules\jquery\lib\node-jquery.js:5:31)
at c:\users\XYZ12\documents\visual studio 2015\Projects\NodeMovieList\NodeMovieList\node_modules\jquery\lib\node-jquery.js:9435:18
at Object.<anonymous> (c:\users\XYZ12\documents\visual studio 2015\Projects\NodeMovieList\NodeMovieList\node_modules\jquery\lib\node-jquery.js:9437:2)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)

Answer

Kalana Demel picture Kalana Demel · Sep 22, 2017

Try using the following,

var jsdom = require('jsdom');
const { JSDOM } = jsdom;

const { document } = (new JSDOM('')).window;
global.document = document;

Reference : https://github.com/airbnb/enzyme/issues/942#issuecomment-314715229