【问题标题】:TypeScript: nested npm package causes errors (Duplicate identifier)TypeScript:嵌套的 npm 包导致错误(重复标识符)
【发布时间】:2017-08-14 04:59:39
【问题描述】:

我有一个项目文件夹,其根级别为node_modules,另一个位于名为functions 的子文件夹中。像这样,

├── functions
│   ├── index.js
│   ├── index.js.map
│   ├── index.ts
│   ├── package.json
│   ├── node_modules
│   └── tsconfig.json
├── package.json
└── node_modules

我想在functions 文件夹中编译打字稿文件,但它一直在查看根 node_modules 并给我错误,

tsc --project functions
functions/node_modules/@types/node/index.d.ts(60,13): error TS2451: Cannot redeclare block-scoped variable 'global'.
functions/node_modules/@types/node/index.d.ts(84,13): error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts(8365,11): error TS2451: Cannot redeclare block-scoped variable 'global'.
node_modules/@types/react-native/index.d.ts(8366,14): error TS2300: Duplicate identifier 'require'.

如何让 typescript 忘记上层 node_modules 或以其他方式解决此问题?

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "CommonJS",
    "outDir": ".",
    "rootDir": ".",
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "experimentalDecorators": true,
    "preserveConstEnums": true,
    "allowJs": true,
    "sourceMap": true
  },
  "filesGlob": [
    "./**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

【问题讨论】:

  • 也许:cd functions && tsctsconfig.json的内容是什么?
  • cd 没有任何区别。添加了 tsconfig

标签: typescript npm


【解决方案1】:
{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

这个解决方案对我有用 我在这里找到了答案 How to block typescript 2.0 from walking up all parent directories while resolving modules?

【讨论】:

    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 2019-02-18
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2016-03-15
    • 2020-07-23
    相关资源
    最近更新 更多