## Show JavaScript errors in Neovim on macOS

Written by strager on 2022-02-16

Neovim with quick-lint-js running on macOS

Neovim is one of the most popular code editors, and JavaScript is one of the most popular programming languages. Let's look at some plugins to catch JavaScript bugs immediately in Neovim.

1. Install Homebrew
2. Install Neovim
3. Install nvim-lspconfig
4. Install quick-lint-js
5. Customize & configure
6. Troubleshooting

### 1. Install Homebrew

We are going to use the Homebrew package manager to install most of our tools. Installing Homebrew is simple: open Terminal.app (or iTerm 2 or any other terminal program) and paste the following command:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Homebrew

### 2. Install Neovim

Use Homebrew to install Neovim by running the following command in a terminal:

    brew install neovim

Install Neovim using Homebrew

After installing Neovim, tell Neovim to use plugins installed through Homebrew by adding the following line to your `~/.config/nvim/init.lua` file:

    -- Replace /opt/homebrew with the path
    -- printed by the brew --prefix command.
    vim.cmd [[set runtimepath+=/opt/homebrew/share/nvim/site]]

Configure Neovim to use Homebrew-installed plugins

### 3. Install nvim-lspconfig

Next, we need to install the nvim-lspconfig Neovim plugin. This plugin lets you configure LSP servers, such as the JavaScript tool we're going to install later, in Neovim.

LSP servers provide diagnostics, completion, go-to-definition, and other features.

If you already use a Neovim plugin manager, install nvim-lspconfig using that plugin manager. Otherwise, run the following two commands in a terminal to install the plugin:

    mkdir -p ~/.local/share/nvim/site/start
    curl --location 'https://github.com/neovim/nvim-lspconfig/archive/refs/heads/master.tar.gz' | tar xz -C ~/.local/share/nvim/site/start

Install nvim-lspconfig

### 4. Install quick-lint-js

We're almost done! We now need to install quick-lint-js, which is the software which finds the errors in your JavaScript code. Install it using Homebrew by running the following commands in a terminal:

    brew tap quick-lint/quick-lint-js https://github.com/quick-lint/quick-lint-js.git
    brew install quick-lint-js

Install quick-lint-js

Then, add the following to your `~/.config/nvim/init.lua` file:

    require('lspconfig/quick_lint_js').setup {}

Enable quick-lint-js in Neovim

Restart Neovim and open a `.js` file. Type something, and you should see a warning:

Neovim with quick-lint-js running on macOS

If something doesn't work, see the troubleshooting steps below.

### 5. Customize & configure 🔧

- Detect errors while you type
- Hide `W`/`E` sign in left gutter
- Hide “■ error message” virtual text

#### Detect errors while you type

By default, nvim-lspconfig only shows you errors while in normal mode. To show errors while you type in insert mode, add the following to your `~/.config/nvim/init.lua` file:

    vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with(
      vim.lsp.diagnostic.on_publish_diagnostics, {
        update_in_insert = true,
      }
    )

Show errors while you type

#### Hide `W`/`E` sign in left gutter

By default, Neovim shows a symbol in the gutter to indicate an error. If there is no error, the gutter is hidden. If you want the gutter to always be hidden, add the following line to your `~/.config/nvim/init.lua` file:

    vim.diagnostic.config({signs = false})

Before (signs enabled)
After (signs disabled)
Hide diagnostic signs in gutter

#### Hide “■ error message” virtual text

By default, Neovim shows virtual text to the right of lines containing errors. If there is no error, the virtual text is hidden. If you want the error message virtual text to always be hidden, add the following line to your `~/.config/nvim/init.lua` file:

    vim.diagnostic.config({virtual_text = false})

Before (virtual text enabled)
After (virtual text disabled)
Hide error message virtual text

### 6. Troubleshooting 🐞

#### “Spawning language server with cmd: `quick-lint-js` failed. The language server is either not installed, missing from PATH, or not executable.”

nvim-lspconfig gives this error if quick-lint-js was installed but isn't in `$PATH`.

Possible cause: Homebrew's directory was not added to `$PATH` during installation.
  Possible solution: Open a new terminal to reset `$PATH`.
  Possible solution: Run the following command to add Homebrew's directory to `$PATH`:

  >     eval "\$(${HOMEBREW_PREFIX}/bin/brew shellenv)"

Possible cause: quick-lint-js is not installed.
  Possible solution: Install quick-lint-js.

#### “E5113: Error while calling lua chunk: …/.config/nvim/init.lua: module 'lspconfig/quick_lint_js' not found”

Neovim gives this error if it can't find quick-lint-js' nvim-lspconfig configuration.

Possible cause: quick-lint-js is not installed.
  Possible solution: Install quick-lint-js.
Possible cause: Neovim's `runtimepath` does not list Homebrew's `share` directory.
  Possible solution: In `init.lua`, add Homebrew's `share` directory to `runtimepath`.
  Possible solution: In `init.lua`, place the `set runtimepath` command above the `require('lspconfig/quick_lint_js')` command.

If you have any corrections or need any help, please email me at strager.nds@gmail.com.