Skip to content

Instantly share code, notes, and snippets.

@livexia
livexia / safe_require_nvim.lua
Created May 13, 2024 13:23 — forked from beauwilliams/safe_require_nvim.lua
A simple function to safely require packages. Avoids vim crashing when packages not installed
-- @USAGE:
-- local foo = safe_require('foo')
-- if not foo then return end
_G.safe_require = function(module_name)
local package_exists, module = pcall(require, module_name)
if not package_exists then
vim.defer_fn(function()
vim.schedule(function()
vim.notify('Could not load module: ' .. module_name, 'error', { title = 'Module Not Found' })
end)