D
Deno

help

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

FSFilip Seman11/10/2023
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".
Mmarvinh.11/10/2023
You need to use the proper extensions. Change the path to ./bar.ts
FSFilip Seman11/10/2023
By proper you mean it wont work in same way like running ts-node --esm foo.ts with esm modules?
Mmarvinh.11/10/2023
In Deno all files are ESM by default already
FSFilip Seman11/11/2023
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
DDoctor11/11/2023
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?
FSFilip Seman11/11/2023
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
Rraunioroo11/11/2023
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
FSFilip Seman11/11/2023
@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.
Rraunioroo11/11/2023
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.
DDoctor11/12/2023
Why not just run Deno?
FSFilip Seman11/12/2023
@Doctor what you mean?
DDoctor11/12/2023
Your original message said you're trying to run nodejs. Why not run deno instead?
FSFilip Seman11/12/2023
Yes, i'm trying to run Nodejs project in TypeScript, but it seems that esm modules with are not fully supported by Deno
DDoctor11/12/2023
Why not just write it all for Deno? Why write nodejs code when you could write Deno code
FSFilip Seman11/14/2023
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

Looking for more? Join the community!

Recommended Posts
Any tutorial links to help me understand the Schedule string in Deno.cron()I am currently working on an issue on the deno repo and I am trying to reconstruct the deno scheduleDeno Deploy failed:Uncaught Exception /Permission Denied : Deno.exit() is not allowedI have a college assignments that needs to be deployed using deno deploy but i get this message whenConnect to a WebSocket through a HTTP(S)/SOCKS5 proxyTitle - I am trying to connect to a `wss://` websocket through a proxy of choice at runtime. Is therExternal package code throws error: window is undefinedHello, I have imported an npm package to my project. And in this package's code, there is a conditiError with importsI have this fancy peace of code that import a bunch of files from a directory: ```ts const utils = Deno.env via fileHow can I use set env from a file and get them via deno.env.get() function as i want to push my codeRunning LLaMA on DenoHello! I use Deno to run my apps on the edge, what Deno solution or other provider would be ideal Occasional very slow isolate start timesI have a Vue.js app (using Vite) project with a few edge functions running on Deno Deploy. What I'mChanging terminal zoom in windows?Is it possible to change the terminal zoom using deno? Also, this is about windows terminal, not conWhimsical error shows up when I run a fileI'm trying to make a discord bot with Deno and d.js but for some reasons I get this error ```tex errI cannot able to install itwhen i run ```sh irm https://deno.land/install.ps1 | iex ``` i got this error messageDeno lint custom rulesHey is there currently a way to create custom rules for Deno lint? And if so how can I specify a cususing OpenAI npm in Deno deploy playground:i don't know how to return the resualt , now this just return [object Object] i don't how to use asFrontend & Backend in one Deno Deploy projectI have a Nuxt 3 frontend and an Oak backend. Is it technically possible to deploy both in one deno dIs there a split_to_array function like join_to_stringI need to split a string with some delimiter `=[, ` and ask my self if there is something in deno whHow to set proxy to overall all requests coming from application?In Node JS, we can `import https` and set `https.proxy = "proxyDetails` However, I couldn't find a wOverload not typing object literalsshouldn’t only “real” show up as a property here? if not, how could I achieve that?Making Post Request To HubSpot ApiHi there I'm trying to make a post request to the HubSpot Api. However the request results in a "HubCLI prompt consistent with Deno CLII want to make outputs of my CLI tools look consistent with Deno CLI. Is there a rust crate to do thcannot set property on fetch responsein the browser this works ```javascript let o = await fetch('https://deno.com'); o.asdf = 2 console.