"require" in higher directories

Asked

Viewed 125 times

3

I have a program in Lua. This is the file structure:

|Programa
|-Main.lua
|-config.lua
|--------functions
                  |-functions.lua

Through the functions.lua, I want to give a require in the config.lua. Is there any way?

I’ve tried to:

require "../config"
require "..\\config"

1 answer

1


Before the require you must set the path package, tell in which files you should search for the module:

package.path = package.path .. ";../config.lua"
require "config"

Note that the usage is similar to what is done with path of operating systems. You add a new path to what already exists so as not to lose others.

  • It didn’t work, now the errors also show: no file '.. /luafunctions.lua'

  • And what appeared before?

  • Same message(in file), only without this directory.

  • try like this: package.path = package.path .. ";../?.lua". Prints the package.path and see if it’s being formed correctly.

  • Returned: . /?. lua;/usr/local/share/lua/5.1/?. lua;/usr/local/share/lua/5.1/? /init.lua;/usr/local/lib/lua/5.1/?. lua;/usr/local/lib/lua/5.1/? /init.lua;/usr/share/lua/5.1/?. lua;/usr/share/lua/5.1/? /init.lua;.. /?. lua

  • Yeah, I have no idea, in my short experience I’ve never seen it happen.

Show 1 more comment

Browser other questions tagged

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