6

I'm new to neovim (or vim) settings and I was trying to require '.lua' file but failed.

This is my directory structure.

~/.config/nvim
 ├ init.vim
 ├ /lua
   ├ basic.lua

and these are my codes.

~/.config/nvim/init.vim

lua require('basic')

~/.config/nvim/lua/basic.lua

print('hello from basic.lua')

and error logs (My system is M2 MacOS)

E5108: Error executing lua [string ":lua"]:1: module 'basic' not found:
        no field package.preload['basic']
        no file './basic.lua'
        no file '/opt/homebrew/share/luajit-2.1.0-beta3/basic.lua'
        no file '/usr/local/share/lua/5.1/basic.lua'
        no file '/usr/local/share/lua/5.1/basic/init.lua'
        no file '/opt/homebrew/share/lua/5.1/basic.lua'
        no file '/opt/homebrew/share/lua/5.1/basic/init.lua'
        no file './basic.so'
        no file '/usr/local/lib/lua/5.1/basic.so'
        no file '/opt/homebrew/lib/lua/5.1/basic.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        [string ":lua"]:1: in main chunk

I found that I can requiring like require('lua.basic') runs okay. So I'm assuming that '~/.config/nvim' is contained in runtimepath.

But how can I check my lua runtimepath and modify it?

romainl
  • 186,200
  • 21
  • 280
  • 313
hyunsik lim
  • 63
  • 1
  • 3

4 Answers4

8

You can get your current runtimpath via Neovim API with nvim_list_runtime_paths() function :

:lua print(vim.inspect(vim.api.nvim_list_runtime_paths()))

lcheylus
  • 1,217
  • 8
  • 21
  • 1
    Thanks so much. I've checked current runtimepath and added this line to my init.vim and it worked fine. `let &runtimepath.=', "~/.config/nvim/lua"'` – hyunsik lim Aug 15 '22 at 08:21
  • 1
    This doesn't seem to be the full runtime path, my neovim is loading lua modules from a folder that is not present in `vim.api.nvim_list_runtime_paths()`. – UltraMaster Mar 10 '23 at 14:49
  • 2
    If it helps anyone, I found out that `lazy.nvim` changed the `runtimepath` with a slight delay. So my init script was able to import the module from the "default" `runtimepath` and shortly after `lazy.nvim` changed `runtimpath` but the pesky module has already been loaded. – UltraMaster Mar 10 '23 at 15:50
4

I had the same problem and I think the following fixed it: I removed the init.vim and am now using an init.lua with the following content:

vim.api.nvim_command('set runtimepath^=~/.vim')
vim.api.nvim_command('let &packpath = &runtimepath')
vim.api.nvim_command('source ~/.vim/vimrc')

Suddenly, require works as expected!

Sergio
  • 41
  • 2
0

I think something must be messed up. Sourcing lua files from your config directory should just work. Unfortunately, I'm not sure how to debug that.

But here is a way you could set your rtp using lua

lua vim.opt.runtimepath:append(',~/.config/nvim/lua')
DieOde
  • 61
  • 4
0

you can add vim.o.path = vim.o.path .. './lua' in lua file

abliger
  • 1
  • 1