Anyone familiar with modules, as in using module.exports and requiring that file in another?
I’ve implemented it, (Webpack is combining the two files into a bundle.js) but now my previously working functions are complaining that variables are not defined. In particular a global variable defined in my main app.js file (kinda stores the app state so is needed to be accessible throughout the app), but needed in a function I moved to the helpers.js module. I’m wondering if I need to move this global variable to the module… but then it won’t be available to the main app.js file. I’ve tried various things such as prefixing with ‘window.’ , using ‘export let myGlobalVar’ in the module file but am not getting very far.
All the resources I’ve found show how to get a module to import, but none seem to discuss use of global vars (yes, I know generally not to use them, but this one is storing the state that is used everywhere), or general guidance on how to troubleshoot/restructure/rewrite code once you’ve moved it to a module, for it to continue to function correctly.
Update: This is not an Express app. It’s just a basic front-end app. All I want to do is split out some functions into their own file and use webpack to recombine them back into a single file. I believe you can do a similar thing with multiple script tags in the index.html file to include multiple js files, but I’m trying to do it in a more scalable way with Webpack.
Any advice/guidance appreciated, I think I have a fundamental piece of understanding missing.
Cheers,
Tim.