question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find module '$lib/...' or '$app/...' from language server for .svelte files in default configuration of sveltekit

See original GitHub issue

Describe the bug

In a fresh install of sveltekit with demo app and typescript, LSP works fine out of the box except for imports from the $ aliases in .svelte files.

These throw a diagnostic such as Cannot find module '$lib/Counter.svelte' or its corresponding type declarations and jump to definition does not work for those imported symbols.

These diagnostics disappear if I replace $lib or $app with absolute paths and jump to definition starts working.

These diagnostics DO NOT happen in .ts files, where jump to definition works fine for symbols imported from $app, for example (see src/lib/form.ts in the demo app).

Reproduction

I’m using a fresh install of Node 16.14.2 LTS straight from the node main page tarball. Fresh global installs of svelte-language-server, typescript-language-server and typescript. Using the Helix editor which has a native LSP client, using the default config that automatically finds svelteserver for .svelte files and typescript-language-server for .js and .ts files.

I set up a new sveltekit project as per instructions on sveltekit main page answering yes for typescript and demo app and no for everything else. Cd to app dir, npm install, npm run dev -- --open. No changes made to anything.

Edit a .svelte file from the app root dir: hx src/routes/index.svelte

As soon as the language server loads, LSP features start working fine but it throws a diagnostic for the line import Counter from '$lib/Counter.svelte'; that reads: Cannot find module '$lib/Counter.svelte' or its corresponding type declarations and jump to definition does not work for that symbol (works fine otherwise for everything else in the same file).

The about.svelte page does the same thing for import { browser, dev } from '$app/env'; .

Expected behaviour

No diagnostic, jump to definition works.

System Info

  • OS: Debian 11
  • IDE: Helix

Which package is the issue about?

language-server

Additional Information, eg. Screenshots

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
dustinclevelandcommented, Aug 6, 2022

I encountered this issue early on in an empty svelte-kit app. npm run prepare just told me that I had a missing script. Instead, I just needed to manually add the following to .svelte-kit/tsconfig.json:

"paths": {
  "$lib": [
      "src/lib"
  ],
  "$lib/*": [
      "src/lib/*"
  ]
},
4reactions
ngouldcommented, Aug 7, 2022

I got to the same answer as @dustincleveland, although worth clarifying that "paths" needs to be under compilerOptions. E.g.,

{
  "compilerOptions": {
    ...,
    "paths": {
      "$lib/*": ["src/lib/*"],
      ...
    },
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction • Docs • SvelteKit
SvelteKit is a framework for rapid development of robust, performant web applications. Building an app with all the modern best practices is fiendishly ......
Read more >
Sveltekit: Cannot find module 'swiper' - Stack Overflow
There is a workaround for SvelteKit (should work for Sapper too). Slider.svelte <script> import { Swiper, SwiperSlide } from 'swiper/svelte' ...
Read more >
Learn SvelteKit by Building a Reddit Clone Application
Under-the-hood, SvelteKit leverages Vite to spin up a development server that transforms and serves source code (over native ESM) on-demand.
Read more >
Getting started with Svelte - Learn web development | MDN
config.js : Svelte uses rollup.js as a module bundler. This configuration file tells rollup how to compile and build your app. If you...
Read more >
How to add SCSS or SASS to SvelteKit - Hao's learning log
Although Svelte/SvelteKit support other CSS preprocessors, nothing comes baked-in so we need to manually install dependencies in order to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found