Repository navigation
URL version of process.cwd() #49273
Copy link
Copy link
Closed as not planned
Labels
feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.processIssues and PRs related to the process subsystem.Issues and PRs related to the process subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
Description
What is the problem this feature will solve?
Right now we don't have convenient way to get current working directory in URL form such as file:///path/to/current/working/directory/.
Having easy access to this form would allow to resolve local paths using WHATWG URL constructor, i.e. new URL('../path/to/file', cwdURL), without need to import path and worry about platform-specific separators.
Getting this form in userspace is not very convenient:
// results in URL without trailing slash, indicating that it's directory and allowing to use it directly as baseURL
const cwdURL = url.pathToFileURL(process.cwd());
// appending pathname suffix to full href is semantically wrong
const cwdURL = url.pathToFileURL(process.cwd()).href + '/';
// triggers pathname setter which requires reparsing URL again
const cwdURL = url.pathToFileURL(process.cwd());
cwdURL.pathname += '/';
// three imports for one value, including `path` which we may want to avoid
const cwdURL = url.pathToFileURL(process.cwd() + path.sep);
// very bad for multiple reasons but tempting enough to appear in the wild :(
const cwdURL = `file://${process.cwd()}/`;What is the feature you are proposing to solve the problem?
- support
URLinstances withfile:protocol inprocess.chdir(directory). - add
process.cwdURL()that returnsURLinstance or string. - add
resolve(...urls)method that would construct new absolute URL on top of cwd if all providedurlsare relative.url.resolve()is not vacant and has different behaviour, so it should have different name or place.
Better naming suggestions are very welcome.
What alternatives have you considered?
- userland module.
- adjustments to parsing URLs with
file:protocol that would resolve relativepathnameagainst cwd instead of root, i.e.:
// current behaviour
new URL('file:./file.txt').href === 'file:///file.txt'
// proposed behaviour
new URL('file:./file.txt').href === 'file:///path/to/current/working/directory/file.txt'Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.processIssues and PRs related to the process subsystem.Issues and PRs related to the process subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.whatwg-urlIssues and PRs related to the WHATWG URL implementation.Issues and PRs related to the WHATWG URL implementation.
Activity
aduh95 commentedon Aug 21, 2023
What not use paths? Do you have a specific use case in mind?
LiviaMedeiros commentedon Aug 22, 2023
In general,
file:///usr/portage/net-dialup/andrsync://winchester.local/portage/net-dialup//pathToFileURL\((process\.)?cwd\(/, js:file://${cwd}, ts:file://${cwd}lib/repl.jslib/internal/process/pre_execution.jslib/internal/modules/esm/resolve.jslib/internal/modules/esm/loader.jsIn most of these, having
URLinstance that is resolved against cwd would allow avoid conversion (or even double conversion) between URL and path and having a mix of these in code (which can easily become a bug farm, as many operations keep working until there is directory with%/?/#, or URL with hash, or the code runs on Windows).As specific usecase, let's say, I want to read a file defined by URL (can be string, can be relative), in a way like
It's simple with absolute
URL, but there's no concise way to resolve relative URL into it: WHATWG URL standard doesn't support relative URLs (at least, yet),url.fileURLToPath('file:./path')resolves against/or throws if protocol is omitted.Also as a matter of fact, even if we want to switch context from URLs to paths. we don't have
relativeFileURLtoRelativePath()utility either.anonrig commentedon Aug 22, 2023
I think what you want is similar to Rust's Path struct where you can easily mutate the pathname, get URL components and change them without sacrificing the DX and performance.
github-actions commentedon Feb 19, 2024
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
github-actions commentedon Mar 21, 2024
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
karlhorky commentedon Apr 5, 2024
FWIW I also ran into this issue today with using
process.cwd()as part of a path passed to dynamicimport(), and the error was pretty confusing (and only on Windows);Doesn't show any user code in stack trace. In my case, I was using the dynamic import inside of a
eslint.config.js(ESLint Flat Config) file:Eventually I guessed my way to figure out it was the
process.cwd()failing only on Windows inside the dynamicimport(), and also usedpathToFileURLfromnode:url:karlhorky commentedon Apr 5, 2024
@aduh95 @anonrig is there any Node.js implementer interest in making this easier to fall into the pit of success on Windows?
If so, maybe this issue could be reopened?