webpack: Problem with amd modules: "define cannot be used indirect"

Sometimes webpack throws an error saying define cannot be used indirect. It’s caused by various third-party modules (like moment.js) which use a module pattern like

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['moment'], factory); // AMD
    } else if (typeof exports === 'object') {
        module.exports = factory(require('../moment')); // Node
    } else {
        factory(window.moment); // Browser global
    }
}(function (moment) { ... }))

What’s wrong with this module definition? And what can we do about it?

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 1
  • Comments: 34 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks, changed to

module: {
    noParse: /node_modules\/json-schema\/lib\/validate\.js/
}

I am facing this error with jquery-migrate and none of answers did not worked. webpack : 4.28.2 os : windows npm : 6.1.02

I’m experiencing the same problem with the validate.js dependency however if I specify a pattern with more path levels than just /validate\.js/ in noParse, the amd-define.js built-in is added by webpack and the error about indirect define is thrown. Can anyone think of a reason the full node_modules path to the script as provided by @artem-russkikh wouldn’t match (yes, it is in the file system at that path).

Edit: apparently you need to allow for platform specific directory separators in your pattern, that’s all.

The same problem with request@2.67.0 as node-gitlab dependency, but problem was in Validate.js https://github.com/ansman/validate.js/issues/12

module: {
    noParse: /validate\.js/
}

Fixes “define cannot be used indirect” problem

Your noParse config is very risky. It will ignore any file with that name. You should add node_modules or something to disambiguate it.

Take your time, it’s christmas! 🎄 👍