-
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
Means to detect if the current code is run via --import
#53882
Comments
--import
on the app entry point--import
You can recognize this via: |
It does not tell you if code has been loaded via
import { isMainThread } from "worker_threads";
console.log("import.mjs", isMainThread);
import { isMainThread } from "worker_threads";
console.log("app.mjs", isMainThread);
|
|
@timfish One way to check if your script was imported is to use the cache: ➜ node --import ./import.js main.js
[IMPORT] false
[MAIN] true
|
Thanks @RedYetiDev! Is This does have the downside that it's only reliable the first time it's called. Can it be removed from the cache so it's correct every time it's called?
import module from "node:module";
export function check(type) {
console.log(
`[${type}]`,
Object.values(module._pathCache).includes(import.meta.filename)
);
}
import { check } from "./check.mjs";
check("import");
import { check } from "./check.mjs";
check("app");
It looks like this would be easy to add to Node, I'm happy to do a PR if an API can be agreed! const {
privateSymbols: {
entry_point_module_private_symbol,
},
} = internalBinding('util');
const isEntryPoint = globalThis[entry_point_module_private_symbol]) |
I don't know to much about whether the API is stable (because it's used internally) so CC @nodejs/loaders As for the addition of the API, I believe #49440 discussed something similar. This may be a duplicate of that, I don't really know. |
Does a similar approach work for --require? |
Yes, I think |
So if |
Yes |
for I think a similar API should be added for |
That could also work. That's perhaps more appropriate for this particular case because there are questions about what I think we don't need to distinguish between modules loaded via |
I agree; does |
Not in my fast local tests in main thread only. |
Unless the code is bundled, a library will always read
|
Agreed. In the meantime, I think looking in |
What is the problem this feature will solve?
Code might want to determine if it's running via main app entry point or via a module imported via
--import
. For example, a library might instruct users to run the code via--import
and might want to warn users if this is not the case.What is the feature you are proposing to solve the problem?
Not sure what the API should be, maybe
process.isEntryPoint
or something like that?The entry point is already marked on
globalThis
via a private symbol:node/lib/internal/modules/esm/module_job.js
Lines 255 to 257 in aca49fc
What alternatives have you considered?
Currently the only way I can think to detect this would be to parse
new Error().stack
to find the entry point file and then parse and compare toprocess.execArgv
. This is not trivial since you'd need to resolve bare specifiers to their actual source files.The text was updated successfully, but these errors were encountered: