You’re doing it wrong. But that’s ok, we’ve all done it.
What you’re experiencing is something all compilers force you to avoid, called cyclic dependency. That is, two things which depend on each other. And there are very good reasons for avoiding it.
How to “not do that”? Well, let’s assume that your ‘global’ module is a library which has very useful functions and the ‘sub’ modules are the working parts of your app…
If you define your ‘global’ module like this:
-- my global module local lib = {} function lib.aUsefulGlobalFunc( otherModule, param1, param2 ) -- do something interesting return "some kind of value" end return lib
In any other module (read: all of your ‘sub’ modules) you can then use your global module, like this:
-- my 'sub' module 1 local globallib = require("globallib") local sublib = {} function sublib.doSomethingSubby() local aVal = globallib.lib.aUsefulGlobalFunc( sublib, "a parameter", "another parameter" ) end return sublib
Note: I always call my library modules ‘xxxlib.lua’ and I always create a table at the top and return it at the bottom, because that’s what Lua’s module system expects.