簡體   English   中英

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

[英]What is @types/node package in NodeJs?

我正在使用 Node v10,發現在使用庫時存在依賴性問題。

ldapjs庫中, lib\url.js使用const url = require('url')然后parsedURL = new url.URL(urlStr)將字符串解析為 ZE6B391A38D2C4D857A8B

我收到此錯誤:

Unhandled Rejection (TypeError): url.URL is not a constructor

使用這行代碼: parsedURL = new url.URL(ldapurl)在我的 React 項目中。

但是,在我的環境下, 'url'實際上是指node_modules\@types\node\url.d.ts不包含任何.URL(str)方法,因此是錯誤的。

什么是@types/node 是否與 TypeScript 有關? 我不在我的項目中使用 TypeScript。 我可以刪除它嗎?

如果我在終端中運行 Node,它可以正常工作:

> const url = require('url')
undefined
> let purl = new url.URL("https://google.com")
undefined
> console.log(purl)
URL {
  href: 'https://google.com/',
  origin: 'https://google.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'google.com',
  hostname: 'google.com',
  port: '',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: '' }

問題是什么?

直接取自@types/node npm package

此 package 包含 Node.js ( http://nodejs.org/ ) 的類型定義。

在節點中使用 typescript 時,此 package 用於加載所有類型定義。 添加其他包時,如果默認情況下不包含它們,則還必須添加它們的類型。

例如,假設您想在 typescript 應用程序中使用lodash ,您還需要安裝類型定義 ( @types/lodash )。

當您不使用 typescript 時,您無需擔心這一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM