[英]TypeError: require(…).jsdom is not a function
I have used visual studio 2015 for nodejs web development. 我使用visual studio 2015进行nodejs web开发。
I have following packages for nodejs like. 我有类似nodejs的以下包。
and my server.js is 我的server.js是
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. 无法使用jquery获取错误。 " TypeError: require(...).jsdom is not a function "
“ TypeError:require(...)。jsdom不是一个函数 ”
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)
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 参考: https : //github.com/airbnb/enzyme/issues/942#issuecomment-314715229
This problem is because of the older version of jQuery which you are using, move to the newer version which is 3.2.1. 此问题是由于您使用的旧版jQuery,请移至较新版本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
您可以查看此答案以获取更多信息: https : //stackoverflow.com/a/47215319/5892553
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. 希望这可以帮助您或任何一直面临这些类型问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.