【问题标题】:webpack - Module not found: Error: Can't resolve 'jquery'webpack - 找不到模块:错误:无法解析“jquery”
【发布时间】: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


【解决方案1】:

一个不同的问题把我带到了这里。对于来自 Google 的未来访问者,请尝试运行:

webpack --display-error-details

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 2016-12-10
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多