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.
سؤال
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.
المحلول
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.
نصائح أخرى
I do not use TypeScript, but on the basis of the TypeScript code I've seen and on the basis of what I know of RequireJS, I believe you should be able to access module.uri
like this:
import module = require("module");
console.log(module.uri);
The name module
is special. You could also use require.toUrl(<module name>)
(that's Url
as in URL whereas the variable above is uri
as in URI
) but it does not seem as useful as module.uri
for this task. For one thing you'd have to include the module's name in the call.
module.uri
may contain ..
, so it needs cleaning up. I understand your code to be running server-side, so in Node.js we'd call path.dirname(path.normalize(module.uri))
.