'Can't figure out how to use webpack's require.context()
I have an AngularJS with webpack project, and I want to import all the .js files in my project to webpack without adding the path of each file individually, so on the webpack docs I found this require.context() and it looked straight forward, but for some reason I keep getting this error:
webpackEmptyContext(req) {
var e = new Error("Cannot find module '" + req + "'");
e.code = 'MODULE_NOT_FOUND';
throw e;
}
This is how the function is declared, I used the same example that they gave on the docs:
function importAll(r) {
console.log(r)
r.keys().forEach(r);
}
importAll(require.context('./src', true, /\.js$/));
I'm using node 10 and webpack 5.
Solution 1:[1]
The path argument in the require.context call is relative to your file that declared the function. If the file that calls require.context is deep in your src directory, you may try locating your src directory by something like ../../../../src and put this string into the argument.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Finn Lewis |
