TypeScript: __dirname value?

My folder structure is as follows:

  • attachment
    • tsFolder
      • XX.ts

Code in XX.ts file:

var __dirname: string;
console.log("__dirname: " + __dirname); // the output is undefined.

I tried: commented on the code var __dirname: string;, value __dirname: ...../apppaste into the current folder, for example ...../app/tsFolder.

Can anyone explain this?

+6
source share
3 answers

Just run

npm install --save-dev @types/node

And if that doesn't work on its own, add

"types": ["node"]

in the field tsconfig.json compilerOptions.

+11
source

I assume that you are referencing the global variable nodejs __dirname .

.... /app, , XX.ts .... /app. js - __dirname (js ).

, , __dirname. nodejs , , :

declare var __dirname;
+5

I tried npm i @ types / node without changes in tsconfig. You must ensure that the working directory path must be less than the maximum Windows limit.

0
source

All Articles