-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 import.meta.directory
and import.meta.file
properties
#47756
Comments
Should be |
import.meta.dir
and import.meta.file
propertiesimport.meta.directory
and import.meta.file
properties
Absolutely. I amended OP and title accordingly. |
What would we use as values when the module is loaded from outside of the FS? E.g. from the network or from a custom loader. |
One option may be to define both as |
I’m not sure it’s worth adding something that you can rely upon because it may not be defined. Maybe we should ask why |
Sure. Webpack, for instance, requires an absolute path. This requirement brought me to the idea that something intrinsic, important is missing. Multi-threaded applications or libraries might also prefer to work with absolute paths. |
That looks like an issue on Webpack side rather than Node.js. Did you try reaching out to them? In the mean time, you can use |
I have a different perspective. Writing desktop applications or libraries will not be able to use |
Why is that? What is it that you can do with an absolute path that you can’t do with a |
After further digging into the Webpack issue I'm facing, I'm not quite sure if it isn't a Webpack issue after all. However, from my observation I noticed that the import * as path from 'node:path';
const url = import.meta.url;
const dir = path.dirname(import.meta.url);
const newdir = path.join(path.dirname(import.meta.url), 'dust');
console.log(`url: ${url}`);
console.log(`dir: ${dir}`);
console.log(`newdir: ${newdir}`); … yields:
|
Using const newdir = new URL('./dust/', import.meta.url); |
That's exactly what this issue is about: Providing |
But why? Shouldn’t you use URLs instead? |
Here's a simple test program using the file system and the (failing) result from running it: import * as path from 'node:path';
import * as fs from 'node:fs';
const filePath = path.join(path.dirname(import.meta.url), 'test.txt');
console.log(filePath);
fs.writeFileSync(filePath, 'My test.');
Same for: import * as fs from 'node:fs';
const filePath = new URL('Test.txt', import.meta.url).toString();
console.log(filePath);
fs.writeFileSync(filePath, 'My test.');
Are you convinced now? |
Here's the same program written using URL: import * as fs from 'node:fs';
const fileUrl = new URL('test.txt', import.meta.url);
console.log(fileUrl);
fs.writeFileSync(fileUrl, 'My test.'); |
I added the URL example to my previous post. Fails just the same. |
It fails because you convert it to a string. You need a URL instance. |
Yes, you are right. My machine is currently building, so I'm very slow in responding and amending my comments at the moment. Yet, it doesn't look like |
URL APIs won’t provide path manipulation, only URL manipulations. EDIT: I should point out that there are specific use case that are not supported (e.g. |
@aduh95: I comprehend and agree. So, if |
yeah why not, PRs welcome! |
I could give it a try. Would you mind pointing me to the Never mind … found it. |
I tried a preliminary solution on this issue, just to see if it works (PR: #47982). Unfortunately, I'm not able to set-up a running development system in due time. Would you mind checking if the changes I made are appropriate? I could add further path related methods then. |
Why don't you use |
Slowly I feel being kidded with. I added these two properties because @aduh95 suggested to do so. I didn't feel like and I didn't express that I'd like to have these two properties at hand. I regularly need That's not a niche. It's rather a quite daily business. |
As multiple people have tried to explain throughout this discussion, that's a valid use case and it is already possible using, for example, This is widely used. If you take a look at the MDN page about
Your original feature request was the addition of new properties to In #47982, you added Could you please clarify what exactly your feature request is at this point? |
Apologies if I led you into a dead end here @SetTrend, this was not my intent. I was thinking on adding them to
And maybe we'll find that the current APIs are good enough as is, and it would help folks who are familiar with |
Thanks for clarifying. Given that some programs store a number of variables for storing their data within a hierarchy, and given the user is able to set these variables, I still have the impression that import URL from 'node:url';
let langId= ...;
let images = ...;
let packages = ...;
let libLocation = ...;
const newUrl = new URL(langId, new URL(images, new URL(packages, new URL(libLocation, import.meta.url)))); |
If // No import is needed, URL is available as a global.
// If you want to import it, you can do this as such:
import { URL } from 'node:url';
let langId= ...;
let images = ...;
let packages = ...;
let libLocation = ...;
const newUrl = new URL(`./${libLocation}/${packages}/${images}/${langId}/`, import.meta.url); More likely you would store the intermediate folder references as URL instances: let langId= ...;
const libLocation = new URL('./lib/', import.meta.url);
const packages = new URL('./packages/', libLocation);
const images = new URL('./images/', packages);
const newUrl = new URL(`./${langId}/`, images); All in all, it's not a perfect API but it's the most closest thing to perfect: it's standard. I encourage you to use it over Anyways, I'm going to close this issue, as it's derived quite some bit from the original request. Feel free to continue the discussion and ask more questions though. |
What is the problem this feature will solve?
Currently,
import.meta.url
gets the current file's absolute file path in URL format. This format cannot be used withnode:path
for creating absolute file paths.The proposed additional properties will provide the current file's file path details in a format that can be used with
node:path
.What is the feature you are proposing to solve the problem?
import.meta
should provide the following additional properties:directory
file
Example
Given the following absolute file path (on Windows):
…
import.meta
would provide the following properties:The text was updated successfully, but these errors were encountered: