I’m trying to figure out if I can get require to work on files saved in the documents folder. I can’t seem to get a valid file path.
package.path = os.getenv("HOME").."/Documents/?"
local test = require("mod_test")
-Error
error: [string "function setup()..."]:3: module 'mod_test' not found:
no field package.preload['mod_test']
no file '/private/var/mobile/Applications/5C085C9F-8529-4298-AD0F-41255E4DE931/Documents/mod_test'
no file './mod_test.so'
no file '/usr/local/lib/lua/5.1/mod_test.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
My work around is by overwriting require with this:
function setup()
local mod = require("mod_test.lua")
mod.foo() --prints "Hello World"
end
require = function(name)
local path = os.getenv("HOME").."/Documents/"
local rtn = assert(loadfile(path..name))
return rtn()
end
--mod_test.lua
local mymodule = {}
function mymodule.foo()
print("Hello World!")
end
return mymodule
This seems to get the job done. But why can’t require find the correct path?