I have a NextJS app with some server side code that gets bundled with Webpack (default NextJS setup). When running this with the product build, I get the following error:
ReferenceError: Cannot access 'providers' before initialization
Looking at the generated bundle at the lines referenced by the error, I can see this:
const providers = {
[_myapp_webapp_types__WEBPACK_IMPORTED_MODULE_0__.ProviderType.Provider0]: _provider0__WEBPACK_IMPORTED_MODULE_1__/* .provider0 */ .d,
[_myapp_webapp_types__WEBPACK_IMPORTED_MODULE_0__.ProviderType.Provider1]: _provider1__WEBPACK_IMPORTED_MODULE_2__/* .provider1 */ .S
};
function getProvider(config) {
return providers[config.type];
}
In my head, there can be nothing stopping providers
from being initialised when the getProvider
function is called. What is going on here, and what can I do about it?
EDIT: The code is within a separate file that gets imported by another file that calls the getProvider-function. This means (in my head) that the providers variable without doubt should be initialised as soon as the function is called (from outside the module).
(Also, everything works just fine when running locally)