Jquery jsdom应该如何配置NuxtJS?
我正在尝试在NuxtJS上使用jQuery。在完成以下操作之后,我显然收到了错误消息,说jQuery需要一个窗口上下文。我安装并使用了jsdom及其所有依赖项,但每次运行Jquery jsdom应该如何配置NuxtJS?,jquery,node.js,jsdom,Jquery,Node.js,Jsdom,我正在尝试在NuxtJS上使用jQuery。在完成以下操作之后,我显然收到了错误消息,说jQuery需要一个窗口上下文。我安装并使用了jsdom及其所有依赖项,但每次运行npm run dev时,总会弹出以下错误: ERROR Failed to compile with 5 errors friendly-errors 14:13:40 These dependencies were
npm run dev
时,总会弹出以下错误:
ERROR Failed to compile with 5 errors friendly-errors 14:13:40
These dependencies were not found: friendly-errors 14:13:40
friendly-errors 14:13:40
* child_process in ./node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js friendly-errors 14:13:40
* fs in ./node_modules/jsdom/lib/jsdom/browser/resources/resource-loader.js, ./node_modules/jsdom/lib/jsdom/living/xhr-utils.js and 2 others
friendly-errors 14:13:40
To install them, you can run: npm install --save child_process fs
请注意,这些依赖项确实已安装且是最新的。这是因为Nuxt上的双方冲突吗?到底该如何解决呢
我还设置了一个插件,通过jsdom始终具有默认窗口上下文:
jsdom.js
var jsdom = require("jsdom")
const { JSDOM } = jsdom
const { document } = new JSDOM("").window
global.document = document
要使用jsdom,必须修改nuxt.configure.js。修改numxt.configure.js后,重新启动numxt
export default {
// some configurations
build: {
/*
** You can extend webpack config here
*/
extend (config, { isDev, isClient }) {
if (isClient) {
config.node = {
fs: 'empty',
child_process: 'empty',
}
}
}
},
// other configurations
}
如果遇到相同的依赖项错误,只需在config.node中添加相同的设置,如下所示
fs: 'empty',
child_process: 'empty',
tls: 'empty',
net: 'empty',
在本例中,我假设您实际上不需要在代码中使用fs、child_进程,但它们会阻止您导入jsdom。
通过上面提到的配置,您可以用空对象替换fs和child_进程,并抑制生成错误。所以,若您尝试使用依赖于这些库的函数,比如使用文件模式获取,那个么它将失败
有关config.node的详细信息记录在此处: