Search code examples
node.jstypescript

TSC cannot find name of Global Object


In my typescript code I am trying to access the __dirname global object: https://nodejs.org/docs/latest/api/globals.html

I am seeing a compile error: TS2304:Cannot find name '__dirname'. I can run the code without any issue. How can I get tsc to compile without error?


Solution

  • In tsconfig.json add "node" to compilerOptions.types.

    Example:

    {
     "compilerOptions": {
      ...
      "types": [
        "node"
      ]
      ...
      }
    }
    

    Then run npm install @types/node --save-dev