Search code examples
requirejstypescriptamddirname

Use __dirname into class compiled in Typescript AMD


I'm looking for a solution to use __dirname (or equivalent) inside a TypeScript class compiled in AMD, __dirname doesn't exists there. And because it's typescript, I can't (or I don't know how) import module that contains uri, allowing me to get the uri.

I found the solution once, but I don't remember how and I can't find it again so far.

I took a look to this, but it's in pure javascript and I use typescript, I tried to import module, but I get a TS error.

https://stackoverflow.com/questions/9027429/how-to-use-nodejs-global-module-objects-in-requirejs-modules


Solution

  • One solution is to:

    In app.js, create a global var:

    __basePath = __dirname;

    Then, in the AMD script, use __basepath + 'relativePathFromBasePath' to load the file.

    I know I found another solution but I cannot remember how.