NodeJs 中的 @types/node 包是什么?

Gor*_*don 24 node.js

我正在使用 Node v10,发现使用库时存在依赖问题。

\n

ldapjs图书馆,lib\\url.js使用const url = require('url')然后parsedURL = new url.URL(urlStr)将字符串解析为 URL。

\n

我收到此错误:

\n
Unhandled Rejection (TypeError): url.URL is not a constructor\n
Run Code Online (Sandbox Code Playgroud)\n

用这行代码:parsedURL = new url.URL(ldapurl)在我的 React 项目中。

\n

然而,在我的环境下,'url'实际上指的node_modules\\@types\\node\\url.d.ts是其中不包含任何.URL(str)方法,因此出现错误。

\n

什么是@types/node?与 TypeScript 相关吗?我的项目中没有使用 TypeScript。我可以删除它吗?

\n

如果我在终端中运行 Node,它工作正常:

\n
> const url = require('url')\nundefined\n> let purl = new url.URL("https://google.com")\nundefined\n> console.log(purl)\nURL {\n  href: 'https://google.com/',\n  origin: 'https://google.com',\n  protocol: 'https:',\n  username: '',\n  password: '',\n  host: 'google.com',\n  hostname: 'google.com',\n  port: '',\n  pathname: '/',\n  search: '',\n  searchParams: URLSearchParams {},\n  hash: '' }\n
Run Code Online (Sandbox Code Playgroud)\n

问题是什么?

\n

Tit*_*lum 16

直接取自@types/node npm 包

该包包含 Node.js ( http://nodejs.org/ ) 的类型定义。

该包用于在节点中使用 typescript 时加载所有类型定义。当您添加其他包时,如果默认情况下不包含它们,您还必须添加它们的类型。

举个例子,假设您想在打字稿应用程序中使用lodash,您也需要安装类型定义 ( @types/lodash)。

当你不使用 typescript 时,你不需要担心这一切。