How to add directories to search for modules in Lua?

Asked

Viewed 174 times

1

I’m having a problem with require function, I want her to look for a moon file, but I don’t want to specify the folder, and my moon is installed on another disk, I’ll put a photo to see the errorinserir a descrição da imagem aqui

As you can see the highlighted part is the file I want to use, is in the lib folder in the corner, I gave a C cd: in the terminal to test some things and I saw that this giving error, I think I just have to specify the disc, however i do not want to specify the folder because on someone else’s computer will not work, why the folder will have different name, then how can I do?

  • You should not specify only the relative path of the file, in case Lib\\BibliotecaPadrao?

  • yes and did it, however it gives the same error q i think q eh prq just look in the same drive of the moon installed, and eh why I asked if it has how to say I want q look in the two drives or only in C:\

1 answer

1

Lua searches the modules using the variable package.path which in turn is started by reading a system variable called LUA_PATH.

To add a new place to search for modules just change one of these variables.

If you want the folder to be always available you will want to edit the LUA_PATH.

In Windows vc should go to System | Advanced System Settings | Environment Variables and add the LUA_PATH or edit if it already exists. In Linux you will put this setup in your .bashrc.

Ex:

LUA_PATH = ;;D:\libs\lua\?.lua

The ;; at the beginning makes the moon load the default libs at the beginning of the path. Note that you have a query ? in Pattern. It serves to mark the place where you will enter what you type in require.

Ex: Consider this directory structure:

D:
|- libs
    |- lua
        |- math
            |- linearAlgebra.lua

With the LUA_PATH defined as above you can do so in your moon code:

local la = require "math.linearAlgebra"
local v = la.dot({1, 0}, {1, 2})
...

The convention is to use a dot . where there would be a directory separator (so math.linearAlgebra and not math\\linearAlgebra)

And also note that the .lua is not specified as it has already been defined in LUA_PATH.

If you want to add more search patterns, just separate them with ;.

And finally it’s important to know that all of this also holds true for the package.path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.