Using require, invalid file path.

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?

Are you sure about the ‘?’ in

package.path = os.getenv("HOME").."/Documents/?"

I believe ? Tells require where to attempt to read the file.

Little bump. As you I needed to include some parts of libs. I’ve came to this solution, overwrite the require function to include paths on the fly, that I keep require caching system:

package.path = package.path..";"..os.getenv("HOME").."/Documents/?.lua"
local _require = require
require = function(mod)
    local p,f = string.match(mod,"(.+)/(.+)")
    local path = "/Documents/"..p
    if p and not string.find(package.path,path) then
        package.path = package.path..";"..os.getenv("HOME")..path..".codea/?.lua"
    end
    return _require(f and f or mod)
end

I put it in a project named _require and link it when I need the require function.
Usage when I want a tab of a project :
mod = require("MyProject/MyTab")
I haven’t yet tested for lua file saved in Documents root.