Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "this.getOptions is not a function" a bug for webpack?

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?

like image 273
K.Yang Avatar asked Sep 11 '25 03:09

K.Yang


1 Answers

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

like image 65
slideshowp2 Avatar answered Sep 13 '25 16:09

slideshowp2