4

问题是Error: Cannot find module 'jquery'

当我用脚本构建它时:

rm -rf dist && webpack --config webpack.config.js

已经尝试过使用外部等其他线索,但仍然在 jquery 上遇到问题

这是我的 package.json,

{
  "name": "webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "rm -rf dist && webpack --config webpack.config.js"
  },
  "devDependencies": {
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "autoprefixer": "^9.4.10",
    "babel-loader": "^8.0.5",
    "copy-webpack-plugin": "^5.0.4",
    "css-loader": "^2.1.1",
    "cssnano": "^4.1.10",
    "expose-loader": "^0.7.5",
    "file-loader": "^3.0.1",
    "img-loader": "^3.0.1",
    "mini-css-extract-plugin": "^0.5.0",
    "postcss-loader": "^3.0.0",
    "sass": "^1.17.2",
    "sass-loader": "^7.1.0",
    "url-loader": "^2.1.0",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.2.3"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {}
}

这是我的 webpack.config.js,

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
module.exports = {
    entry: './public/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    mode: 'development',

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },
            {
                test: /\.(sa|sc|c)ss$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader
                    },
                    {
                        loader: "css-loader",
                    },
                    {
                        loader: "postcss-loader"
                    },
                    {
                        loader: "sass-loader",
                        options: {
                            implementation: require("sass")
                        }
                    }
                ]
            },
            { test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' },
            {
                // Exposes jQuery for use outside Webpack build
                test: require.resolve('jquery'),
                use: [{
                    loader: 'expose-loader',
                    options: 'jQuery'
                }, {
                    loader: 'expose-loader',
                    options: '$'
                }]
            }
        ]
    }, externals: {
        jquery: 'jQuery'
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "bundle.css"
        }),
        new CopyWebpackPlugin([
            { from: './public/img', to: 'img' }
        ]),
        new webpack.ProvidePlugin({
            '$': 'jquery',
            'jQuery': 'jquery',
            'window.jQuery': 'jquery'
        }),
    ]
};

在我的入口文件上:

import './css/reset.css';
import './css/main-1.0.css';
import './css/homepage-1.0.css';

import './js/jquery.lazy.min.js';
import './js/jquery.ellipsis.js';
import './js/main-1.0.js';
import './js/helper.js';
import './js/homepage-1.0.js';

我已经被这个问题困住了 2 天.. 任何建议我都会这样做,

我得到的错误在终端端在此处输入图像描述

此致。

4

2 回答 2

1

就我而言,我require("jQuery")在 Windows 中使用,但它在 Linux 机器上不起作用。Linux 是区分大小写的,所以我不得不使用require("jquery").

于 2019-12-05T20:43:27.803 回答
0

在同一个项目中安装 npm i jquery 解决我的问题

于 2020-05-04T10:45:18.180 回答