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?

4 Answers 4

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()))

3
  • 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"' Aug 15, 2022 at 8: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(). Mar 10 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. Mar 10 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!

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')
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.