By using export
keyword, we can export anything from a file to another file in JavaScript. But is their any way to export for specific files. So that other file can’t import from this file.
export const t = {
a:'this will export for index.js only'
}
2
Answers
If you are referring to how java controls access of classes to the rest of the application
public, private, protected
then no. If you only want a singular file to be able to use a variable then declare the variable in the file you want to use. or look into changing the architecture of your application. Ex: change your function to use dependency injection.There’s no way to achieve that directly, since
import.meta
does not contain that information. But you can do it if you export a factory function instead, that way you can access the caller file by usingError.stack
.module.js
index.js
See: Access the calling file name from within a function in a different file
I don’t recommend exporting something based on the caller file though.