How to use select2 with webpack?

I loaded select2 as a node module:

 npm install select2 

and included it in my app.js :

 require('select2')($); 


When I start webpack , there are no errors, but when I open the application, I get:
 Uncaught TypeError: Object.defineProperty called on non-object(…) 

coming from select2.js :

 S2.define('select2/core',[ 'jquery', './options', './utils', './keys' ], function ($, Options, Utils, KEYS) { (...) } 

Does this select2 because the module shell for select2 only works with AMD and is not compatible with CommonJS?

+5
source share
1 answer

Where do you see how to use select2? As far as I can see from the look at the project, you need to install jquery as dep, but then it will be automatically required .

Looking at the signature of the exported function, it looks like it can take a jQuery element and parameters: https://github.com/select2/select2/blob/master/dist/js/select2.js#L5052

However, after importing it, it just needs to be attached to jQuery as a plugin, so I think $('.some-element').select2([options]); will also work.

So you just tried require('select2') (and npm i jquery --save if you didn't)?

+1
source

Source: https://habr.com/ru/post/1261775/


All Articles