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
- more reliable in parsing AMD stuff fixes #138 — committed to webpack/webpack by sokra 11 years ago
- track free vars over IIFEs #138 — committed to webpack/webpack by sokra 11 years ago
- track free vars over IIFEs #138 — committed to webpack/webpack by sokra 11 years ago
- Fixing webpack incompatibilty with json-schema. Read here for details: * https://github.com/kriszyp/json-schema/issues/59 * https://github.com/webpack/webpack/issues/2101 Used a fix mentioned here: ... — committed to MOOCFetcher/moocfetcher-api by deepakjois 8 years ago
- Requests loads a json validator that messes with how webpack ensures the module export system does not fail. Added noParse to that file after viewing discussion here https://github.com/webpack/webpac... — committed to N0taN3rd/wail by N0taN3rd 8 years ago
- Dont parse `node_modules/json-schema/lib/validate.js` in order to avoid "define cannot be used indirect" error - See `https://github.com/webpack/webpack/issues/138` — committed to halhenke/reactor by halhenke 8 years ago
- add workaround to handle webpack issue https://github.com/webpack/webpack/issues/138 — committed to scitran/organizer by deleted user 8 years ago
Thanks, changed to
I am facing this error with
jquery-migrate
and none of answers did not worked. webpack : 4.28.2 os : windows npm : 6.1.02I’m experiencing the same problem with the validate.js dependency however if I specify a pattern with more path levels than just
/validate\.js/
innoParse
, 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
Fixes “define cannot be used indirect” problem
Your
noParse
config is very risky. It will ignore any file with that name. You should addnode_modules
or something to disambiguate it.Take your time, it’s christmas! 🎄 👍