我试图在NuxtJS上使用NuxtJS。在跟踪本教程之后,我显然会看到一些错误,说明jQuery需要一个窗口上下文。我安装了jsdom及其所有依赖项,并使用了它应该是,但是每次运行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请注意,这些依赖关系确实已安装并更新.这是由于双方在努克斯特的冲突吗?如何才能完全解决这一问题?
我还通过jsdom设置了一个插件,使其始终具有默认窗口上下文:
jsdom.js
var jsdom = require("jsdom")
const { JSDOM } = jsdom
const { document } = new JSDOM("").window
global.document = document发布于 2019-05-13 00:02:40
要使用jsdom,必须修改nuxt.configure.js。修改nuxt.configure.js后,重新启动nuxt。
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_process,但是它们阻止您导入jsdom。使用上面提到的配置,您可以用空对象替换fs和child_proccess,并抑制生成错误。因此,如果您尝试使用依赖于这些库的函数,比如用文件获取方案,它将失败。
关于config.node的详细信息记录在这里:https://webpack.js.org/configuration/node/#other-node-core-libraries
https://stackoverflow.com/questions/54962160
复制相似问题