Unable to require from subdirectory

Create issue
Issue #93 invalid
Andrew Bibby created an issue

Hopefully, I'm doing something wrong, and the answer is easy. Let's say I have a structure like this,


I'm appending lib to the package.path in main.lua

package.path = package.path .. ';lib/?.lua;'

When log.lua is in the project root, of course there is no problem. But I can't figure out how to require any file from a subdirectory when packaged and run on Android. love . from the command line works ok on Ubuntu.

Printing ... shows that the current file is main, but I have no other path information to make a more explicit declaration. I've also tried ./lib/?.lua (starting with dot slash).

Must all of my files reside in one project root directory?

Comments (6)

  1. Martin Felis repo owner

    In the project layout you mentioned the following should work:

    log = require "lib.log"

    I.e. replace the directory separator with a dot and also remove the .lua.

  2. Andrew Bibby reporter

    Sorry for the delay, I have several other problems keeping me from a complete build -- but it looks as though the standard love client doesn't like that format (debugging locally using love .). I may have to wrap the require method itself to know for certain.

  3. Alex Szpakowski

    I'm appending lib to the package.path in main.lua

    love.filesystem's require module searcher function doesn't use package.path (package.path is part of a separate module searcher function.) log = require "lib.log" is the correct way to do it and it will work on all versions of LÖVE (i.e. the folder containing main.lua is always the root directory for love.filesystem's module searcher.)

    LÖVE 0.10 will have a new function love.filesystem.setRequirePath which acts much like package.path but for love.filesystem's module searcher, but it's not in LÖVE 0.9.x.

    Modifying package.path might accidentally work for you if your love game is unzipped and your current working directory is the game's root directory, but you can't guarantee those conditions on users' systems, and at that point it's not using love.filesystem to load the files.

  4. Fernando Paredes Garcia

    You can extend require path like this:

    love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";share/?.lua;share/?/init.lua")

    If you have sub-modules in submodules, do this:

    love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";share/?.lua;share/?/init.lua;share/?/?.lua;share/?/?/init.lua")
  5. Log in to comment