Skip to content

Volar LSP: "Can't find typescript.js or tsserverlibrary.js" #3742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
albnavarro opened this issue Apr 17, 2025 · 3 comments
Open

Volar LSP: "Can't find typescript.js or tsserverlibrary.js" #3742

albnavarro opened this issue Apr 17, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@albnavarro
Copy link

Description

After changing the import to fly from
require("lspconfig").volar.setup({}) to vim.lsp.enable("volar")
I get the following error both using vtsls and using ts_ls:

Error executing vim.schedule lua callback: ...im.aPBmfPd/usr/share/nvim/runtime/lua/vim/lsp/client.lua:548: RPC[Error] code_name = InternalError, message = "Request initialize failed with message: Can't find typ
escript.js or tsserverlibrary.js in \"/home/<name>/Site/vue-test/node_modules/typescript/lib\""                                                                                                            
stack traceback:                                                                                                                                                                                                   
        [C]: in function 'assert'                                                                                                                                                                                  
        ...im.aPBmfPd/usr/share/nvim/runtime/lua/vim/lsp/client.lua:548: in function ''                                                                                                                            
        vim/_editor.lua: in function <vim/_editor.lua:0>

After seeing the error when opening the fisrt file everything works correctly (go-to-definition, reference, import component).

Inside the folder after/lsp vtsls this content returns:

return {
    filetypes = { "typescript", "javascript", "vue" },
    settings = {
        javascript = {
            preferences = {
                -- importModuleSpecifier = "non-relative",
            },
            inlayHints = {
                functionLikeReturnTypes = { enabled = true },
                parameterNames = { enabled = "all" },
                variableTypes = { enabled = true },
            },
        },
        typescript = {
            preferences = {
                -- importModuleSpecifier = "non-relative",
            },
        },
        vtsls = {
            autoUseWorkspaceTsdk = true,
            experimental = {
                -- Inlay hint truncation.
                maxInlayHintLength = 30,
                -- For completion performance.
                completion = {
                    enableServerSideFuzzyMatch = true,
                },
            },
            tsserver = {
                globalPlugins = {
                    {
                        name = "@vue/typescript-plugin",
                        -- location = volar_path,
                        location = require("mason-registry").get_package("vue-language-server"):get_install_path()
                            .. "/node_modules/@vue/language-server",
                        languages = { "vue" },
                        configNamespace = "typescript",
                        enableForWorkspaceTypeScriptVersions = true,
                    },
                },
            },
        },
    },
}
@albnavarro albnavarro added the bug Something isn't working label Apr 17, 2025
@albnavarro
Copy link
Author

albnavarro commented Apr 17, 2025

edit:

As per doc volar in this case only handles html and css, so it is normal that the js/ts part works.

On nuxt everything works ok because typescript is installed in the default project, and is therefore in node-modules.

The error occurs when typescript is not installed in the project. An installation of vue.js without typescript therefore now generates an error.

This condition was solved in the previous configuration I guess using the typescript package installed with mason or globally if the package is not found in node-module.

An installation made with vite of vue javascript does not install typescript.

@justinmk justinmk changed the title Volar LSP Volar LSP: "Can't find typescript.js or tsserverlibrary.js" Apr 18, 2025
@justinmk
Copy link
Member

The error occurs when typescript is not installed in the project. An installation of vue.js without typescript therefore now generates an error.

What do you expect from nvim-lspconfig to resolve this?

Are the docs not clear enough? I noticied that a lot of the ts-related configs have these big complex docs:

--- **Overriding the default TypeScript Server used by Volar**
---
--- The default config looks for TypeScript in the local `node_modules`. This can lead to issues
--- e.g. when working on a [monorepo](https://monorepo.tools/). The alternatives are:
---
--- - use a global TypeScript Server installation
--- ```lua
--- vim.lsp.config('volar', {
--- init_options = {

@albnavarro
Copy link
Author

albnavarro commented Apr 30, 2025

Sorry, you're right, I just wanted to point out the differences between the old require("lspconfig") and the new vim.lsp.enable.
I personally have already solved the problem but my solution is quite personal and I don't think it's possible to abstract it on this repository.
I opened this issue because maybe it could be useful to understand what changed from the old to the new config.

For those who may encounter this problem I can expose my solution (maybe something better can be done).
( For convenience I use small utilities that are equivalent to the Higher Order Functions of javascript, in this case map and filter)

local U = require("utils/tables_utils")

-- local typescript
local function get_local_typescript_server_path(root_dir)
    local project_root = vim.fs.dirname(vim.fs.find("node_modules", { path = root_dir, upward = true })[1])
    return project_root and vim.fs.joinpath(project_root, "node_modules", "typescript", "lib") or ""
end

-- typescript lib in order of priority
local pathsTable = {
    {
        package = "vtsls",
        path = "/node_modules/@vtsls/language-server/node_modules/typescript/lib",
    },
    {
        package = "typescript-language-server",
        path = "/node_modules/typescript/lib/",
    },
    {
        package = "vue-language-server",
        path = "/node_modules/typescript/lib",
    },
}

-- get table with lib paths
local paths = U.map(pathsTable, function(item)
    return require("mason-registry").get_package(item.package):get_install_path() .. item.path
end)

return {
    before_init = function(_, config)
        if config.init_options and config.init_options.typescript and config.init_options.typescript.tsdk == "" then
            -- add local lib in first tposition
            table.insert(paths, 1, get_local_typescript_server_path(config.root_dir))

            -- print(vim.inspect(paths))

            -- Find first valid path in priority order.
            local firstValidPath = U.find(paths, function(path)
                return vim.fn.isdirectory(path) ~= 0
            end)

            -- print(firstValidPath)

            config.init_options.typescript.tsdk = firstValidPath
        end
    end,
}

@justinmk justinmk added enhancement New feature or request and removed bug Something isn't working labels Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants