安装jQuery给出了webpack错误
问题描述:
我npm安装了jQuery,现在我看到一堆Module not found: Error: Can't resolve...
错误。对根本问题可能是什么以及解决方案有任何想法?安装jQuery给出了webpack错误
ERROR in ./node_modules/jquery/lib/node-jquery.js
Module not found: Error: Can't resolve 'jsdom'...
ERROR in ./node_modules/jquery/lib/node-jquery.js
Module not found: Error: Can't resolve 'xmlhttprequest'...
ERROR in ./node_modules/jquery/lib/node-jquery.js
Module not found: Error: Can't resolve 'location'...
ERROR in ./node_modules/jquery/lib/node-jquery.js
Module not found: Error: Can't resolve 'navigator'...
我敢肯定,这已是谷歌搜索的错误后做的WebPack 2,但没有一个提出的解决方案解决错误。我见过,但是没有奏效
一种解决方法是把下面的在我的WebPack配置:
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
这里是我的index.html:
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<div id="fb-root"></div>
<div id="app"></div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
<script src="common.js"></script>
<script src="bundle.js" type="text/javascript"></script>
</body>
</html>
这里是我的webpack.config.js:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'public');
var APP_DIR = path.resolve(__dirname, 'src', 'js');
var node_dir = __dirname + '/node_modules';
var config = {
\t entry: {
\t app: APP_DIR + '/index.js',
\t common: ["jquery"],
\t },
\t output: {
\t \t path: BUILD_DIR,
\t \t filename: 'bundle.js'
\t },
\t resolve: {
\t \t // This is so that you don't have to write the file extension while importing it.
\t \t // Instead of import HomeComponent from './HomeComponent.jsx'
\t \t // you can do import HomeComponent from './HomeComponent'
\t \t extensions: ['.js', '.jsx','.json', '*'],
\t \t alias: {
'jquery': node_dir + '/jQuery/src/wrapper.js',
},
\t },
\t externals: {
jquery: 'jQuery'
},
\t plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "common",
filename: "common.js",
minChunks: Infinity,
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
jquery: "jquery",
"window.jQuery": "jquery",
}),
],
\t module: {
loaders : [
\t \t \t {
\t \t \t \t test : /\.jsx?/,
\t \t \t \t include : APP_DIR,
\t \t \t \t exclude: /node_modules/,
\t \t \t \t loader : 'babel-loader'
\t \t \t }
\t \t ],
\t },
};
module.exports = config;
你见过这个帖子吗? https://*.com/questions/44084544/jquery-with-webpack-2 –
是的,不幸的是,错误仍然存在。 – chewchew