Require is not working properly

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Require is not working properly

Post by norubal »

I'm trying to use library called 'Slab', and I have a directory structure like this:

Code: Select all

myproject/
    |- main.lua
    |- conf.lua
    |- ...
    └lib/
        └slab/
            ├const.lua
            ├API.lua
            ├Slab.lua
            ├...
            └init.lua
And this is my main.lua:

Code: Select all

local Slab = require 'lib/slab'

function love.load(args)
	Slab.DisableDocks({'Left', 'Right', 'Bottom'})
	-- love.graphics.setBackgroundColor(0.4, 0.88, 1.0)
	Slab.Initialize(args)
end

function love.update(dt)
	Slab.Update(dt)
  
	Slab.BeginWindow('MyFirstWindow', {Title = "My First Window", AllowMove = false, ShowMinimize = false})
	Slab.Text("Welcome.")
	Slab.EndWindow()
end

function love.draw()
	Slab.Draw()
end
When I try to run my project, I have this error:

Code: Select all

Error

lib/slab/init.lua:28: module 'const' not found:
no field package.preload['const']
no 'const' in LOVE game directories.
no file 'const' in LOVE paths.
no file '.\const.lua'
no file 'C:\Program Files\LOVE\lua\const.lua'
no file 'C:\Program Files\LOVE\lua\const\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\const.luac'
no file '.\const.dll'
no file 'C:\Program Files\LOVE\const.dll'
no file 'C:\Program Files\LOVE\loadall.dll'


Traceback

[love "callbacks.lua"]:228: in function 'handler'
[C]: in function 'require'
lib/slab/init.lua:28: in main chunk
[C]: in function 'require'
main.lua:1: in main chunk
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'
It seems /lib/slab/const.lua cannot be loaded by /lib/slab/init.lua, so I think I'm doing something wrong on the first line of main.lua.
How can I use 'require' properly in this case?
Attachments
myproject.love
(251.88 KiB) Downloaded 16 times
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: Require is not working properly

Post by darkfrei »

I am not sure, but it looks like that you need to have file const.lua in the root of your project and define here the path to your slab lib:

Code: Select all

 -- lib/slab/init.lua:
local Const = require("const")
local Slab = require(Const.SLAB_PATH .. ".API")

return Slab
The file const.lua:

Code: Select all

-- const.lua (root)
local const = {
	SLAB_PATH = 'lib.slab'
}
return const
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
norubal
Party member
Posts: 137
Joined: Tue Jan 15, 2013 5:55 am

Re: Require is not working properly

Post by norubal »

darkfrei wrote: Sun Jul 09, 2023 12:17 pm I am not sure, but it looks like that you need to have file const.lua in the root of your project and define here the path to your slab lib:

Code: Select all

 -- lib/slab/init.lua:
local Const = require("const")
local Slab = require(Const.SLAB_PATH .. ".API")

return Slab
The file const.lua:

Code: Select all

-- const.lua (root)
local const = {
	SLAB_PATH = 'lib.slab'
}
return const
Thank you. I contacted to owner of the library and I think the issue would be resolved soon.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Require is not working properly

Post by yetneverdone »

Hi, maintainer of Slab here, sorry for the broken commit. It should be fixed now :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 63 guests