How to refactor very large legacy JavaScript code? [closed]

I have a large JavaScript file with over 20,000 lines of code. The code is organized into objects, with functions like so:

let module1 = {
 fun1:function() {
  console.log("module1 fun1 called");
 },
 fun2:function() {
  module2.fun1();
 },
}
let module2 = {
 fun1:function() {
  console.log("module2 fun1 called");
 },
 fun2:function() {
  module1.fun1();
 },
}

And this pattern continues for other objects. These objects and their functions depend on each other, leading to a lot of circular dependencies. Since everything is currently in a single file, this is not an issue, but I want to refactor this code by separating it into multiple files for better maintainability.

The challenge I’m facing is how to break the code into separate modules without running into circular dependency issues during the refactor. I also need to ensure the structure remains flexible and scalable as I may need to organize modules into sub-modules.

How can I refactor this file?