Is it possible to disable lsp in node_modules directory file?

Hello,

I would like to disable LSP client or just efm langserver when I’m in the file which is inside node_modules directory.

Because of efm and prettier errors related to my config I can’t read anything:

Is it possible? I can provide my lsp/efm config if needed.
Maybe some particular vim BufEnter syntax to emit :LspStop inside node_modules dir file?

Thank you!

1 Like

This might work

autocmd BufRead,BufNewFile */node_modules/* LspStop

Doesn’t seem to be working. Maybe It depends on where I put it in my config (The sequence in which scripts are sourced).

I would rather suggest this

autocmd BufRead,BufNewFile */node_modules/* lua vim.diagnostic.disable(0)

0, as you might expect, would disable diagnostics for the buffer. LspStop will make LSP stop even for non-node_modules’ files.

1 Like

I had this same problem, and I think I’ve worked it out, assuming you’re using nvim-lspconfig. I’m new to Lua, so it’s possible I’m holding a footgun here WRT lspconfig’s lazy-loading system, but it seems to work.

From :h lspconfig-setup:

- {root_dir}

    `function(filename, bufnr)`

    Returns either a filepath (string) or nil. The language server will only
    start if the function returns a filepath.

So if we return nil from the root_dir function, it won’t attach the language server to the buffer in the first place.

So, this is my ESLint setup:

require("lspconfig").eslint.setup {
	-- […]
	root_dir = function(filename, bufnr)
		if string.find(filename, "node_modules/") then
			return nil
		end
		return require("lspconfig.server_configurations.eslint").default_config.root_dir(filename, bufnr)
	end
}

I just added an early return to the default root_dir function if the file is inside a node_modules folder.

1 Like

Hi, maybe a late reply, but this is the proper solution that I found:

lua/lsp/eslint.lua:

require("lspconfig").eslint.setup {
	codeAction = {
		disableRuleComment = {
			enable = true,
			location = "separateLine",
		},
		showDocumentation = {
			enable = true,
		},
	},
	codeActionOnSave = {
		enable = false,
		mode = "all",
	},
	experimental = {
		useFlatConfig = false,
	},
	format = true,
	nodePath = "",
	onIgnoredFiles = "off",
	packageManager = "npm",
	problems = {
		shortenToSingleLine = false,
	},
	quiet = false,
	rulesCustomizations = {},
	run = "onType",
	useESLintClass = false,
	validate = "on",
	workingDirectory = {
		mode = "location",
	},
}

/lua/autocommands.lua:

local ag = vim.api.nvim_create_augroup
local au = vim.api.nvim_create_autocmd

-- GROUPS:
local disable_node_modules_eslint_group =
	ag("DisableNodeModulesEslint", { clear = true })

-- AUTO-COMMANDS:
au({ "BufNewFile", "BufRead" }, {
	pattern = { "**/node_modules/**", "node_modules", "/node_modules/*" },
	callback = function()
		vim.diagnostic.disable(0)
	end,
	group = disable_node_modules_eslint_group,
})

I added this as a reply to the same issue that I was having with eslint.nvim: I get diagnostics on `.node_modules` files · Issue #4 · MunifTanjim/eslint.nvim · GitHub

Just chiming in with my two cents here. @ahmedelgabri his suggestion works if you add a timeout, like so:

autocmd({'BufRead', 'BufNewFile'}, {
  pattern = '*/templates/*',
  callback = function()
    vim.defer_fn(function()
      vim.cmd 'LspStop'
    end, 1000)
  end
})

1000 milliseconds seems to be sufficient for my use case, but YMMV.

1 Like