Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua require not working

Tags:

lua

I am trying to make one lua file require another. I am following this guide: http://lua-users.org/wiki/ModulesTutorial

My basic test, which should be a trivial hello world, does not work, and I can't figure out why.

Here's a console log which shows all files and all errors:

C:\Users\TestUser\Desktop\LuaTest>dir
 Volume in drive C has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Users\TestUser\Desktop\LuaTest

11/15/2017  03:03 PM    <DIR>          .
11/15/2017  03:03 PM    <DIR>          ..
11/15/2017  02:53 PM    <DIR>          Bar
11/15/2017  03:04 PM                92 BazModule.lua
11/15/2017  02:53 PM    <DIR>          Foo
11/15/2017  03:08 PM               139 main.lua
               2 File(s)            231 bytes
               4 Dir(s)  253,774,073,856 bytes free

C:\Users\TestUser\Desktop\LuaTest>lua main.lua
lua: main.lua:1: module 'BazModule' not found:
        no field package.preload['BazModule']
        no file 'C:\dev\LuaDist\bin'
        no file '.\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
stack traceback:
        [C]: in function 'require'
        main.lua:1: in main chunk
        [C]: ?

C:\Users\TestUser\Desktop\LuaTest>type main.lua
local baz = require("BazModule")
baz.Baz()

local bar = require("Bar.BarModule")
bar.Bar()

local foo = require("Foo.FooModule")
foo.Foo()

C:\Users\TestUser\Desktop\LuaTest>type BazModule.lua
local BazModule = {}

function BazModule.Baz()
    print("Hello Baz!")
end

return BazModule

C:\Users\TestUser\Desktop\LuaTest>lua -v
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio

The expected output should be

Hello Baz!
Hello Bar!
Hello Foo!

But it can't find any of the files adjacent to main.lua and I don't understand why.

like image 458
Paul Gilmore Avatar asked Nov 15 '17 23:11

Paul Gilmore


People also ask

How does require work in Lua?

Lua offers a higher-level function to load and run libraries, called require . Roughly, require does the same job as dofile , but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work.

What is LUA_PATH?

Lua modules and packages - what you need to know As with most search paths, the LUA_PATH is actually a semicolon-separated collection of filesystem paths. Lua scans them in the order they are listed to find a module. If the module exists in multiple paths, the module found first wins and Lua stops searching.

Where does Lua look for modules?

And Lua will now search for modules in the lib directory (in addition to where it usually does). You can also use the LUA_PATH environment variable to do this before even starting Lua.

What is a Lua module?

A module in the Lua programming language is a piece of code that contains functions and variables: it's an user library. It's a powerful way to split your code in several files. A module is loaded using the Lua require keyword.


1 Answers

require searches in directories listed in package.path (for Lua files) and package.cpath (for compiled libraries).

Your error message…

lua: main.lua:1: module 'BazModule' not found:
        no field package.preload['BazModule']
        no file 'C:\dev\LuaDist\bin'
        no file '.\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
        no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'

indicates the paths that require searched in. It seems that package.path is completely empty, or maybe there's a single malformed path pattern in there. (Which would be C:\dev\LuaDist\bin.)

The way the search for a module foo.bar works is that ? is substituted by foo/bar (or foo\bar – depending on OS) and so ./?.lua would find ./foo/bar.lua.

So the way to fix this is to (a) fix the place where you (or something that you installed) are/is mangling the package.path (via environment variable, startup script, …?) and/or (b) add the current directory to the search path.

like image 74
nobody Avatar answered Nov 17 '22 23:11

nobody