Angular2-找不到NgModule [英] Angular2 - Could not find an NgModule

查看:92
本文介绍了Angular2-找不到NgModule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我刚刚使用简单的

Hello I've just created a new project from the CLI using the simple

ng new frontend

命令,但是一旦进入项目,我将执行以下操作:

command however once I go into my project to:

cd frontend/src/app

并尝试生成一个像这样的新组件

and try to generate a new component like so

ng generate component about

我在终端机(MacOS)中收到以下错误消息: 找不到NgModule.使用跳过导入"选项可以跳过在NgModule中的导入.

I get the following error message in my terminal(MacOS): Could not find an NgModule. Use the skip-import option to skip importing in NgModule.

我已全局安装Angular 2,并且在运行时安装了

I have installed Angular 2 globally and when I run my

ng --version

我收到以下信息:

Angular CLI: 6.0.0
Node: 8.11.1
OS: darwin x64
Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.6.0
@angular-devkit/core         0.6.0
@angular-devkit/schematics   0.6.0
@schematics/angular          0.6.0
@schematics/update           0.6.0
rxjs                         6.1.0
typescript                   2.7.2

我的Angular.json内部有:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "frontend": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/frontend",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "frontend:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "frontend:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "frontend:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "frontend-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "frontend:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "frontend"
}

并进一步挖掘:package.json

{
  "name": "frontend",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.0",
    "@angular/common": "^6.0.0",
    "@angular/compiler": "^6.0.0",
    "@angular/core": "^6.0.0",
    "@angular/forms": "^6.0.0",
    "@angular/http": "^6.0.0",
    "@angular/platform-browser": "^6.0.0",
    "@angular/platform-browser-dynamic": "^6.0.0",
    "@angular/router": "^6.0.0",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.0",
    "@angular/cli": "~6.0.0",
    "@angular/compiler-cli": "^6.0.0",
    "@angular/language-service": "^6.0.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

我尝试过的事情: 我尝试重新安装Angular.我已经确认我的TypeScript是最新的.我的NPM是最新的,依此类推.我按照Angular.io提供的教程的最基本步骤创建了一个新项目,无济于事.

What I've tried: I have tried reinstalling Angular. I have confirmed my TypeScript is up to date. my NPM is up to date and so on. I've created a new project following the most basic steps of the tutorial provided at Angular.io to no avail.

如果有人可以在这里教我发生了什么问题,我将不胜感激.

If anyone can lecture me on what is going wrong here I'd greatly appreciate it.

起草这篇文章时,我有一个绝妙的主意,那就是通过我的IDE终端运行此过程,并且ng generate component about令人震惊地工作没有问题.但是,好奇并想知道我的本机终端出了什么问题,我想我毕竟会发布.

While drafting this post I had the brilliant idea of running this process through my IDE's terminal and shockingly ng generate component about worked without a problem. However being curious and wanting to know what is wrong with my native terminal I figured I'd post after all.

所以随机的想法突然出现在我的脑海里,简单而简单的文件权限...是的 使用sudo ng g c about起作用.所以我想你有.希望这对外面的人有帮助.

EDIT 2: So random thought popped in my head plain and simple File Permissions...yup using sudo ng g c about worked. So there you have it I suppose. Hopefully this was helpful to someone out there.

推荐答案

您应在应用程序文件夹中运行此命令'ng g c component-name'.

you should run this command 'ng g c component-name' inside is app folder.

1)cd src/app 2)ng g c组件名称

这篇关于Angular2-找不到NgModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆