-
Notifications
You must be signed in to change notification settings - Fork 290
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
How do I get the module's imported parent from an ESM module? #1476
Comments
This doesn't really make sense, a module can have multiple parents.
At the moment we use the full resolved URL, but there's no guarantee that things will stay this way. /cc @bmeck on that |
In CommonJS modules, Maybe, a better question is how to write an uncached ESM module with that constraint. (May require an es-discuss proposal instead...) |
@isiahmeadows both in browsers and in node's current implementation caches are done including fragments. However, node uses the fully resolved Response URL in browser terms. Browsers use the Request URL, so there is some mismatch. nodejs/modules#62 has some details on differences.
In CJS there is the possibility of reliable parents because evaluation is done inline. In ESM you can get into cases where the parent differs based upon timing: // main.mjs
import('a');
import('b'); // a
import 'c'; // b
import 'c';
|
This answers my question well enough. |
If we have two modules //a.js
export function readFiles(){
//read files, relative to the module that called this function
} Then, in some other file //b.js
import {readFiles} from 'a.js'
function start(){
let myFiles = readFiles()
console.log(`Here are my files: %s\n`, myFiles)
} The whole point is, even if its not reasonable for a module to have a parent, there should be some way of knowing that this method was called by that module, without explicitly passing a url |
In the docs, it makes no mention of the parent URL. I would like this, for the sake of resolving a module name from a query parameter. (It would make my module much easier to use.) Is it possible to retrieve the parent URL from the child, and if so, how?
Also, while I'm at it, how are URLs tested for equivalence WRT caching? Are hashes included, or only queries?
The text was updated successfully, but these errors were encountered: