Search code examples
luarequireluvit

How can I use require in Lua to access Luvit's built-in modules?


I am running a Luvit environment on a Ubuntu 16.04 Server VPS used to host my project. This is an image of my current file tree

project tree

I am currently inside (via the cd command) the WrapperTest folder, and running the main.lua file. This requires the server file within net/socket.

It feels like Lua is changing the file path every time I switch files. Networking is a MUST in this project, and my files can't even detect built-in modules.

Luvit provides many built-in modules such as coro-http, which provides HTTP support for Lua.

Why is it when I require a different file I cannot detect normal modules and my entire file path changes?

Edit: When I require 'main' in WrapperTest, it successfully requires WrapperTest/net/socket/server. This server file depends on 'discordio.lua' within WrapperTest/net/. 'discordio.lua' requires a file in the same directory called http-lib with the path require "net/http-lib". On http-lib's very first line, it requires a module called coro-http which is built into Luvit's interpreter. This fails and I can't figure out why.

Stack trace (unofficial since it's a Luvit error, not pure Lua):

[string "bundle:deps/require.lua"]:278:
  No such module 'net/discordio' in '/usr/local/WrapperTest/net/socket/server.lua'
  ./net/http-lib.lua:1: module 'coro-http' not found:

Solution

  • http is the 'built-in' library provided by Luvit, mirroring the http library found in Node.

    coro-http is an auxiliary library available for install using the Luvit package manager, lit, from the public package repository.

    $ lit install creationix/coro-http
    

    The lit install command will download and install the library to a local directory called deps.

    Luvit provides a custom require which should include deps (and recursively ascending deps directories) in its search patterns.


    If all else fails, you can try manually adjusting your search paths by changing the package.path field:

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