【问题标题】:Angular 2 - moved to @types giving error "cannot find name 'require'"Angular 2 - 移至@types 给出错误“找不到名称'require'”
【发布时间】:2016-10-19 15:37:07
【问题描述】:

我已将我的项目从旧类型转移到类型发布者。我的大部分依赖项都已解决,除了我不断收到此错误 Cannot find name 'require' 这是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "typeRoots": [
    "./node_modules/@types",
    "./src/custom-typings.d.ts"
  ],
  "types": [
    "core-js",
    "jasmine",
    "node",
    "lodash",
    "jquery",
    "requirejs",
    "angular",
    "googlemaps"
  ]
}

在我的“devdependencies”的 package.json 中,我有这些

"@types/angular": "^1.5.16",
    "@types/core-js": "^0.9.34",
    "@types/googlemaps": "^3.25.35",
    "@types/jasmine": "^2.5.35",
    "@types/jquery": "^2.0.33",
    "@types/lodash": "^4.14.37",
    "@types/node": "^6.0.45",
    "@types/requirejs": "^2.1.28",

我也安装了@types/requirejs...但是这个错误不会消失。 是否有我遗漏或添加不正确的内容?

我正在为我的应用程序使用 webpack 2.1.0.beta.25。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    将您的tsconfig.json 更改为如下typeRootstypescompilerOptions 属性根据documentation

    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "types": [
          "core-js",
          "jasmine",
          "node",
          "lodash",
          "jquery",
          "requirejs",
          "angular",
          "googlemaps"
        ],
        "typeRoots": [
          "./node_modules/@types",
          "./src/custom-typings.d.ts"
        ]
      }
    }
    

    还要检查documentation for types, typeroots and types,它表示If typesRoots is specified, only packages under typeRoots will be included,因此请通过删除typesRoots 或更改typesRoots 属性设置来检查。

    【讨论】:

    • 谢谢,它解决了这个问题。不再需要requirejs。
    猜你喜欢
    • 2018-07-31
    • 2017-09-14
    • 2017-09-07
    • 2018-07-23
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多