1

I want to add web pack provided Plugins to use JQuery globally. Some articles says

You need to put it in webpack.config.js under plugins:[]

But when I look at my webpack.config.js there is no such plugins object. Then I create plugins object manually under the module array.

After that when I try to run the project using npm run dev

it doesn't run. I want to add this plugin to the webpack.config.js

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

How do I do this? This is my webpack.config.js file

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  plugins[
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new webpack.ProvidePlugin({
  Vue: ['vue/dist/vue.esm.js', 'default']
})


],
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

2 Answers 2

1

If you take a quick look at the webpack documentation for plugins here, you would see where the plugins object belongs.

The good news is: your webpack config already looks correct - you put the plugins array as a direct child of the config, after module, instead of inside it. You only miss a : colon after plugins, which likely is a typo.

So I don't see a reason what should not work.

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

1 Comment

=) That may the reason. But even If I correct that thing even after that it doesn't work. It gives me the error Module not found: Error: Can't resolve 'jquery' I import JQuery as a CDN in in my index.html. Do I need to install JQuery as npm
0

Try this in build/webpack.dev.conf.js and build/webpack.prod.conf.js:

plugins: [

  // ...

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

jQuery becomes globally available to all your modules:

3 Comments

Where is the build folder I'm unable to find it in my project. I use webpack-simple. Is that the reason? I have a dist folder. But there is no such a webpack.prod.conf.js file.
try it in the webpack.config.js folder
That is my question, there is no place called plugins when I create that one manually and try to run project doesn't run. See my webpack.config.js file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.