How to add directories to module search in Lua?

I'm having a problem with the require function, I want it to look for a lua file, but I don't want to specify the folder, and my lua is installed on another disk, I'll put a photo to see the errorinsert the description of the image here

As you can see the highlighted part eh the file I want to use, is in the folder lib there in the corner, I gave a CD C:\ in the terminal to test some things and saw that this giving this error, I think I just have to specify the disk, but I do not want specify the folder because on someone else's computer will not work, why the folder has a different name, so how can I do?

Author: Eric Chiesse, 2019-07-10

1 answers

Lua searches for 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 the module search, you simply 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. No Linux you will put this setup on your .bashrc.

Ex:

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

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

Ex: consider this directory structure:

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

With LUA_PATH defined as above you can do this in your lua 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 .lua is not specified because it has already been defined in LUA_PATH.

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

And finally it is important to know that all this also applies to package.path.

 1
Author: Eric Chiesse, 2019-11-20 23:33:58