Allow multiple JS files to read each other

I’m working on a project where my JavaScript file is nearly 2000 lines long. I have it sorted into sections, but it’s frustrating to have to scroll up and down through lots of code.

It would be great if I could separate my JS file into multiple files that can read global variables and call functions from each other. This technically works at the moment, but glitch flags the errors when the function I’m calling isn’t defined in that file.

If there was an option to have tabs on a single JS file, or tell glitch to treat two JS files as one, that would help me organise my code.

In this case, breaking down the file into smaller JS modules (which is totally different from blindly splitting a huge file into smaller ones) can help a lot. That way, you get better project management, and you don’t need to deal with implicit global variables. That works outside of glitch too.

If the JS file is run using Node (e.g. server script) then use require to import and module.exports to export. NodeJS docs, tutorial.

If the JS file is for browser, use the same style as NodeJS and use something like Browserify to bundle the modules into one file automatically.