Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack Can't resolve 'jquery'

Tags:

jquery

webpack

Hi I'm a new to Webpack , currently I'm adding this tool to my project, during bundling (webpack ...) jquery-dependent library I get an error like that:

Error: Can't resolve 'jquery' in 'C:\some_folder'

I browsed through similar posts on internet with no positive effect. People recommend using different approaches like : - resolve.alias - plugins ProvidePlugin

I use webpack 3.3.0, in my project jQuery is loaded in normal way via script tag before vendor bundle scripts. Most of vendor librairies including jQuery live not in node_modules folder.

webpack.config.js:

const webpack = require('webpack');
const { resolve } = require('path');

module.exports = env => {
    return {
        context: resolve('src'),
        entry: {
            comp: './start.comp.js',
            culture: './start.culture.js',
            strategy: './start.strategy.js',
            vendors: './start.vendors.js'
        },
        output: {
            path: resolve('dist'),
            publicPath: '/dist/',
            filename: 'bundle.[name].js'
        },
        devtool: env.dev ? 'eval' : 'source-map'
    };
};

the last entry jquery.placeholder.min.js is a problem

require('./../assets/vendor/typeahead.js');
require('./../assets/vendor/hogan-2.0.0.js');
require('./../assets/vendor/swfobject.js');
require('expose-loader?_!./../assets/vendor/underscore-min.js');
require('expose-loader?FastClick!./../assets/vendor/fastclick.js');
require('expose-loader?AOS!./../assets/vendor/aos.js');
require('./../assets/vendor/jquery.placeholder.min.js');
like image 627
kuba0506 Avatar asked Jul 26 '17 14:07

kuba0506


People also ask

Why am I getting a Webpack config error when importing jQuery?

You also need to add node_modules to keep the regular module resolution. Well in my case it was something about importing jquery instead of jQuery, it is a webpack config: resolve error using following command. If you are using React with Bootstrap and you're getting this error, it may be because you're using the lower version of Bootstrap.

What is the use of expose require resolve in Webpack?

The require.resolve call is a Node.js function (unrelated to require.resolve in webpack processing). require.resolve gives you the absolute path to the module ( "/.../app/node_modules/jquery/dist/jquery.js" ). So the expose only applies to the jquery module. And it's only exposed when used in the bundle.

Is it possible to change the default resolve in Webpack?

Webpack provides reasonable defaults, but it is possible to change the resolving in detail. Have a look at Module Resolution for more explanation of how the resolver works. Configure how modules are resolved. For example, when calling import 'lodash' in ES2015, the resolve options can change where webpack goes to look for 'lodash' (see modules ).

How to add jQuery as an external module in Webpack?

Webpack sees require ("jquery") and tries to bundle the jQuery library (which does not exist in the node_modules). The solution is to add jQuery as an external in your webpack.config.js: When a module is marked as an external, Webpack knows not to bundle it, but instead use the global variable.


2 Answers

Problem

Since jquery.placholder.min.js is using UMD as its loading strategy, it's recognizing that it is required via a require - and tries to require jQuery in the same way:

"object"==typeof module&&module.exports?require("jquery"):jQuery

Webpack sees require("jquery") and tries to bundle the jQuery library (which does not exist in the node_modules).

Solution

The solution is to add jQuery as an external in your webpack.config.js:

{
  ...
  externals: {
    // require("jquery") is external and available
    //  on the global var jQuery
    "jquery": "jQuery"
  }
}

When a module is marked as an external, Webpack knows not to bundle it, but instead use the global variable.

like image 170
lyosef Avatar answered Sep 17 '22 18:09

lyosef


I was working in react i have found that when i import the react-bootstrap components sometimes the components come like {Buttons} from bootstarp; then i got this error. But i solved it by calling the path from bootstrap to react-bootstrap;

Check your auto import call and try changing it. this worked for me.

like image 26
NAiM ALAM Avatar answered Sep 16 '22 18:09

NAiM ALAM