sveltejs / svelte

web development for the rest of us
https://svelte.dev
MIT License
79.78k stars 4.23k forks source link

"Cannot find module" error if `lang="ts"` is ommited #14201

Open romaindurand opened 18 hours ago

romaindurand commented 18 hours ago

Describe the bug

Hi ! I don't know if this is a bug or if this is intended but when I import a module in a page, I get an error if the module is a .ts file and I dont put a lang="ts in my <script>.

There's no TS syntax in the <script> or in the imported module.

Adding lang="ts" in the src/routes/+page.svelte <script> fixes the issue.

I usually put lang="ts in all my scripts but the autoimport feature in vscode creates a simple <script></script> if there are none so this took me some time to figure out

image

Reproduction

Here's a small reproduction repro : https://github.com/romaindurand/repro-svelte-ts-import-error

Logs

the svelte CLI configuration :

❯ npx sv create repro-svelte-ts-import-error
┌  Welcome to the Svelte CLI! (v0.6.1)
│
◇  Which template would you like?
│  SvelteKit minimal
│
◇  Add type checking with Typescript?
│  Yes, using Typescript syntax
│
◆  Project created
│
◇  What would you like to add to your project? (use arrow keys / space bar)
│  prettier, eslint
│
◇  Which package manager do you want to install dependencies with?
│  pnpm
│
◆  Successfully setup add-ons
│
◇  Successfully installed dependencies
│
◇  Successfully formatted modified files
│
◇  Project next steps ─────────────────────────────────────────────────────╮
│                                                                          │
│  1: cd repro-svelte-ts-import-error                                      │
│  2: git init && git add -A && git commit -m "Initial commit" (optional)  │
│  3: pnpm dev --open                                                      │
│                                                                          │
│  To close the dev server, hit Ctrl-C                                     │
│                                                                          │
│  Stuck? Visit us at https://svelte.dev/chat                              │
│                                                                          │
├──────────────────────────────────────────────────────────────────────────╯
│
└  You're all set!

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
    Memory: 15.47 GB / 30.98 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
    pnpm: 9.12.2 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chrome: 129.0.6668.58
  npmPackages:
    svelte: ^5.0.0 => 5.1.12

Severity

annoyance

elliott-with-the-longest-name-on-github commented 7 hours ago

This is intended behavior. Think about it: Can you import a TypeScript file into a JavaScript file, then run that JavaScript file without using TypeScript?

Conduitry commented 6 hours ago

There is no $lib/index.js file for you to be importing - there's an index.ts. I'm actually surprised that adding lang="ts" changes the resolution logic so that $lib/index.js can resolve to index.ts.