Skip to content
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

Closed
dead-claudia opened this issue Sep 5, 2018 · 5 comments
Closed

How do I get the module's imported parent from an ESM module? #1476

dead-claudia opened this issue Sep 5, 2018 · 5 comments

Comments

@dead-claudia
Copy link

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?

@devsnek
Copy link
Member

devsnek commented Sep 5, 2018

Is it possible to retrieve the parent URL from the child, and if so, how?

This doesn't really make sense, a module can have multiple parents.

how are URLs tested for equivalence WRT caching?

At the moment we use the full resolved URL, but there's no guarantee that things will stay this way. /cc @bmeck on that

@dead-claudia
Copy link
Author

This doesn't really make sense, a module can have multiple parents.

In CommonJS modules, module.parent is the first parent to request the module. I'd like to maybe do similar for the module.

Maybe, a better question is how to write an uncached ESM module with that constraint. (May require an es-discuss proposal instead...)

@bmeck
Copy link
Member

bmeck commented Sep 6, 2018

@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 CommonJS modules, module.parent is the first parent to request the module. I'd like to maybe do similar for the module.

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';

c could have either b or a evaluate first depending on speed of loading the files. However, c will always evaluate before a or b.

@dead-claudia
Copy link
Author

This answers my question well enough.

@akwotom
Copy link

akwotom commented Oct 17, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants