Search code examples
luaneovim

How to import table from a lua file to another lua file in Neovim?


I want to import a table from a lua file (custom_header.lua) containing ascii art to use as custom headers in dashboard-nvim plugin. In init.lua file I have this following code:

local db = require('dashboard')
local header_import = require('custom_header')
local imported_header_list = header_import.custom_header_list()
db.custom_header = imported_header_list[1]

In custom_header.lua file:

logo_list = {}
function logo_list.custom_header_list()
  header_choose = 
  {
     {
      '.................',
      '..... Art 1 ...',
     },
    {
     '.............',
      '...art 2 ...',
    }
  }
  return header_choose
end
return logo_list

Both files are in same directory. But when I'm opening neovim it's throwing me error saying:

module 'custom_header' not found

Folder structure is following:

.config/nvim/lua/dash-config/(init.lua, custom_header.lua)

All the other lua files are sourced at a init.lua file that is in the lua folder.

What I'm doing wrong here ?


Solution

  • Depending on how neovim configures Lua, you should be able to require subpackages like so:

    local header_import = require('dash-config.custom_header')
    

    or like so:

    local header_import = require('dash-config/custom_header')