-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a variable or func to get the path of the current script, like __dirname in Node.js #1286
Comments
Hi, I am not sure if this is what you need. import { cwd } from 'deno'
console.log(cwd()) |
Actually no, cwd is just the working directory... |
Unfortunately, ~/demo/test/test.ts
~/demo/test/test.txt When I execute This is a common use, for example, when I use Deno to write some tool scripts |
Sounds like injecting through TS compiler should work. There is also a pitfall that the actual running files are loaded from cache under DENO_PATH and thus different from the expected TS file path. Need to differentiate the two. @kitsonk Does this look like a good place for injection? (on my phone so cannot really verify if it would work) |
No, in my opinion. We are working on #975. When that lands, coupled with #1190 then in a lot of cases the TypeScript compiler won't get involved. Rust "knows" where the original file is located, even if it is loading from the cache, but even then it is tricky when dealing with remote modules. Basing code dealing with the relative location of the module that the code is running in is rather dangerous. It can also be super fragile, what if the module get moved around. Why would you want to "hard code" that into the logic of the module? While it would be Ryan's call, I would personally want to try to avoid this at all costs unless there are problems that just can't be solved. Don't want to handle people a 👣 🔫 to author fragile code if we can avoid it. All file creation/read/access APIs that are exposed would be relative to the |
I think it would be nice if we could do
that is, statically resolving and including non-javascript files. Maybe that negates the need to have I'm not necessarily against I'm definitely not doing the two underscore thing again tho. We will use |
@ry we would/could handle any media type in the compiler. We already support export function toUint8Array(): Uint8Array {
return deno.getUint8Array("./style.css");
}
export function toString(): string {
return (new TextDecoder()).decode(deno.getUint8Array("./style.css"));
} I think we would have to generate that "shell" module which would be loaded in the runtime which will op back out to Rust to get the resource. Rust would use the module resolution logic, which would negate the need for the module to know where it sits. Doing some sort of "getter" as an export of a module is awkward since import * as styleCss from "./style.css";
const value = styleCss.toUint8Array(); |
Isn't this what |
@Janpot thanks - I didn’t know about that |
@Janpot I think we might need to tweak the tsconfig options for this to work as intended:
|
__dirname doesn't work in node.js either when you are using ES Modules. Here's how you get around it in node.js :
After this, I could use __dirname in the node script, but eslint couldn't parse any use of the import.meta object, because eslint doesn't support stage 3 proposals: |
You can now retrieve URL of script with |
Hesitated to write here this but all my searches led me to this thread. What about finding the directory of the current script then? That is what I want most of the time. The import.meta.url gives a |
@npup There's the path module which you could use. |
@SyrupThinker |
@npup |
@nayeemrmn that gives a path that looks like "/C:/Users/seishun/project/static-files", which Deno.readFileSync doesn't accept. |
Here's a hacky but cross-platform way: import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);
console.log(require.resolve("./static-file")); |
@seishun |
@nayeemrmn |
related to my above comment: #4915 |
Thanks for your mention, this is very useful for me! |
From my testing, it seems like |
@han-tyumi there is already an issue for that: #6344 |
import __ from "https://deno.land/x/dirname/mod.ts";
import { IMakeLoc } from "https://deno.land/x/dirname/types.ts";
const { __filename, __dirname }: IMakeLoc = __(import.meta) is a way to do it, but I really don't like to depend on an external module for something like this. |
import { fromFileUrl, dirname } from "jsr:@std/path";
console.log(dirname(fromFileUrl(import.meta.url))); |
There is also this: const __filename = import.meta.filename;
const __dirname = import.meta.dirname; |
When the entry script is under the subdirectory, reading the relative file under the subdirectory is unsuccessful.
In
Node.js
, you can get the directory where the script is located by__dirname
.And
__file__
can also be used inPython
The text was updated successfully, but these errors were encountered: