javascript - 安装本地 Node 类型时出现重复标识符

标签 javascript node.js typescript

我尝试在项目上运行 tsc,但收到一条错误消息,指出有两个重复的标识符 URLURLSearchParams .

我在全局范围内安装了 typescript ,并且在我的项目中我有@types/node

但是,当我在项目中安装 @types/node 时,我收到此错误:

../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(13404,11): error TS2300: Duplicate identifier 'URL'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(13420,13): error TS2300: Duplicate identifier 'URL'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(13427,11): error TS2300: Duplicate identifier 'URLSearchParams'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(13454,13): error TS2300: Duplicate identifier 'URLSearchParams'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(15740,13): error TS2300: Duplicate identifier 'URL'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(15741,13): error TS2300: Duplicate identifier 'URLSearchParams'.
../../../../../.nvm/versions/node/v10.0.0/lib/node_modules/typescript/lib/lib.es2016.full.d.ts(16503,11): error TS2300: Duplicate identifier 'URLSearchParams'.
node_modules/@types/node/index.d.ts(2381,15): error TS2300: Duplicate identifier 'URL'.
node_modules/@types/node/index.d.ts(2399,15): error TS2300: Duplicate identifier 'URLSearchParams'.
node_modules/@types/node/index.d.ts(2417,14): error TS2661: Cannot export 'URL'. Only local declarations can be exported from a module.
node_modules/@types/node/index.d.ts(2417,19): error TS2661: Cannot export 'URLSearchParams'. Only local declarations can be exported from a module.

不确定出了什么问题以及为什么会收到此错误。

如果我从项目中删除 Node 类型,我会在其他依赖项中收到错误,指出它找不到nethttp等:

node_modules/@types/uuid/interfaces.d.ts(1,23): error TS2688: Cannot find type definition file for 'node'.
node_modules/@types/uuid/interfaces.d.ts(4,48): error TS2304: Cannot find name 'Buffer'.
node_modules/@types/uws/index.d.ts(7,23): error TS2688: Cannot find type definition file for 'node'.
node_modules/@types/uws/index.d.ts(10,23): error TS2307: Cannot find module 'http'.
node_modules/@types/uws/index.d.ts(11,24): error TS2307: Cannot find module 'https'.
node_modules/@types/uws/index.d.ts(12,22): error TS2307: Cannot find module 'net'.

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "system",
    "strict": true,
    "outFile": "../../lib/client/red5.js",
    "sourceMap": true,
    "declaration": true
  },
  "include": [
    "./Connector.ts",
    "**/*.ts"
  ]
}

package.json中的依赖项

"dependencies": {
  "@types/get-port": "^3.2.0",
  "@types/node": "^10.0.0",
  "@types/uuid": "^3.4.3",
  "@types/uws": "^0.13.2",
  "get-port": "^3.2.0",
  "uuid": "^3.2.1",
  "uws": "^9.148.0"
},
"devDependencies": {
  "uglify-es": "^3.3.9"
}

最佳答案

如 Node.js 10 变更日志中所述,URLURLSearchParams 现在可在全局范围内使用,以模拟浏览器中的环境:

https://github.com/nodejs/node/commit/312414662b

所以我敢打赌,类型定义@types/node尚未更新,无法正确考虑此更改。

关于javascript - 安装本地 Node 类型时出现重复标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50089075/

相关文章:

javascript - 将 CSS cubic bezier 缓动转换为 Javascript

JavaScript:隐藏字段的真实表单重置

javascript - 为什么相同的日期有不同的时间?

javascript - 500 错误 :expected "indent", 但得到 "newline"

javascript - 使用带有 tap 和 map RxJS 运算符的函数时出现 TypeScript 类型错误?

javascript - typescript 的/** @class */有目的吗?

javascript - 返回带有 ascii 值和前一个数组的数字的新数组

node.js - 如何使用旧版本的node.js buildpack?

node.js - 在node.js中使用imagemagick调整图像大小

node.js - Stripe + TypeScript : How to extend @types/stripe definitions for stripe-node?