Filip Seman
Filip Seman•14mo ago

Is possible to run nodejs (typescript) project with esm modules?

I'm trying to run small nodejs project with esm modules. Simple example
// foo.ts
import { bar } from './bar.js';

// bar.ts
exportconst bar = 'bar';
// foo.ts
import { bar } from './bar.js';

// bar.ts
exportconst bar = 'bar';
i'm getting error: Module not found "file:///tmp/tmp.LPU7JDdIZK/bar.js".
15 Replies
marvinh.
marvinh.•14mo ago
You need to use the proper extensions. Change the path to ./bar.ts
Filip Seman
Filip SemanOP•14mo ago
By proper you mean it wont work in same way like running ts-node --esm foo.ts with esm modules?
marvinh.
marvinh.•14mo ago
In Deno all files are ESM by default already
Filip Seman
Filip SemanOP•14mo ago
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-paths
Documentation - Modules - Reference
Module syntax and compiler options reference
Doctor 🤖
Doctor 🤖•14mo ago
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?
Filip Seman
Filip SemanOP•14mo ago
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
raunioroo
raunioroo•14mo ago
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
Filip Seman
Filip SemanOP•14mo ago
@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.
raunioroo
raunioroo•14mo ago
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.
Doctor 🤖
Doctor 🤖•14mo ago
Why not just run Deno?
Filip Seman
Filip SemanOP•14mo ago
@Doctor what you mean?
Doctor 🤖
Doctor 🤖•14mo ago
Your original message said you're trying to run nodejs. Why not run deno instead?
Filip Seman
Filip SemanOP•14mo ago
Yes, i'm trying to run Nodejs project in TypeScript, but it seems that esm modules with are not fully supported by Deno
Doctor 🤖
Doctor 🤖•14mo ago
Why not just write it all for Deno? Why write nodejs code when you could write Deno code
Filip Seman
Filip SemanOP•14mo ago
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

Did you find this page helpful?