【发布时间】:2017-12-28 16:15:19
【问题描述】:
我正在使用带有 webpack 的 Sails 1,当我尝试从我的主 js 文件导入 jquery 时,我收到这个错误,大喊它无法解析 jquery。
webpack.config.js:
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports.webpack = {
devtool: 'source-map',
entry: {
'admin': './assets/admin/js/admin.js'
},
output: {
filename: 'js/[name].bundle.js',
path: path.resolve(__dirname, '..', '.tmp', 'public')
},
resolve: {
modules: [
path.join(__dirname, "js"), "node_modules"
]
},
module: {
// Extract less files
rules: [{
test: /\.css$/,
loader: ExtractTextPlugin.extract({ use: 'css-loader' })
}, {
test: /\.less$/,
loader: ExtractTextPlugin.extract({ use: 'css-loader!less-loader' })
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
[ 'es2015', { modules: false } ]
]
}
}]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
// This plugin extracts CSS that was imported into .js files, and bundles
// it into separate .css files. The filename is based on the name of the
// .js file that the CSS was imported into.
new ExtractTextPlugin('css/[name].bundle.css'),
// This plugin cleans out your .tmp/public folder before lifting.
new CleanWebpackPlugin(['public'], {
root: path.resolve(__dirname, '..', '.tmp'),
verbose: true,
dry: false
}),
// This plugin copies the `images` and `fonts` folders into
// the .tmp/public folder. You can add any other static asset
// folders to this list and they'll be copied as well.
new CopyWebpackPlugin([{
from: './assets/images',
to: path.resolve(__dirname, '..', '.tmp', 'public', 'images')
}])
]
};
在我的 assets/admin/js/admin.js 我只有
import 'jquery';
jquery 在我的 json 包上,它显示在我的 node_modules 包下,名称为 jquery。
另一个奇怪的事情是,如果我更改 jquery 并尝试导入 angular,它工作完美,所以我不认为这是一个 node_modules 目录问题或类似那个。
【问题讨论】:
-
@jimcollins 我看到了那个答案,但是由于 angular 工作正常并且像 jquery 一样通过 npm 安装,所以它似乎可能有所不同,我错过了什么吗?
-
配置看起来不错,我认为它与 webpack 无关。也许模块没有正确安装。你能跑
node -e 'require("jquery")'吗?如果这引发错误,您应该删除node_modules并重新安装它们。 -
抛出错误,module.js:487 throw err; ^ 错误:找不到模块'jquery',但我不明白,它显示在 node_modules 下,当我安装它时,它没有抛出任何错误。我尝试删除整个文件夹并再次安装它,同样的事情发生了
-
我听说人们在使用 npm 5 时遇到了一些问题。请尝试升级它或使用
npm install -g npm@4降级到 4。之后,您还可以在重新安装之前使用npm cache clean删除缓存(可能会拉入相同的损坏包)。
标签: javascript jquery import webpack sails.js