早上好
我对Webpack还是很陌生,感到有点迷茫-从源路径导入模块可以正常工作-但从node_modules(例如jQuery)导入模块会给我错误消息,指出找不到模块。我完全迷路了,甚至不知道要寻找什么或如何进一步调试它。
我收到的错误消息是:
external "jquery":1 Uncaught ReferenceError: jquery is not defined
at Object.jquery (external "jquery":1)
at __webpack_require__ (bootstrap:723)
at fn (bootstrap:100)
at Object../js/ManagementApplication.ts (ManagementApplication.ts:5)
at __webpack_require__ (bootstrap:723)
at fn (bootstrap:100)
at Object.0 (dist.js:40457)
at __webpack_require__ (bootstrap:723)
at bootstrap:790
at bootstrap:790
jquery @ external "jquery":1
__webpack_require__ @ bootstrap:723
fn @ bootstrap:100
./js/ManagementApplication.ts @ ManagementApplication.ts:5
__webpack_require__ @ bootstrap:723
fn @ bootstrap:100
0 @ dist.js:40457
__webpack_require__ @ bootstrap:723
(anonymous) @ bootstrap:790
(anonymous) @ bootstrap:790
这是我的webpack配置:
// shared config (dev and prod)
const {resolve} = require('path');
const {CheckerPlugin} = require('awesome-typescript-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require("webpack")
module.exports = {
resolve: {
extensions: ['.ts', '.js'],
},
context: resolve(__dirname, '../src/main/'),
output: {
filename: "dist.js",
path: resolve(__dirname, '../target')
},
externals: {
bootstrap: "bootstrap",
jquery: "jquery"
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader'],
},
{
test: /\.tsx?$/,
use: ['babel-loader', 'awesome-typescript-loader'],
},
{
test: /\.css$/,
use: ['style-loader', {loader: 'css-loader', options: {importLoaders: 1}}],
},
{
test: /\.(scss)$/,
use: [{
loader: 'style-loader', // inject CSS to page
}, {
loader: 'css-loader', // translates CSS into CommonJS modules
}, {
loader: 'postcss-loader', // Run postcss actions
options: {
plugins: function () { // postcss plugins, can be exported to postcss.config.js
return [
require('autoprefixer')
];
}
}
}, {
loader: 'sass-loader' // compiles Sass to CSS
}]
},
{
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: 'url-loader?limit=10000',
},
{
test: /\.hbs/,
loaders: "handlebars-loader"
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'file-loader?hash=sha512&digest=hex&name=img/[hash].[ext]',
'image-webpack-loader?bypassOnDebug&optipng.optimizationLevel=7&gifsicle.interlaced=false',
],
},
],
},
plugins: [
new CheckerPlugin(),
new HtmlWebpackPlugin(),
new webpack.IgnorePlugin(/\/iconv-loader$/)
],
performance: {
hints: false,
},
};
和这个:
// development config
const merge = require('webpack-merge');
const webpack = require('webpack');
const commonConfig = require('./common');
module.exports = merge(commonConfig, {
mode: 'development',
entry: [
'webpack-dev-server/client?http://localhost:4000',// bundle the client for webpack-dev-server and connect to the provided endpoint
'webpack/hot/only-dev-server', // bundle the client for hot reloading, only- means to only hot reload for successful updates
'./js/ManagementApplication.ts' // the entry point of our app
],
devServer: {
hot: true,
host: "0.0.0.0",
port: "4000"
},
devtool: 'source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(), // enable HMR globally
new webpack.NamedModulesPlugin(), // prints more readable module names in the browser console on HMR updates
],
});
(它们都被加载,后一个覆盖第一个)。
我已经检查了十亿次库是否正确位于node_modules中-只是不知道为什么不加载它们。此问题不仅特定于特定的库,而且一般来说也适用于所有库。
相反,从库中导入CSS资源的效果很好。
有人知道如何解决此问题,或者可以帮助我了解正在发生的事情吗?
答案 0 :(得分:0)
如果不看更多项目,实际上不是100%肯定,但可以尝试一下。
将您的jquery设置为:
"jquery": "jQuery"
,然后在项目中使用jquery作为import jQuery from 'jquery'
或import $ from 'jquery'
答案 1 :(得分:0)
如果您打算将jquery
视为外部变量,则@Pandelis回答是正确的(请注意大写的Q
:jquery: jQuery
)。但是,如果您要将jquery作为节点模块导入,请参见下文。
如果要使用jQuery作为节点模块并将其捆绑在一起,则应从npm安装jquery
npm install jquery
然后将其导入您的代码中
import $ from "jquery";
无需向webpack.config.js
添加任何内容。但是,如果您想使用jQuery作为外部对象:
在webpack.config.js
中执行类似的操作时:
...
module.exports = {
externals: {
jquery: "jQuery" // or jquery: "$"
},
...
}
它告诉webpack在import jquery
行中,jquery
不应被捆绑;而是在全局范围内查找jQuery
对象(在本例中为window
)。 jQuery
和$
均有效。这也意味着您必须从外部源加载jquery:
#index.html
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<sript src="bundle.js"></script>
然后在您的代码中,您就可以这样做
import 'jquery' // or import $ from 'jquery'
$...
仅作说明,您也可以执行externals: { foo: 'jQuery' }
,而import 'foo'
仍然可以使用。
希望有帮助!