5

I am writing a Lua script and am trying to use require on a file that exists in the same directory as the main script. I cannot seem to get require to work in this case and have tried several solutions I have found but none seem to work. I have the following files together in a directory:

main.lua
helper.lua

I've tried the following solutions and gotten the error following each:

Solution 1:

local folderOfThisFile = (...):match("(.-)[^%.]+$") 
local helper = require(folderOfThisFile .. 'helper')

lua: ...domizerWPF\DataFiles\LUA\main.lua:2: attempt to index local 'pathOfThisFile' (a nil value)
stack traceback:
    ...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
    [C]: ?

Solution 2:

package.path = "/?.lua;" .. package.path 
local helper = require('helper')

lua: ...domizerWPF\DataFiles\LUA\main.lua:2: module 'helper' not found:
    no field package.preload['helper']
    no file '/helper.lua'
    no file '.\helper.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\helper\init.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\helper.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\helper\init.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\helper.luac'
    no file '.\helper.dll'
    no file '.\helper51.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\helper.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\helper51.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\clibs\helper51.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\loadall.dll'
    no file 'C:\Program Files (x86)\Lua\5.1\clibs\loadall.dll'
stack traceback:
    [C]: in function 'require'
    ...domizerWPF\DataFiles\LUA\main.lua:2: in main chunk
    [C]: ?

I've tried variations on Solution 2 with various paths such as "?.lua;" and "./?.lua;" to no avail.

Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
Lance
  • 611
  • 6
  • 28
  • What is the current working directory for your process? – Alexander Gladysh Dec 25 '14 at 09:28
  • Do you mean of the lua exe or of the script? The lua exe is in program files (x86). The script is in a separate folder like so: c:\lua files\. – Lance Dec 25 '14 at 11:39
  • The `lua.exe` — require looks for files relative to the CWD, as far as I remember. – Alexander Gladysh Dec 25 '14 at 11:50
  • If you run it from command line, be sure to cd into working directory. If it is a shortcut, set workdir in its properties. – user3125367 Dec 25 '14 at 12:10
  • While I can set the working directory, it will be run from a .net application in the end so I need to be able to have it look in the directory that the script is located in. – Lance Dec 25 '14 at 12:26
  • 2
    So just add path with script to package.path before run it. – moteus Dec 25 '14 at 13:58
  • I'm using moteus' method. Until I get to that point (for standalone testing) I am using CWD. – Lance Dec 27 '14 at 14:11
  • Does this answer your question? [Is there a better way to require file from relative path in lua](https://stackoverflow.com/questions/5761229/is-there-a-better-way-to-require-file-from-relative-path-in-lua) – midrare May 17 '22 at 22:23

3 Answers3

2

These two lines of the error message shed some light on your problem:

no file '/helper.lua'
no file '.\helper.lua'

The first line is due to your change to package.path. As you can see, it looks for a "/helper.lua" file that doesn't exist so its not doing anything. The second line is due to the default package.path and is looking for a "helper.lua" in the current working directory. Since its not finding, your current working directory must not be the directory your main.lua is located on.

The fix is to either make the current working directory the directory where main.lua and helper.lua are located or to add "C:\\path\\to\\your\\lua\\project\\?.lua" to the package.path

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • Workdir issues are unrelated to the code and lie entirely on the host side (lua.exe or it's starter or embedding host app). What if script path changes? Correct ways are: a) cd before run, b) use specific loader (if embedded). – user3125367 Dec 26 '14 at 13:25
0

i am still learning lua but here is what I can whip up for you, if you don't have a file system API installed then you can make a string variable with your curent working dir in it and you can add to it like this

local cwd="C:\users\user\Desktop\"
dofile(cwd.."program.lua")

thats what I do, and I have no problems with it

joshua chris
  • 55
  • 12
  • 1
    That doesn’t look at all practical. If you move the scripts to another directory, or another machine, or another user on the same machine, it will break. That doesn’t solve the question because the path becomes absolute, not relative. – user137369 Feb 21 '20 at 21:54
0

If you mean that you want to be able to invoke the program from any directory and that it correctly locates the required files, then you can use this solution (you only need it within main.lua):

local base_path = string.match(arg[0], '^(.-)[^/\\]*$')
package.path = string.format("%s;%s?.lua", package.path, base_path)

This works by adding the directory where the file is to the package path, so that require can work on the files in that directory. Lua doesn't do this automatically yet (Python does, since version 2.6 or so); hopefully it will be implemented in future. You can also use base_path to refer to other files in the same directory. In my case, for example, there is a SQLite database in that directory and the program needs to open it, so I use this:

local database_filename = base_path .. 'db.sqlite'

You can also make base_path a global so that it's available to other modules if necessary.

Pedro Gimeno
  • 2,837
  • 1
  • 25
  • 33