octo3.
octo3.8mo ago

How to get `require.main.filename` like Node as Deno

I am making a module to be published on deno.land/x. I want the user to put a file describing the configuration for the module on the same level as the entrypoint file. I could not figure out how to get the entrypoint filename from the module side. In Node.js, require.main.filename seems to solve this. How would this be appropriate to write in Deno?
9 Replies
Joe Hildebrand
Joe Hildebrand8mo ago
import.meta.main is a boolean
octo3.
octo3.8mo ago
Thanks @Joe Hildebrand san However, A does not satisfy the requirement. What we want to know is not whether the script we are running is main, but the path of the main file.
Joe Hildebrand
Joe Hildebrand8mo ago
Say more about what you're trying to accomplish?
octo3.
octo3.8mo ago
@Joe Hildebrand I will list examples of what I would like to do. I hope this gets the message across. - User imports my public module from deno.land/x into app.ts - When executed, the module will explore the expansion directory in the same hierarchy as app.tsx. - If the expansion directory does not contain the desired file, the module will provide functionality with default parameters. We want to get the path to app.ts to make this work.
Joe Hildebrand
Joe Hildebrand8mo ago
Thank you, that makes sense. I will think about this some (maybe someone else can help as well)
octo3.
octo3.8mo ago
Thankyou @Joe Hildebrand san. This will be an extension mainly for Fresh. So the goal is to get the exact path of main.ts.
syhol
syhol8mo ago
Does this fulfill your requirements https://deno.land/api@v1.38.5?s=Deno.mainModule Deno.mainModule
The URL of the entrypoint module entered from the command-line. It requires read permission to the CWD.
Deno
Deno.mainModule | Runtime APIs | Deno
The URL of the entrypoint module entered from the command-line. It requires read permission to the CWD.
jcayzac
jcayzac8mo ago
oh this is nice! I like const isRepl = Deno.mainModule.split('/').slice(-1)[0] === '$deno$repl.ts' to detect the repl (I wonder what Deno.mainModule returns in the Jupyter kernel… 🤔 )
octo3.
octo3.8mo ago
Thank you @Syhol san. This seems to solve the problem.