Home > database >  Node.js. How to get a path to imported file
Node.js. How to get a path to imported file

Time:03-23

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!

CodePudding user response:

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

  • Related