【问题标题】:Error: Cannot find module 'jquery' on require.resolve('jquery') webpack错误:在 require.resolve('jquery') webpack 上找不到模块 'jquery'
【发布时间】:2020-01-17 18:11:09
【问题描述】:

问题是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 天.. 任何建议我都会做,

我得到的错误是在终端端

最好的问候。

【问题讨论】:

  • 对这个线程有疑问吗? stackoverflow.com/a/32266859/11700321
  • 不。这是不同的问题,我在构建 webpack 配置时遇到了问题
  • 尝试:“你需要安装 Expose-loader 才能正常工作 npm install Expose-loader --save-dev” 根据对此线程的评论? stackoverflow.com/a/46937798/11700321
  • 它已经尝试使用expose loeader,但不希望你能看到它,我已经在使用它了。也许另一个?
  • aw...我在做一些愚蠢的事情..是的,你对@EGC4 兄弟在你的第一条评论大声笑。我查看的错误 package.json 是不同的文件夹.. haiyah ty2

标签: jquery node.js webpack jquery-lazyload


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-12-28
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2017-10-16
      • 2018-02-03
      • 2016-07-23
      相关资源
      最近更新 更多