How to get 2 modules to require each other?

Hi,

I have one global module and then I have multiple other modules that do various things and each one of those requires the global module.

I have a function inside the global module that needs to access a function inside one of the “sub” modules but I cannot require that sub module in the global one because the sub module require the global module and I get an error:  loop or previous error loading module.

Does anyone know how I could get my global module to access the function in my sub module?

Thanks!

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.

Cyclic dependency is just bad and to be avoided at all costs.  It is an absolute nightmare to debug too.

Try and program to a “parent-child” pattern.  A Child can be dependent on a parent but a parent should never depend on a child!

It is time to refactor if you find you are breaking that pattern.

I agree with @sgs, but overcoming this in the rare case you need it can be done thus:
 
modA.lua

local modA = {} modA.foo = function() local modB = require "modB" print("In modA.foo()") modB.bar() end modA.bar = function() print("In modA.bar()") end return modA

 
modB.lua

local modB = {} modB.foo = function() local modA = require "modA" print("In modB.foo()") modA.bar() end modB.bar = function() print("In modB.bar()") end return modB

Usage

local modA = require "modA" local modB = require "modB" modA.foo() modB.foo()

Or alternately, use registration functions…
 
 
modA.lua

local modB local modA = {} modA.register( mod ) modB = mod end modA.foo = function() print("In modA.foo()") modB.bar() end modA.bar = function() print("In modA.bar()") end return modA

modB.lua

local modA local modB = {} modB.register( mod ) modA = mod end modB.foo = function() print("In modB.foo()") modA.bar() end modB.bar = function() print("In modB.bar()") end return modB 

Registration** :**

-- ONLY DO THIS ONCE AND IT IS PERMANENT local modA = require "modA" local modB = require "modB" modA.register( modB ) modB.register( modA)

Usage:

local modA = require "modA" local modB = require "modB" modA.foo() modB.foo()

I most often run into this when coding up a grab bag ‘util.lua’ file that contains snippets of helper code I want to use in various places and then these ‘helpers’ sometimes use features in the main modules.  

If said, ‘main modules’ have a proper hierarchy of dependence, the inclusion of util can sometimes goof this up.

I could fix this by improving order and org, but if I’m in a huge rush or the function just seems too much like a ‘utility’ function I simply use ‘late requires’ as shown above.

Having said all that, SGS is absolutely right.  This is a bad practice.

Thanks for all your solutions guys! They’re all great but I have figured out a way where I no longer need to use cyclic dependency.

This is an awesome forum, thanks for being so active and helpful :slight_smile:

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.

Cyclic dependency is just bad and to be avoided at all costs.  It is an absolute nightmare to debug too.

Try and program to a “parent-child” pattern.  A Child can be dependent on a parent but a parent should never depend on a child!

It is time to refactor if you find you are breaking that pattern.

I agree with @sgs, but overcoming this in the rare case you need it can be done thus:
 
modA.lua

local modA = {} modA.foo = function() local modB = require "modB" print("In modA.foo()") modB.bar() end modA.bar = function() print("In modA.bar()") end return modA

 
modB.lua

local modB = {} modB.foo = function() local modA = require "modA" print("In modB.foo()") modA.bar() end modB.bar = function() print("In modB.bar()") end return modB

Usage

local modA = require "modA" local modB = require "modB" modA.foo() modB.foo()

Or alternately, use registration functions…
 
 
modA.lua

local modB local modA = {} modA.register( mod ) modB = mod end modA.foo = function() print("In modA.foo()") modB.bar() end modA.bar = function() print("In modA.bar()") end return modA

modB.lua

local modA local modB = {} modB.register( mod ) modA = mod end modB.foo = function() print("In modB.foo()") modA.bar() end modB.bar = function() print("In modB.bar()") end return modB 

Registration** :**

-- ONLY DO THIS ONCE AND IT IS PERMANENT local modA = require "modA" local modB = require "modB" modA.register( modB ) modB.register( modA)

Usage:

local modA = require "modA" local modB = require "modB" modA.foo() modB.foo()

I most often run into this when coding up a grab bag ‘util.lua’ file that contains snippets of helper code I want to use in various places and then these ‘helpers’ sometimes use features in the main modules.  

If said, ‘main modules’ have a proper hierarchy of dependence, the inclusion of util can sometimes goof this up.

I could fix this by improving order and org, but if I’m in a huge rush or the function just seems too much like a ‘utility’ function I simply use ‘late requires’ as shown above.

Having said all that, SGS is absolutely right.  This is a bad practice.

Thanks for all your solutions guys! They’re all great but I have figured out a way where I no longer need to use cyclic dependency.

This is an awesome forum, thanks for being so active and helpful :slight_smile: