我试图找出在节点中使用层次依赖关系的方法。
我正在使用Vue CLI和vue-cli插件浏览器扩展(https://www.npmjs.com/package/vue-cli-plugin-browser-extension)构建VueJs铬扩展。在后台文件中,我需要使用两个依赖项:
为了工作,表到json需要jquery。
我使用节点安装安装了这两个依赖项,并且需要后台文件中的依赖项。但是,当我试图运行后台脚本时,我会得到以下错误:
background.js:37 Uncaught ReferenceError: jQuery未定义在Object.f370 ( background.js:37 ) at n ( background.js:1 ) at Object.fe77 (background.js:37) at n ( background.js:1 ) at Object.1 (background.js:1) at n (background.js:1) at background.js:1 f370 @background.js:37 n@ background.js:1 fe77 @ background.js:37 n@ background.js:1 1@ background.js:1 n@ background.js:1 (匿名)@ background.js:1 (匿名)@ background.js:1 DevTools未能加载SourceMap:无法加载内容用于HTTP错误:状态代码404,net::ERR_UNKNOWN_URL_SCHEME
表到json无法看到jquery。我遗漏了什么?
这是我的背景文件,package.json和vue.config.js:
backgroung.js
const jQuery = require('jquery'), $ = jQuery
const tableToJSON = require('table-to-json')package.json
{
"name": "test",
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "vue-cli-service build --mode development --watch",
"build": "vue-cli-service build"
},
"dependencies": {
"core-js": "^3.6.4",
"jquery": "^3.5.0",
"table-to-json": "^1.0.0",
"vue": "^2.6.11",
"vuex": "^3.1.3"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.3.0",
"@vue/cli-plugin-vuex": "~4.3.0",
"@vue/cli-service": "~4.3.0",
"vue-cli-plugin-browser-extension": "~0.24.0",
"vue-template-compiler": "^2.6.11"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}vue.config.js
module.exports = {
pages: {
popup: {
template: 'public/index.html',
entry: './src/popup/main.js',
title: 'Popup'
}
},
pluginOptions: {
browserExtension: {
componentOptions: {
background: {
entry: 'src/background.js'
},
contentScripts: {
entries: {
script: 'src/content-scripts/script.js'
}
}
}
}
}
}发布于 2020-05-02 09:53:16
这个问题一般不适用于节点依赖关系。table-to-json是遗留的jQuery插件,依赖于jQuery全局,而不是JavaScript模块。它变量存在于插件执行的作用域中。
这可以通过显式公开jQuery全局来实现:
window.$ = window.jQuery = require('jquery');或者是配置Webpack为垫片 jQuery变量,或者是全局的ProvidePlugin,或者是带有imports-loader的table-to-json包的本地变量。后者无需修改Webpack配置插件导入即可实现。
require('imports-loader?jQuery=jquery!table-to-json');因为table-to-json不是JavaScript模块,所以不需要将它导入变量,因为导入时的tableToJSON === undefined。它应该被用作jQuery插件,$(...).tableToJSONCell。
https://stackoverflow.com/questions/61556945
复制相似问题