Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: expressing module dependency

Tags:

I'm trying to require the bootstrap-webpack module in my webpacked application.

It appears to need jQuery, since the bundled javascript then throws the following:

Uncaught ReferenceError: jQuery is not defined

How do I go about specifying to webpack that jQuery is a dependency for the bootstrap-webpack module, to fix this issue? It feels like it should be trivial, but I've been struggling to figure it out.

I've tried adding:

"jquery": "latest" 

to the dependecies in the bootstrap-webpack's package.json, but this didn't work. The documentation is incomplete, and I can't seem to find much about this issue. It should be trivial, right? Help!

like image 798
eye_mew Avatar asked Nov 05 '14 03:11

eye_mew


People also ask

Does webpack include node_modules?

Webpack builds a dependency graph used internally Now all modules that are used in your app are included in the dependency graph. Your project have many installed dependencies in the node_modules folder that should not be included in your client-side JavaScript production bundle.

What are module rules in webpack?

module.rules An array of Rules which are matched to requests when modules are created. These rules can modify how the module is created. They can apply loaders to the module, or modify the parser.

Does webpack use CommonJS?

Webpack supports the following module types natively: ECMAScript modules. CommonJS modules.


1 Answers

There are two possible solutions:

Use the ProvidePlugin: It scans the source code for the given identifier and replaces it with a reference to the given module, just like it has been required.

// webpack.config.js module.exports = {     ...     plugins: [         new webpack.ProvidePlugin({            $: "jquery",            jQuery: "jquery"        })     ] }; 

Use the imports-loader: It provides the possibility to prepend preparations like require() statements.

// webpack.config.js {     ...     module: {         loaders: [             { test: require.resolve("jquery"), loader: "imports?jQuery=jquery" }         ]     } } 

In that case you need to run npm install imports-loader --save before.

like image 93
Johannes Ewald Avatar answered Sep 27 '22 22:09

Johannes Ewald