0

I want to integrate a downloaded javascript file from the source folder.

Consider that the css and js files exists in the path.

From what I gather to integrate a css in the main.js I just add

require('./vendor/bootstrap/css/bootstrap.min.css')

but how to add a js file ? When I add the js files the same way

require('./vendor/jquery/jquery.min.js')
require('./vendor/bootstrap/js/bootstrap.bundle.min.js')

I get an error message

./src/vendor/bootstrap/js/bootstrap.bundle.min.js
Module not found: Error: Can't resolve 'jquery' in '/home/<cool path>/ClientVueJs/src/vendor/bootstrap/js'
 @ ./src/vendor/bootstrap/js/bootstrap.bundle.min.js 14:125-142
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
3
  • apparently bootstrap.bundle.min.js expects jquery. Try to instal it via npm and require it before bootstrap. Commented Oct 16, 2018 at 8:56
  • Do I add it to the package.json file and run npm install or how is jquery integrated. I thought that I already integrated the jquery file Commented Oct 16, 2018 at 9:06
  • I believe here is what your are looking for. Commented Oct 16, 2018 at 9:13

1 Answer 1

0

Add alias for jquery to webpack config(and remove your require line require('./vendor/jquery/jquery.min.js')), because bootstrap depends on 'jquery' module:

    module.exports = {
    //...
        resolve: {
            alias: {
                jquery: path.resolve(__dirname, 'src/vendor/jquery/jquery.min.js')
            }
        }
    };

This binding lets webpack resolve require('jquery') from bootstrap file.

If you need to map library to additional variables like $ you have to use ProvidePlugin, check @Arseniy-II's comment with link.

Sign up to request clarification or add additional context in comments.

2 Comments

I have added the allias line to my webpack.base.conf.js file but I still get the error Module not found: Error: Can't resolve 'jquery' in ',somepath./ClientVueJs/src' @ ./src/main.js 24:0-17
path should be relative to project directory, i think it should be src/vendor/jquery/jquery.min.js in alias.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.