How do I know what require or import statements were used in a .js
or .mjs
or .cjs
file?
Asked
Active
Viewed 42 times
0

Gary
- 2,293
- 2
- 25
- 47
-
It's not clear what exactly you're trying to do, but there is no built-in way in nodejs to know what files your currently module has imported. Bundlers do this by parsing your code and building dependency chains. Your `package.json` file also may specify some dependencies (but not all). – jfriend00 Oct 24 '22 at 23:47
-
pls let me know how to access the ast in JavaScript/Nodejs so that I can access what modules have been imported. Can I do it with require(module"). I am clueless here of how I can do it. – Gary Oct 25 '22 at 00:55
-
the worst is getting a close request on a question that someone does not want to answer or let anyone else answer it. hopefully that was not targeted for a close because of me. i do not think that was nice attitude nor was that professional. but it is fine. this needs an answer – Gary Oct 25 '22 at 00:58
-
@jfriend00 what is the simplest and smallest good bundler parser you can refer me. i am using this for a package. any suggestions? – Gary Oct 25 '22 at 01:29
-
I got a direction while searching right now. Let me check if this solves the purpose. but is there no ast access directly in Nodejs? any nice bundler parser https://stackoverflow.com/questions/16127985/what-is-javascript-ast-how-to-play-with-it – Gary Oct 25 '22 at 01:37
-
1What problem are you really trying to solve? Right now this is an XY problem where you're asking about a solution rather than explaining the original problem. Normal programming in nodejs does not require any knowledge of what modules have been loaded. You write your code to load the modules you need and you use them. So, apparently you have some other problem you're trying to solve. If you edit your question to describe the actual problem, then perhaps we can tell you how people usually solve those types of problems. – jfriend00 Oct 25 '22 at 01:54
-
I didn't vote to close your question, but you get close votes here when the underlying problem you're trying to solve is not clear or when you're not clarifying things people are asking you about your question. I've already told you that nodejs does not give you access to the module-loaded graph. – jfriend00 Oct 25 '22 at 01:55
-
@jfriend00 frankly i think the question's intent had a lot of clarity unless the reader was in an urgency to close the question. it is normal for me or my questions. i am getting used to it now. :). accessing require/import statements in the syntax was the quesion. but i surely thank you for the bundlers information. it lead me to typescript and @babel/parser allowing ast/syntax access. let me try that. seems like there is a proposal for this since more than a year – Gary Oct 25 '22 at 02:53
-
Well, I guess the only answer we can provide here is that nodejs does not provide that information. How else you would get that information from outside of nodejs depends upon the details of the actual problem you're trying to solve and what exactly you really need it for which you do not say anything about here so we really have no idea how to answer this question. An unanswerable question (because of lack of information in the question) here is generally considered an incomplete question that should be closed. – jfriend00 Oct 25 '22 at 03:09
-
1Also, keep in mind that what you think is a clear question is irrelevant (particularly given all the context you have in your head that we have no access to). What matters is whether your readers are finding all the information they think they need to provide a meaningful and useful and complete answer. I've often heard it put this way. A good university textbook is not something the author or the professor thinks is great - it's something the students think is great (the audience). You may also want to read about what an [XY question](https://meta.stackexchange.com/a/66378/164634) is. – jfriend00 Oct 25 '22 at 03:10
-
i have raised an issue with the nodejs team for a status follow up with tc9 proposal. pls vote and support his feature status request in the github issue i raised if you think this should be a feature in the nodejs. the feature is available in python. https://github.com/nodejs/node/issues/45158 – Gary Oct 25 '22 at 03:27