在Node16版本中,我想要使用新的定时器/承诺API。我用NVM
安装了Node 16
,并切换到它:
$ nvm use
Found '/Users/golergka/Projects/my-project/.nvmrc' with version <16>
Now using node v16.0.0 (npm v7.10.0)
甚至在我的package.json
:中添加了引擎
"engines": { "node": ">=16" }
我不确定是否需要安装@types/node
的16
版本,但我尝试了(没有成功(:
npm install --save-dev @types/node@16
npm ERR! code ETARGET
npm ERR! notarget No matching version found for @types/node@16.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
目前,我已经安装了^14.14.41
。
最后,我尝试在Typescript
文件中导入setTimeout
:
import { setTimeout } from 'timers/promises'
但出现了一个错误:
Cannot find module 'timers/promises' or its corresponding type declarations.
我该怎么解决这个问题?
它已于3天前在@types/node
中发布。只需运行npm install --save-dev @types/node@latest
或yarn add --dev @types/node@latest
。版本为0。
当前节点类型已经过时,您只能用ts忽略来关闭错误