简体   繁体   English

使用Webpack加载引导时出现“ jquery未定义”

[英]'jquery is not defined' when using webpack to load bootstrap

I am just starting to learn to use Webpack (previously I just use the manual way to include individual scripts separately). 我刚刚开始学习使用Webpack(以前,我只是使用手动方式分别包含各个脚本)。 And I used bootstrap-loader for loading bootstrap. 我使用bootstrap-loader来加载bootstrap。 Here is my webpack.config.js 这是我的webpack.config.js

var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')

module.exports = {
    context: __dirname,

    entry: './assets/js/index', // entry point of our app. assets/js/index.js should require other js modules and dependencies it needs

    output: {
        path: path.resolve('./assets/bundles/'),
        filename: "[name]-[hash].js",
    },

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'})
    ],

    module: {
        loaders: [
            { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader'}, // to transform JSX into JS
            { test: /\.css$/, loader: 'style-loader!css-loader'}, // to transform css
            // images
            { test: /\.png$/, loader: 'url-loader?limit=100000'},
            { test: /\.jpg$/, loader: 'file-loader'},
            // bootstrap image files
            { test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff'},
            { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
            { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
            { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}
        ],
    },

    resolve: {
        modulesDirectories: ['node_modules', 'bower_components'],
        extensions: ['', '.js', '.jsx'],
        jquery: './vendor/jquery/jquery.js',
    },
}

And here is my entry.js 这是我的entry.js

global.jQuery = global.$ = require('jquery');
require('bootstrap-loader');

This seems to work. 这似乎有效。 However, I used this before and it did not work: 但是,我之前使用过它,但是没有用:

window.jQuery = window.$ = require('jquery');

I found the line above suggested by so many people. 我发现上面的行有很多人建议。 But I just simply get errors when load the page. 但是我只是在加载页面时出错。 Just some examples: some SO question , webpack issue , another SO question . 只是一些示例: 一些SO问题webpack问题另一个SO问题

Later I found this question , and this question . 后来我找到了这个问题 ,还有这个问题 So the page actually works with bootstrap js functionality working as well. 因此,该页面实际上也可以与bootstrap js功能一起使用。

I will add my package.json as well in case it is relevant: 如果相关,我也会添加我的package.json:

{
  "author": "franklingu",
  "dependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.4",
    "bootstrap-loader": "^1.2.0-beta.1",
    "bootstrap-sass": "^3.3.7",
    "extract-text-webpack-plugin": "^1.0.1",
    "jquery": "^3.1.0",
    "node-sass": "^3.8.0",
    "resolve-url-loader": "^1.6.0",
    "sass-loader": "^4.0.0",
    "webpack": "^1.13.1",
    "webpack-bundle-tracker": "0.0.93"
  },
  "devDependencies": {
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.4",
    "css-loader": "^0.23.1",
    "file-loader": "^0.9.0",
    "node-sass": "^3.8.0",
    "resolve-url-loader": "^1.6.0",
    "sass-loader": "^4.0.0",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.7",
    "webpack": "^1.13.1"
  }
}

I am new to webpack but I am not new to JS. 我是webpack的新手,但不是JS的新手。 I am wondering why window.$ is not working. 我想知道为什么window.$无法正常工作。

And I wonder, for webpack, why some people suggested this in plugins: 我想知道,对于webpack,为什么有人在插件中建议这样做:

        new webpack.ProvidePlugin({
            'window.jQuery': 'jquery',
            'window.$': 'jquery',
        })

Some people are suggesting this (did not work for me either): 有人建议这样做(对我也不起作用):

 resolve: {
    alias: {
        jquery: "jquery/src/jquery"
    }
}

I played with node for a while back then and I remembered that node makes use of request js for loading(I am not very clear about differences between common vs require vs amd though). 那时我与节点玩了一段时间,我记得该节点利用请求js进行加载(尽管我不太清楚common与require与amd之间的区别)。 But why some people mention common js? 但是为什么有些人提到普通的js?

I was developing backend for some time and just started frontend--so many questions are popping up. 我在开发后端已有一段时间,而刚开始使用前端-出现了很多问题。 It would just enough if you provide me with some link to some guide to read which can clear my doubts/build my basic understanding of these. 如果您为我提供了一些阅读指南的链接,这可以消除我的疑惑/建立我对这些问题的基本理解,那就足够了。

Add this as plugin 将此添加为插件

  new webpack.ProvidePlugin({
   $: "jquery",
   jQuery: "jquery"
  })

and you should be able to have jquery in your whole project. 并且您应该能够在整个项目中使用jquery。

If issue persists after adding the plugin, try restarting your nodejs server. 如果添加插件后问题仍然存在,请尝试重新启动您的nodejs服务器。 Remember to install jquery using npm install --save jquery . 记住安装jquery使用npm install --save jquery

Plugin is not needed. 不需要插件。 Just use following as entry: 只需使用以下作为输入:

entry: [
    'jquery', //will load jquery from node_modules
    './assets/js/index',
]

Then in ES6+ file use: 然后在ES6 +文件中使用:

import $ from "jquery";

I know this is a bit old, but I managed to do it like that : 我知道这有点老了,但是我设法做到了:

global.jQuery = require('jquery'); global.jQuery = require('jquery'); require('bootstrap'); require('bootstrap');

I want to share my findings for any future developer who might have the same use-case I do. 我想与任何可能拥有与我相同用例的未来开发人员分享我的发现。

I had the requirement to only create a vendor bundle and not an app bundle for an AngularJS (1.7.2) app, using Webpack 4 (4.16.4). 我要求使用Webpack 4 (4.16.4) 仅为 AngularJS (1.7.2)应用程序创建供应商捆绑包,而不是应用程序捆绑包。 I think because I was only creating a vendor bundle, none of the other solutions worked. 我认为,因为我只创建了供应商捆绑包,所以其他解决方案都无效。 Only expose-loader worked for me, like so: 只有expose-loader对我有用,就像这样:

    // webpack.config.js
    module: {
        rules: [
            {
                test: require.resolve('jquery'),
                use: [{
                        loader: 'expose-loader',
                        options: 'jQuery'
                    },
                    {
                        loader: 'expose-loader',
                        options: '$'
                    }
                ]
            }
        ]
    }

For more information: https://github.com/webpack-contrib/expose-loader 有关更多信息: https : //github.com/webpack-contrib/expose-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM