2
votes

I'm writing a Lua program, in which there are lots of modules that is independent from each other. Every module keeps related source files in its folder. What I want is to override require function in each module so that when require called, file in module folder is loaded.

Assume that I have modules below and each of them has a file named utils.lua:

src
├── module1
│   ├── main.lua
│   └── utils.lua
├── module2
│   └── utils.lua
├── module3
│   └── utils.lua
└── utils.lua

and I'm writing module1.main. I want ability to write something like

require 'utils' -- load module1.utils actually.

-- Do something...

Any good idea?

Notes: I need require for I have some custom searchers.

1

1 Answers

0
votes

why not writing ?

local utils = require "module1.utils"

if you don't choose to do so, the proper way to change the require behavior is to add your own function to package.loaders.