【问题标题】:Webpack jQuery and jQuery.lazy() TypeError: q is undefinedWebpack jQuery 和 jQuery.lazy() TypeError: q is undefined
【发布时间】:2019-02-20 10:24:56
【问题描述】:

如何将 jqueryjquery.lazy 与 webpack 捆绑在一起? - 每次我尝试这个,我都会得到一个错误。

我的浏览器调试控制台中的错误如下:

TypeError: q is undefined

其余的工作正常。也许你可以看出我做错了什么。

我的 package.json:

{
      "name": "mrd-apple",
      "version": "1i.0.0",
      "description": "mrd Apple",
      "main": "webpack.config.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "jquery": "^3.3.1",
        "jquery-lazy": "^1.7.10",
        "postcss-loader": "^3.0.0",
        "webpack": "^4.29.3"
      },
      "devDependencies": {
        "css-loader": "^2.1.0",
        "style-loader": "^0.23.1",
        "webpack-cli": "^3.2.3"
      }
    }
}

我的 webpack.config.js

var webpack = require('webpack');
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
var OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = function(env) {
    return {
        entry: ['./src/js/index.js'],
        output: {
            path: __dirname + '/dist',
            filename: 'bundle.js'
        },
        plugins: [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery'
            }),
            new MiniCssExtractPlugin({
                // Options similar to the same options in webpackOptions.output
                // both options are optional
                filename: "[name].css",
                chunkFilename: "[id].css"
            }),
            new OptimizeCSSAssetsPlugin({
                assetNameRegExp: /\.optimize\.css$/g,
                cssProcessor: require('cssnano'),
                cssProcessorPluginOptions: {
                    preset: ['default', { discardComments: { removeAll: true } }],
                },
                canPrint: true
            })
        ],
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader,
                            options: {
                                // you can specify a publicPath here
                                // by default it use publicPath in webpackOptions.output
                                publicPath: '../'
                            }
                        },
                        "css-loader"
                    ]
                }
            ]
        },
        optimization: {
            minimizer: [
                new UglifyJsPlugin({
                    cache: true,
                    parallel: true,
                    sourceMap: true // set to true if you want JS source maps
                }),
                new OptimizeCSSAssetsPlugin({})
            ]
        },
    }
}

index.js 的内容

// JS 
// require('./jquery.lazy'); 
require('./lightslider'); 
require('./simple-lightbox'); 
require('./init'); 

// CSS 
require('./../css/index.css');

【问题讨论】:

  • 你在哪里得到这个错误?我的意思是哪个文件抛出了那个错误?
  • 在控制台中,当我打开我的网址时。
  • 不需要 'jQuery' 依赖,因为 jquery-lazy 依赖于 jquery 并与它一起安装 - github.com/eisbehr-/jquery.lazy/blob/master/package.json。在 github 问题中提出了与您类似的问题 - github.com/eisbehr-/jquery.lazy/issues/195
  • 当我只安装 jquery-lazy 时,也是同样的问题。 - 并且没有定义 jQuery。
  • @yfain,你的 index.js 文件里有什么?

标签: javascript jquery webpack


【解决方案1】:

您的问题在于您尝试使用 require.js 的方式

首先,你的 package.json 有问题:

  • webpackpostcss-loader 都是 devDependencies。
  • 您的版本也无效。
  • 我必须将以下内容添加为 devDependencies:uglifyjs-webpack-pluginmini-css-extract-pluginoptimize-css-assets-webpack-plugin

然而,问题在于您如何尝试包含这些库。

当将 require.js 与 package.json 和 webpack 一起使用时,您不会尝试要求 .js 文件本身,而是require('npm-package-name') 或者更好的是import NpmPackageName from 'npm-package-name';见:Dependency Management

如果您想在页面上通过 HTML 加载库,而只是在源代码中引用它,您可以使用 webpack 外部组件来实现。见:Webpack Externals

作为参考,这是我的文件版本:

package.json:

{
  "name": "mrd-apple",
  "version": "1.0.0",
  "description": "mrd Apple",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^3.3.1",
    "jquery-lazy": "^1.7.10"
  },
  "devDependencies": {
    "css-loader": "^2.1.0",
    "mini-css-extract-plugin": "^0.5.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "postcss-loader": "^3.0.0",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  }
}

index.js:


// require('jquery');
// require('jquery-lazy');

import $ from 'jquery';
import jqueryLazy from 'jquery-lazy';


注意:我无法产生您提供的确切错误,但假设这是由于项目设置本身造成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2014-09-09
    • 1970-01-01
    • 2019-09-27
    • 2013-04-25
    相关资源
    最近更新 更多