首页 > 解决方案 > 如何在 Typescript 项目中使用 Node 16 中的计时器/承诺?

问题描述

在Node 16 版本中,我想使用新的 timers/promises API 。我安装Node 16NVM切换到它:

$ 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" }

我不确定是否需要安装 的版本16@types/node但我尝试了(没有成功):

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安装了。

最后,我尝试setTimeout在我的Typescript文件中导入:

import { setTimeout } from 'timers/promises'

但出现错误:

Cannot find module 'timers/promises' or its corresponding type declarations.

我怎样才能解决这个问题?

标签: node.jstypescriptnpm

解决方案


它已于 3 天前在@types/node. 只需运行npm install --save-dev @types/node@latest,或yarn add --dev @types/node@latest。版本为 0。


推荐阅读