ts-serve icon indicating copy to clipboard operation
ts-serve copied to clipboard

import declarations may only appear at top level of a module

Open lukakostic opened this issue 1 year ago • 2 comments

Server.ts :

import { serveDirWithTs } from "jsr:@ayame113/ts-serve";
Deno.serve((request) => serveDirWithTs(request));

index.html :

<html>
    <head>
        <script src="Test.ts"></script>
    </head>
    <body></body>
</html>

Test.ts :

import {test} from "./Test2.ts"
console.log(test);

Test2.ts :

export let test=1;

Result:

image image

Seems typescript imports dont work under Deno with ts-serve.
Seems something is wrapping the Test.ts file and so the import isnt top-level anymore,
but this is not apparent when clicking the error message as it shows the unmodified (correct) code.
Is this an issue with Deno.emit or ts-serve ?

lukakostic avatar Dec 19 '24 19:12 lukakostic

Ah thank you for finding this!

I'll look into this.

ayame113 avatar Jan 01 '25 11:01 ayame113

@lukakostic This seems to be an error coming from the browser. How about adding type="module" to your html?

 <html>
     <head>
-        <script src="Test.ts"></script>
+        <script src="Test.ts" type="module"></script>
     </head>
     <body></body>
 </html>

ayame113 avatar Jan 08 '25 11:01 ayame113