错误:在require.resolve('jquery')Webpack上找不到模块'jquery'

时间:2019-09-16 21:41:23

标签: jquery node.js webpack jquery-lazyload

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

我得到的错误是在终端端enter image description here

最诚挚的问候。

3 个答案:

答案 0 :(得分:2)

就像@EGC一样,我错过了其他项目中的package.json文件

谢谢你..#叹气..愚蠢的我..抱歉

顺便说一句,但是现在我得到了新的错误..哈哈哈

也许是另一个建议吗?

这是在运行npm start时发生的:

it's happen on running npm start

答案 1 :(得分:0)

就我而言,我在Windows中使用{title:"Name", field:"name", headerFilter:true, headerdataFiltered:function()},但在Linux机器上却无法使用。 Linux区分大小写,因此我不得不使用require("jQuery")

答案 2 :(得分:0)

安装 NPM我jQuery 在同一个项目中解决我的问题