Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSC cannot find name of Global Object

Tags:

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?

like image 245
Nelson Avatar asked Aug 01 '17 19:08

Nelson


People also ask

How to fix Cannot find name in TypeScript?

To solve the error "Cannot find name module", install the node types by running npm i -D @types/node . If the error is not resolved, try adding node to your types array in tsconfig. json and restarting your IDE.

Where do I declare global TypeScript?

To declare a global variable in TypeScript, create a . d. ts file and use declare global{} to extend the global object with typings for the necessary properties or methods.

Do you need to install type definitions for node?

The Node runtime does not ship with its own type definitions, so we need to import those types separately. Where can we find them? They are also in npm but need to be installed separately.


Video Answer


1 Answers

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

Example:

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

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

like image 120
Nelson Avatar answered Sep 19 '22 21:09

Nelson