Lukas
Lukas

Reputation: 103

Lua require relative path

I can't load .Lua-files from relative paths.

This works:

2.lua

function Math( v1, v2 )
 return v1 + v2
end

1.lua

package.path = package.path .. ';C:/Users/Lukas/Desktop/lua/function/?.lua'
require("2")
print(Math(1,6))

This doesn't work:

package.path = package.path .. './function/?.lua;'
require("2")
print(Math(1,6))

Couldn't find any solution for my problem.

Upvotes: 6

Views: 10167

Answers (2)

ToyAuthor X
ToyAuthor X

Reputation: 325

package.path = package.path .. ';function/?.lua'

or

package.path = 'function/?.lua;' .. package.path

Up to you.

Upvotes: 3

lhf
lhf

Reputation: 72312

You're missing a ; to separate the new path from the old one:

package.path = package.path .. ';./function/?.lua;'

require probably showed you this message:

no file './2.lua./function/2.lua'

which should have alerted you to the problem.

Upvotes: 8

Related Questions