在我的打字稿代码中,我试图访问
__dirname
全局对象:https://nodejs.org/docs/latest/api/globals.html
我看到编译错误:
TS2304:Cannot find name '__dirname'.
我可以毫无问题地运行代码。我怎样才能让tsc
编译而不出错?
在
tsconfig.json
中将 "node"
添加到 compilerOptions.types
。
示例:
{
"compilerOptions": {
...
"types": [
"node"
]
...
}
}
然后运行
npm install @types/node --save-dev
快跑吧
npm install @types/node --save-dev
或
pnpm install -D @types/node