question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] `Uncaught TypeError: $(...).select2 is not a function` with `4.1.0-rc.0 `

See original GitHub issue

I just wasted an hour because npm installed version 4.1.0-rc.0. With 4.0.13 everything works as expected.

I’m using webpack and imported it with import 'select2'

When running

$('.select2').select2()

I get the error Uncaught TypeError: $(...).select2 is not a function

I’m not sure if it’s related to using webpack or because I’m using jQuery v3.6.0 (the release notes say The minimum jQuery version supported is the latest version in the 1.x, 2.x, and 3.x series). But in either way it definitely should either work or output a better error message.

Additional Notes

I had to add the following webpack config (using laravel-mix) because I’m unfortunately have to use WordPress and jQuery is loaded via an own script tag, not included in the webpack bundle:

mix.webpackConfig({
	externals: {
		$: 'jQuery',
		jquery: 'jQuery',
	}
})

without this config select2 won’t attach to jQuery.fn

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
nzycommented, Feb 18, 2022

This works:

import select2 from 'select2';
select2();

or

import("select2").then(m => m.default());

and later

$(el).select2()
2reactions
nyrfcommented, Sep 5, 2021

I use vite, it seems no versions can work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Select2() is not a function - Stack Overflow
After I loaded a part of code dynamically select2 couldn't work out on a new selectboxes with an error "$(...).select2 is not a...
Read more >
TypeError: $(...).select2 is not a function in jQuery | bobbyhadz
The "$(...).select2 is not a function" jQuery error occurs for multiple reasons: Forgetting to include the select2 library. Loading the select2 library before ......
Read more >
Select2 is not a function - Integrations
I'm trying to use select2 through webpack. Here's what I get : TypeError: $(...).select2 is not a function. My package.json :.
Read more >
Laravel Vite & JQuery - Laracasts
I get "Uncaught ReferenceError: $ is not defined" before [vite] connecting, ... I have "select2": "^4.1.0-rc.0", in package.json in dependencies. Copy Code
Read more >
Select2 with MultiSelect Box Displaying Problems
Hello, I'm using the select2 script and I'm having a bug come up. I have a list of schools in my survey that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found