setmetatable(_G, { __index = function(t, k) -- assume any nonexistent global is a module we haven't loaded yet local ok, result = pcall(require, k) -- if the require failed then we can't do anything more if not ok then error("Failed to autoload " .. k .. ": " .. result) end -- check if the module has an initializer if result.initialize then result:initialize() end -- got something, bind it to the module name and return it rawset(t, k, result) return result end; })