Is possible to run nodejs (typescript) project with esm modules?
I'm trying to run small nodejs project with esm modules. Simple example
i'm getting
error: Module not found "file:///tmp/tmp.LPU7JDdIZK/bar.js".
15 Replies
You need to use the proper extensions. Change the path to
./bar.ts
By proper you mean it wont work in same way like running
ts-node --esm foo.ts
with esm modules?In Deno all files are ESM by default already
That's ok, but i'm wondering why it doesn't suppor standard
.js
import even if it's typescript file
https://www.typescriptlang.org/docs/handbook/modules/reference.html#extensionless-relative-pathsDocumentation - Modules - Reference
Module syntax and compiler options reference
You're trying to import a file that doesn't exist? If your bar file was a JavaScript file instead of a typescript file then it would work with .js
Do you even need a dot extension in nodejs?
Yes, you need them for esm modules in general if you want to follow the standard
Maybe i'm missing something, correct me if I am wrong
yes it's a bit unfortunate that Node/TS needed to do these file extension gymnastics to make modern ESM/TS work without breaking old CJS code and stuff. that's the baggage that backwards compatibility can bring.
...now that Deno actually supports TS and ESM "properly" as-is, you need to specify also the file extensions in imports as is, as intended by the standards. So .ts files are ts files and imported as TS files, same goes for JS. sucks that this is not 100% compatible with node, but node is the weird one here
@raunioroo Are you sure it's Node's implementation? From Typescript documentation:
If TypeScript determines that the runtime will perform a lookup for ./a.js given the module specifier "./a", then ./a.js will undergo extension substitution, and resolve to the file a.ts in this example.To me it sounds like it's up to the runtime to implement this behaviour.
I think that's because only runtime at that time was Node, and Typescript had to spec it that way to have any chance of it working (in Node).
but yes, I guess you might be technically correct kinda.
Why not just run Deno?
@Doctor what you mean?
Your original message said you're trying to run nodejs. Why not run deno instead?
Yes, i'm trying to run Nodejs project in TypeScript, but it seems that esm modules with are not fully supported by Deno
Why not just write it all for Deno?
Why write nodejs code when you could write Deno code
I understand you're asking why not start from scratch, but right now I'm interested in the compatibility layer
https://github.com/denoland/deno/issues/21198