0

I'm unable to successfully include jQuery UI in my project using webpack. jQuery has been working, but when I visit a page of my app that requires jQuery-UI I get the following warning:

jQuery.Deferred exception: $(...).draggable is not a function TypeError: $(...).draggable is not a function

And error:

jquery.js:4055 Uncaught TypeError: $(...).draggable is not a function

I'm nearly brand new to building javascript code using webpack/node, so it's very possible I've made a naive mistake.

This is my config file:

config.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const webpack = require('webpack');


module.exports = {
    entry: './src/js/index.js',
    mode: 'development',
    output: {
        filename: 'index.js',
        path: 'path/js',
    },
    module: {
        rules: [
            {
                test: /\.(scss)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    'sass-loader'
                ]
            },
            {
                test: /\.js$/,
                enforce: 'pre',
                use: ['source-map-loader'],
              },
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '../css/index.css',
        }),
        new webpack.ProvidePlugin({
            jQuery: 'jquery',
            $: 'jquery',
            'window.jQuery': 'jquery',
            'window.$': 'jquery',
        })
    ],
    resolve : {
        alias: {
            // bind version of jquery-ui
            "jquery-ui": "jquery-ui/jquery-ui.js",
            // bind to modules;
            modules: path.join(__dirname, "node_modules"),
        }
    },
}

My entrypoint:

index.js

import '../sass/index.scss';
const $ = require("jquery");
require('webpack-jquery-ui');
require('webpack-jquery-ui/draggable');
global.$ = global.jQuery = $;
import 'bootstrap';

Each time I make a change I run the command:

npx webpack --config config.js

There are no errors output from this.

Where am I going wrong?

Update Upon Chris G suggestion in comments I've tried the following:

import '../sass/index.scss';
const $ = require('webpack-jquery-ui');
global.$ = global.jQuery = $;
import 'bootstrap';

Upon rebuilding I get a series of errors in the console such as:

ERROR in (webpack)-jquery-ui/index.js
Module not found: Error: Can't resolve 'jquery- 
ui/themes/base/autocomplete.css' in 
'.../webpack/node_modules/webpack-jquery-ui'
@ (webpack)-jquery-ui/index.js 57:0-49
@ ./src/js/index.js

This is what's returned when running "npm install --save jquery jquery-ui"

npm WARN saveError ENOENT: no such file or directory, open '.../webpack/package.json' npm WARN enoent ENOENT: no such file or directory, open '.../webpack/package.json' npm WARN webpack No description npm WARN webpack No repository field. npm WARN webpack No README data npm WARN webpack No license field.

Could this now be an install problem?

2
  • According to the docs you don't need to include jquery because webpack-jquery-ui already contains both. Commented Sep 16, 2020 at 7:08
  • Thanks @ChrisG - I've made a tweak based on your suggestion and updated the question to explain what happens. Struggling to re-build the code, I must have mis-understood. Commented Sep 16, 2020 at 7:53

1 Answer 1

1

From my understanding, there are couple of things you need to do:

  • All you need to do is to only install webpack-jquery-ui.

  • I don't think you should are now resolving the correct jquery-ui in your webpack.config.js, just remove that alias of "jquery-ui"

resolve: {
  alias: {
    // bind to modules;
    modules: path.join(__dirname, "node_modules"),
  }
},
  • Finally, I don't think webpack-jquery-ui exports a jquery object for you so don't assign and export it in global anymore since it's currently available in your module with ProvidePlugin. So just simply write like following:
import '../sass/index.scss';

require('webpack-jquery-ui');
require('webpack-jquery-ui/draggable');

// $ is available in the module via `ProvidePlugin`
global.$ = global.jQuery = $;
import 'bootstrap';
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - after actioning I get a long list of errors all very similar for different parts of jquery-ui: ERROR in ./node_modules/jquery-ui/themes/base/accordion.css 11:0 Module parse failed: Unexpected token (11:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See webpack.js.org/concepts#loaders | * api.jqueryui.com/accordion/#theming | */ > .ui-accordion .ui-accordion-header { | display: block; | cursor: pointer; @ (webpack)-jquery-ui/index.js 56:0-46 @ ./src/js/index.js
It looks like to import css automatically but you forgot to handle this file. Just try add the css files for your css loaders by changing to test: /\.(scss|css)$/,
Boom! You solved it :D Thanks a bunch. I'm thinking these errors will make more sense in a couple of weeks time of using webpack to build my code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.