Search code examples
lualua-5.3

Why lua require won't search current directory?


say I've two files test.lua and m.lua in a folder, in test.lua as:

require("m")

then I run this file, howerver it raise an error:

lua: /Users/xx/works/scripts/test.lua:43: module 'm' not found:
    no field package.preload['m']
    no file '/usr/local/share/lua/5.3/m.lua'
    no file '/usr/local/share/lua/5.3/m/init.lua'
    no file '/usr/local/lib/lua/5.3/m.lua'
    no file '/usr/local/lib/lua/5.3/m/init.lua'
    no file './m.lua'
    no file './m/init.lua'
    no file '/usr/local/lib/lua/5.3/m.so'
    no file '/usr/local/lib/lua/5.3/loadall.so'
    no file './m.so'

As you can see the line no file './m.lua' appears but this is not true. ./m.lua exists, and file permission stuff is OK. If I hardcode the path:

package.path = package.path..';'..'/Users/xx/works/scripts/?.lua'
require('m')

It will work as expected.

What should I do to make lua search current directory fisrt (like python's import) when require a module


Solution

  • The current directory is the directory where you launch lua from.

    The command line is missing in your example, if you used lua test.lua then it shoud work, if you used lua works/scripts/test.lua then it will not work.