0

I'm writing a parser and i'm getting imports like ../modules/greeting.js, also i have an absolute path to file (from where i have done an import) - for instance C:\Desktop\Folder\src\scripts\main.js.

How to get path to file which i have imported? (in this case it should be src/modules/greeting.js). Thanks!

  • Does this answer your question? [In node.JS how can I get the path of a module I have loaded via require that is \*not\* mine (i.e. in some node\_module)](https://stackoverflow.com/questions/10111163/in-node-js-how-can-i-get-the-path-of-a-module-i-have-loaded-via-require-that-is) – MinusFour Mar 22 '22 at 17:33
  • No, i just have strings (with paths i have described above). I'm writing on bundle side so it has nothing to do with client side – volodymyr_dos Mar 22 '22 at 18:19

1 Answers1

0

Use this for the absolute path to the imported file:

path.resolve(path.dirname('C:\\Desktop\\Folder\\src\\scripts\\main.js'), '../modules/greeting.js')

Returns: "C:\Desktop\Folder\src\modules\greeting.js"
Check the API docs for more path utils: https://nodejs.org/api/path.html

Andrei
  • 4,289
  • 4
  • 25
  • 23