Can't get this to work with NPM and Webpack

Can't get this to work with NPM and Webpack

JakobudJakobud Posts: 3Questions: 1Answers: 0

I get this error:

jquery.dataTables.js:112 Uncaught TypeError: Cannot set property '$' of undefined
at DataTable (jquery.dataTables.js:112)
at eval (data.js:2)
at Object../js/data.js (data.bundle.js:164)
at webpack_require (data.bundle.js:79)
at checkDeferredModules (data.bundle.js:46)
at data.bundle.js:152
at data.bundle.js:155

Here's my code:

var $ = require('jquery');
var dt = require('datatables.net')();

I've also tried

var $ = require('jquery');
var dt = require('datatables.net')(window, $);

And my package.json dependencies:

"dependencies": {
"datatables.net": "^1.10.19",
"jquery": "^3.3.1"
}

No clue what I'm doing wrong here. I am loading the CSS directly into the page. I'm not loading the CSS via webpack.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @Jakobud ,

    This thread here may help - the suggestion was to leave off the () so as not to invoke.

    Hope that does the trick for you,

    Cheers,

    Colin

  • JakobudJakobud Posts: 3Questions: 1Answers: 0

    Well that worked. Why the hell does all the documentation give completely wrong instructions on how to use this? How would the developer of this thing actually expect people to pay for commercial licenses for it with such poor documentation. The documentation is thorough, yes. But with show-stopper documentation issues like this the rest of the documentation is useless.

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    It depends on if you are using AMD or CommonJS as a loader. With AMD it is correct to not execute the import as it just uses a global window and jQuery. With CommonJS however you are more likely to be loading in your own modules, so you can pass in the window and jQuery instance.

    In your Webpack configuration it sounds like you are using AMD.

    This is the UMD DataTables uses if you are interested.

    Its a very good point that this isn't clear in the documentation and I will work on that. Thanks for the feedback.

    Allan

  • JakobudJakobud Posts: 3Questions: 1Answers: 0

    Pretty sure my Webpack is just using normal CommonJS modules. It's practically a default Webpack config.

This discussion has been closed.