0

I have a node.js backend project, and I use webpack to build it.

"webpack": "^4.46.0",
"webpack-cli": "^4.4.0"

it reports error like

Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: this.getOptions is not a function

Then I read the source code of /node_modules/babel-loader/lib/index.js to figure out why.

Screenshot of the source code with problem

As you can see in the screenshot, there's only one search result of this.getOptions() in the node_modules/babel-loader/lib/index.js file. No definition of the function and it just be used. It's supposed to be a bug, at least it looks like that.

Why does the babel team write souce code like that? And why do all others think it's a version conflict but not a code bug itself?

blurfus
  • 13,485
  • 8
  • 55
  • 61
K.Yang
  • 9
  • 4
  • Which version of babel-loader are you using and why have you been ignoring the warning about the Webpack peer dependency? – Phil Jul 26 '23 at 06:26

1 Answers1

1

See getOptions method for Loaders

Webpack 5 ships with built-in this.getOptions method available in loader context. This is a breaking change for loaders that had been using getOptions method from previously preferred schema-utils:

this.getOptions is available since Webpack 5

babel-loader 9.x supports webpack 5.x, since it uses this.getOptions

If you want to use webpack 4.x, then you should use babel-loader 8.x which supports webpack 4.x or 5.x. babel-loder 8.x uses loaderUtils.getOptions(this)

The version compatibility instructions are very clear on the readme

Lin Du
  • 88,126
  • 95
  • 281
  • 483