luvit/luvit

Requiring within a require disables luvit's require system

Hedwig7s opened this issue · 4 comments

Trying to make my project modular to make it easier to work with and turns out requiring within a require disables luvit require even with ./
Error:

No error requiring from main
No error requiring from script1
Uncaught exception:
.\\/scripts/script2.lua:2: module 'fs' not found

main.lua:

require('fs')
print("No error requiring from main")
require('./scripts/script1')()

scripts/script1.lua:

return function ()
    require('fs')
    print('No error requiring from script1')
    require('./scripts/script2')()
end

scripts/script2.lua:

return function ()
    require('fs')
    print("No error requiring from script2")
end

Sounds like a duplicate of #857

Sounds like a duplicate of #857

The difference is the solution I found was to use ./ or .\ at the start of the require but that only works 1 require down

The goal is to not let your Luvit's require fallback to Lua's, do whatever this requires. So whether it is prefixing the path with ..//./, or it is having your dependencies in a libs or a deps folder and then requiring it with the name only. Like so:

./
├── libs
│   └── mod.lua
└── main.lua

main.lua:

require('mod')

It appears to me that Luvit is falling back to Lua's with require('./scripts/script2'); the reason for that is when you require a file, your working directory (for that file) will change to it. So in your example to make it work, your script1.lua should use ./script2 instead of ./scripts/script2.

You can tell if the require you are using is Lua's or Luvit's by comparing it to _G.require, in your example for example print(_G.require == require) prints true, indicating that it is in fact falling back to Lua's require.

Overall you seem to want a libs folder instead, it is easier to deal with that instead of having to remember your paths. Hope this clarifies it.

Welp guess I gotta rewrite my entire project because of the stupid require system