【问题标题】:ng compiler won't see node_module (Error "Cannot find name 'module'")ng 编译器不会看到 node_module(错误“找不到名称‘模块’”)
【发布时间】:2019-07-09 11:33:21
【问题描述】:

错误:

src/app/app.component.ts:4:12 - 错误 TS2591:找不到名称 '模块'。您需要为节点安装类型定义吗?尝试npm i @types/node,然后将node 添加到 tsconfig 中的 types 字段。

4 moduleId:module.id,

当我尝试运行“ng build”以编译我的 Angular 文件时,每次出现“module”var 使用时都会出错。我确定模块在那里,我可以在 node_modules 文件夹中看到它,也可以 ctrl+单击它,然后打开带有模块 var 的文件。

我尝试删除整个 node_modules 目录并重新安装。

我没有动过我的 tsconfig.json,因为我只是在学习到目前为止,但这里是:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

这里是 ts 组件中的使用示例:

import { Component } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html'
})

export class AppComponent { }

也许这与我的项目结构有关,因为我不太确定要使用什么项目结构。我正在尝试构建 spring + angular 应用程序,具有以下结构:

structure

Angular 应用的结构应该没问题,因为它是由 ng new 命令生成的

angular.json 中的输出路径设置为“outputPath”:“../public”,应该在资源目录中。

请问你有什么问题吗?

【问题讨论】:

    标签: angular typescript npm


    【解决方案1】:

    您可以在组件装饰器中移除模块属性。你不再需要写@Component({ moduleId: module.id }),也不应该,module.id 是由 webpack 自动添加的。也许您在这里指的是旧教程或代码,这就是为什么 module.id 仍然存在于此的原因。

    更多信息请参考此答案 - Angular - What is the meanings of module.id in component?

    【讨论】:

    • 非常感谢,我不知道,我删除了module.id,它工作得很好。我还找到了我在此处发布的其他解决方法,但这似乎是正确的解决方案。
    【解决方案2】:

    我通过从 tsconfig.app.json 中删除以下内容来解决此问题:

    "compilerOptions": {
      "outDir": "./out-tsc/app",
      "types": []
    },
    

    并将其添加到 compilerOptions 下的 tsconfig.json 中

    "types": ["node"]
    

    似乎编译器需要这个 "types": ["node"] 选项才能找到模块,而且似乎默认情况下这个选项在 tsconfig.app.json 中被覆盖..

    如果有人对我有很好的解释,我不会在几天内结束这个问题:-)

    //编辑:对此有更好的解决方案,我检查了答案。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 2016-11-12
      • 2019-01-27
      • 2019-02-21
      • 1970-01-01
      • 2016-05-08
      • 2019-07-03
      相关资源
      最近更新 更多