TSC 找不到全局对象的名称

问题描述 投票:0回答:2

在我的打字稿代码中,我试图访问

__dirname
全局对象:https://nodejs.org/docs/latest/api/globals.html

我看到编译错误:

TS2304:Cannot find name '__dirname'.
我可以毫无问题地运行代码。我怎样才能让
tsc
编译而不出错?

node.js typescript
2个回答
83
投票

tsconfig.json
中将
"node"
添加到
compilerOptions.types

示例:

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

然后运行

npm install @types/node --save-dev


0
投票

快跑吧

npm install @types/node --save-dev

pnpm install -D @types/node
© www.soinside.com 2019 - 2024. All rights reserved.