webpack - 找不到模块:错误:无法解决问题' jquery'

时间:2017-07-22 17:46:35

标签: javascript jquery import webpack sails.js

我正在使用带有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目录问题或类似的东西。

1 个答案:

答案 0 :(得分:0)

另一个问题让我来到这里。对于来自Google的未来访问者,请尝试运行:

webpack --display-error-details