Html-lsp server: error and client can't connect [solved]

I have installed the html-lsp server (vscode-html-language-server) and believe I’ve configured it correctly. However when I open a new html file with nvim foo.html, Neovim reports that its LSP client quits with the exit code 1. LspInfo reports that no LSP-server is attached to the server. I’m using Neovim v0.9.5 and the Lazy plugin manager.

Running cat .local/state/nvim/lsp.log gives this at the end of the log file:

[START][2024-01-31 20:58:02] LSP logging initiated
[ERROR][2024-01-31 20:58:02] .../vim/lsp/rpc.lua:734    "rpc"   "vscode-html-language-server"   "stderr"        "/home/paco/.local/share/nvim/mason/packages/html-lsp/node_modules/vscode-langservers-extracted/node_modules/vscode-languageserver/lib/node/main.js:237\n        let counter = counters.get(message) ?? 0;\n                                             ^\n\nSyntaxError: Unexpected token '?'\n    at wrapSafe (internal/modules/cjs/loader.js:915:16)\n    at Module._compile (internal/modules/cjs/loader.js:963:27)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)\n    at Module.load (internal/modules/cjs/loader.js:863:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:708:14)\n    at Module.require (internal/modules/cjs/loader.js:887:19)\n    at require (internal/modules/cjs/helpers.js:74:18)\n    at Object.<anonymous> (/home/paco/.local/share/nvim/mason/packages/html-lsp/node_modules/vscode-langservers-extracted/node_modules/vscode-languageserver/node.js:7:18)\n    at Module._compile (internal/modules/cjs/loader.js:999:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)\n"

This my current config file for LSP:

            lspconfig.html.setup({
                capabilities = capabilities,
                init_options = {
                    configurationSection = { "html", "css", "javascript" },
                    embeddedLanguages = {
                        css = true,
                        javascript = true,
                    },
                    provideFormatter = true,
                },
            })

Personally I would remove the config and see if it fixes it, I have this lsp installed and it works fine without the config, you are using Mason right ?

Yes, I’m using Mason. I don’t understand how it can work without the setup. Just having a LSP-server installed doesn’t mean Neovim will set it up. Yes, I tested removing it but then nothing happens when I open an HTML-file. No client attempt to start the LSP-server. Do you really have no call to lspconfig.html.setup in your config file(s)?

I dont have a config for that lsp specifically, I do have a few plugins that support lsp’s I will look more into it tomorow but yea it all works fine, where did you get the config ?

Ok, a little bit of progress. I tried running the vscode-html-language-server from the command line and it crashes with a syntax error:

$ .local/share/nvim/mason/bin/vscode-html-language-server
/hom/paco/.local/share/nvim/mason/packages/html-lsp/node_modules/vscode-langservers-extracted/node_modules/vscode-languageserver/lib/node/main.js:237
         let counter = counters.get(message) ?? 0;
                                              ^

SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/paco/.local/share/nvim/mason/packages/html-lsp/node_modules/vscode-langservers-extracted/node_modules/vscode-languageserver/node.js:7:18)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

Opening that file, the head is:

"use strict";
/* --------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
/// <reference path="../../typings/thenable.d.ts" />

It seems to be to be a bug in the vscode-html-language-server. I’ve tried to find the source code file at GitHub - microsoft/vscode: Visual Studio Code but have failed. I don’t know Javascipt/Typescript so any pointers would be appreciated.

I’ve also tried uninstalling and then reinstalling vscode-html-language-server, but it still crashes for me.

Ok. Problem solved. I had an old version of node.js on my machine and needed a newer version for vscode-html-language-server to work.

A summary of the problem:

  • The vscode-html-language-server is written in Node.js and uses the Nullish coalescing operator (??) that was implemented in Node.js version 14.
  • I’m on Pop!_OS and it has an old version of Node.js in the default repositories. So does Ubuntu too. In my case I had Node.js 12.22.9, and thus a version that did not have support for the Nullish coalescing operator (??).

The solution:

  • Install nvm (Node Version Manager).
  • Use nvm to install the latest LTS release of Node.js. In my case, and at the writing of this post, it was 20.11.0.

Then vscode-html-language-server works fine.