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

14,356

Solution 1

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

Solution 2

Do it like the following:

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

var $ = jQuery = require('jquery')(window);

Hope this helps you or anyone who has been facing these types of issues.

Solution 3

This problem is because of the older version of jQuery which you are using, move to the newer version which is 3.2.1.

I was also facing the same error, moving to the new version solved this issue. You can check this answer for more information: https://stackoverflow.com/a/47215319/5892553

Share:
14,356
ssj5
Author by

ssj5

Updated on July 28, 2022

Comments

  • ssj5
    ssj5 almost 2 years

    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)
    
  • ssj5
    ssj5 over 6 years
    Where i use this , please update in my code becoz if i add this code in top location and getting same error.
  • AZinkey
    AZinkey over 3 years
    SyntaxError: redeclaration of non-configurable global property window
  • Plabon Dutta
    Plabon Dutta over 3 years
    You can find my detailed answer here: stackoverflow.com/a/48561379/4180545 Please follow the procedure and let me know.