Lua package.loaded cache behavior during module source updates
0 reputation · 07 Jun 2025, 10:14 UTC
In Lua (versions 5.1 through 5.4), the require function utilizes the package.loaded table to cache modules. Once a module is successfully loaded, subsequent calls to require return the cached value from this table without re-executing the source chunk from disk.
This mechanism creates a challenge when source files are modified during the execution of a long-running process. Because Lua does not implement native file-system monitoring or automatic cache invalidation, the state remains stale until the Lua state is restarted or the cache is manually manipulated.
There is uncertainty regarding the safest way to handle module refreshes without introducing inconsistent states, as other modules may still hold references to the original cached version of the module.
- What is the documented behavior for ensuring a module is re-evaluated from disk without restarting the Lua state?
- Are there side effects when manually clearing
package.loadedentries in environments where multiple modules share dependencies?