我正在使用 Node v10,发现使用库时存在依赖问题。
\n在ldapjs图书馆,lib\\url.js使用const url = require('url')然后parsedURL = new url.URL(urlStr)将字符串解析为 URL。
我收到此错误:
\nUnhandled Rejection (TypeError): url.URL is not a constructor\nRun Code Online (Sandbox Code Playgroud)\n用这行代码:parsedURL = new url.URL(ldapurl)在我的 React 项目中。
然而,在我的环境下,'url'实际上指的node_modules\\@types\\node\\url.d.ts是其中不包含任何.URL(str)方法,因此出现错误。
什么是@types/node?与 TypeScript 相关吗?我的项目中没有使用 TypeScript。我可以删除它吗?
如果我在终端中运行 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: '' }\nRun Code Online (Sandbox Code Playgroud)\n问题是什么?
\nTit*_*lum 16
直接取自@types/node npm 包:
该包包含 Node.js ( http://nodejs.org/ ) 的类型定义。
该包用于在节点中使用 typescript 时加载所有类型定义。当您添加其他包时,如果默认情况下不包含它们,您还必须添加它们的类型。
举个例子,假设您想在打字稿应用程序中使用lodash,您也需要安装类型定义 ( @types/lodash)。
当你不使用 typescript 时,你不需要担心这一切。
| 归档时间: |
|
| 查看次数: |
37142 次 |
| 最近记录: |