6

How to require jquery file with webpack should I have to install from npm, or can I just require the file download from jquery website?

directory

app/
./assets/javascripts/article/create/base.js
./assets/javascripts/lib/jquery-1.11.1.min.js
webpack.config.js

base.js error

require('../../../lib/jquery-1.11.1.min.js'); 
var Content = function() {
};
module.exports = Content;

webpack.config.js

module.exports = {
  entry: {
    'ArticleCreate':['./assets/javascripts/Article/Create/Base.js']
  },
  output: {
    path: './assets/javascripts/bundle/',
    filename: '[name].js'
  }
};

2 Answers 2

5

Have you tried:

module.exports = {
    ...
    resolve: {
        alias: {
            jquery: "jquery/src/jquery"
        }
    }
};

There is a related answer which shows all options of importing jQuery here: Managing jQuery plugin dependency in webpack

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

3 Comments

yes I tried this resolve: { alias: { jquery: "./assets/javascripts/lib/jquery-1.11.1.min.js" } }, and use var $ = require('jquery'); but if shows error
I also tried resolve: { root: path.resolve('./assets/javascripts/lib'), extensions: ['', '.js'] }, and var $ = require('jquery-1.11.1.min.js'); it works but I just don't get why can't I use resolve alias ?
I found why, github.com/webpack/webpack/issues/109 seems have to use absolute path.
1

I have had success with installing jQuery with npm

npm install jquery --save

Then

var $ = require('jquery'), jQuery = $, ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.