0

i am new to webpack. i got installed through https://github.com/vuejs/vue-cli I donl't quite understand how to add jquery dependency. i have tried just about every tutorial i could find and nothing seems to work.

I am using bootstrap and it keeps outputting a console error bootstrap requires Jquery.

in my node_modules/ folder i have Jquery there.

I don't exactly understand where the entry point is for the project. I believe it is packages.json so i will start there

in my scripts portion i have

"scripts": {
    "dev": "node build/dev-server.js",
    "build": "node build/build.js",
    "lint": "eslint --ext .js,.vue src"
  },
  "dependencies": {
    "jquery": "^3.1.1",
    "vee-validate": "^2.0.0-beta.18",
    "vue": "^2.1.0",
    "vue-resource": "^1.0.3",
    "vue-router": "^2.1.1"
  },

then if i am reading this correctly inder 'dev' mode i have build/dev-server.js as an entry point.

require('./check-versions')()
var config = require('../config')
if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var opn = require('opn')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = require('./webpack.dev.conf')

In that block of code i have tried adding var jquery = require('jquery') but that did not solve the issue.

then the next port of entry i believe is the webpack base.

which i have

var path = require('path')
var config = require('../config')
var utils = require('./utils')
var projectRoot = path.resolve(__dirname, '../')
var webpack = require('webpack');

var jquery = require("jquery");

var env = process.env.NODE_ENV
// check env & config/index.js to decide whether to enable CSS source maps for the
// various preprocessor loaders added to vue-loader at the end of this file
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd

module.exports = {
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.vue', '.json'],
    fallback: [path.join(__dirname, '../node_modules')],
    alias: {
      jquery: "jquery/src/jquery",
      'vue$': 'vue/dist/vue.common.js',
      'src': path.resolve(__dirname, '../src'),
      'assets': path.resolve(__dirname, '../src/assets'),
      'components': path.resolve(__dirname, '../src/components')
    }
  },
  resolveLoader: {
    fallback: [path.join(__dirname, '../node_modules')]
  },
  module: {
    preLoaders: [
      {
        test: /\.vue$/,
        loader: 'eslint',
        include: [
          path.join(projectRoot, 'src')
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(js|min.js)$/,
        loader: 'eslint',
        include: [
          path.join(projectRoot, 'src')
        ],
        exclude: /node_modules/
      },
    ],
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue'
      },
      {
        test: /\.js$/,
        loader: 'babel',
        include: [
          path.join(projectRoot, 'src')
        ],
        exclude: /node_modules/
      },
      {
        test: /\.json$/,
        loader: 'json'
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
         test: require.resolve("jquery"),
         loader: "expose?$!expose?jQuery" },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    })
  ],
  eslint: {
    formatter: require('eslint-friendly-formatter')
  },
  vue: {
    loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),
    postcss: [
      require('autoprefixer')({
        browsers: ['last 2 versions']
      })
    ]
  }
}

which i have tried adding the require at the top , as well as the resolve alias, and the plugin for jquery which i all got from other tutorials. But nothing seems to work. what am i doing wrong?

2
  • step 5 of the webpack docs seems like what you're looking for. You should just need to import it from your app's entry point. Also that's a pretty large webpack base. Commented Jan 31, 2017 at 18:31
  • thats basically the default one aside from anything jquery Commented Jan 31, 2017 at 18:34

1 Answer 1

1

Adding this in webpack.base.conf.js

Before module exports var webpack = require('webpack') As well as:

resolve:{
 //other code
 plugins:[
  new webpack.ProvidePlugin({
   $: "jquery",
   jQuery:'jquery',
   jquery:'jquery'
  }),
 ],
//other code
}

Make sure you install jquery via npm install jquery with the specific version you need. The code above will place the 3 names under the window object and you will have access to jquery globally as if you imported it as a script tag.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.