-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
Means to detect if the current code is run via --import #53882
Copy link
Copy link
Closed as not planned
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.loadersIssues and PRs related to ES module loadersIssues and PRs related to ES module loadersstale
Metadata
Metadata
Assignees
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.feature requestIssues that request new features to be added to Node.js.Issues that request new features to be added to Node.js.loadersIssues and PRs related to ES module loadersIssues and PRs related to ES module loadersstale
Type
Fields
Give feedbackNo fields configured for issues without a type.
Projects
Status
Triaged
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--importand 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.isEntryPointor something like that?The entry point is already marked on
globalThisvia 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().stackto 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.