最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use select2 with webpack? - Stack Overflow

programmeradmin3浏览0评论

I've downloaded select2 as node module by:

npm install select2

and included it in my app.js:

require('select2')($);


When I run webpack there are no errors, but when I open the app I get:

Uncaught TypeError: Object.defineProperty called on non-object(…)

ing from select2.js:

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

Does it happen because module wrapper for select2 works only with AMD and is inpatible with CommonJS?

I've downloaded select2 as node module by:

npm install select2

and included it in my app.js:

require('select2')($);


When I run webpack there are no errors, but when I open the app I get:

Uncaught TypeError: Object.defineProperty called on non-object(…)

ing from select2.js:

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

Does it happen because module wrapper for select2 works only with AMD and is inpatible with CommonJS?

Share Improve this question asked Dec 23, 2016 at 9:54 van_folmertvan_folmert 4,51710 gold badges51 silver badges97 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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

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

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

So did you simply try require('select2') (and npm i jquery --save if you haven't)?

if someone looking for this now just doing require('select') is not going to attache it jquery you have to require('select2')(); then you can call

$(document).ready(()=>{
  $('.select2').select2()
})

then It will work. now I have tested this with electron js. it's working!

in my doc, this is how I import everything first jquery then select2

  window.$ = window.jQuery = require("jquery");
  require('select2')();
发布评论

评论列表(0)

  1. 暂无评论