How can I get that source path?

For example, If I have the following path to run:

Windows:
D:\Test\Test_Theia\browser-app

Linux:
\home\username\Test\Test_Theia\browser-app

And I am running “yarn theia start” from the “browser-app” folder

I am developing an extension and I need to get the source path.

For the example above it would be:

Windows:
D:\Test\Test_Theia\

Linux:
\home\username\Test\Test_Theia\

Hi there, thanks for the discussion.

The easiest way would be to use the __dirname or __filename variable provided by the node.js backend.

For example, if your extension directory is something like D:\Test\Test_Theia\custom-extension and you have a file D:\Test\Test_Theia\custom-extension\src\node\test.ts, calling console.log(__dirname) in there will log D:\Test\Test_Theia\custom-extension\lib\node to the console. You can then climb the directory hierarchy three times with something like path.join(..., '..') until you hit D:\Test\Test_Theia.

Be aware, that this only works for the node.js backend code. Doing this in the frontend code won’t work.

@msujew

Thanks for your detailed explanation.

Works like charm!

My mistake is that I was always trying from the fronted code.

1 Like