Generator-jhipster: error with generating new component by angular-cli

Created on 20 Oct 2017  ·  24Comments  ·  Source: jhipster/generator-jhipster


Trying generate new angular component by follow command:
$ ng g c sidebar
I got error message:
Error: Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule.
Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule.

When I try generate new component without import ( -skip-import option)
new component added with wrong path:
$ ng g c sidebar -skip-import
create src/main/webapp/src/main/webapp/app/sidebar/sidebar.component.html (26 bytes)
create src/main/webapp/src/main/webapp/app/sidebar/sidebar.component.ts (245 bytes)

JHipster Version(s)
[email protected] JHIPSTER-projects\demoissue
`-- [email protected]
##### **JHipster configuration, a `.yo-rc.json` file generated in the root folder**
.yo-rc.json file
                                                                                                                        
{                                                                                                                            
  "generator-jhipster": {                                                                                                    
    "promptValues": {                                                                                                        
      "packageName": "com.fiveplus.cosmo"                                                                                    
    },                                                                                                                       
    "jhipsterVersion": "4.10.0",                                                                                             
    "baseName": "demoissue",                                                                                                 
    "packageName": "com.fiveplus.cosmo",                                                                                     
    "packageFolder": "com/fiveplus/cosmo",                                                                                   
    "serverPort": "8080",                                                                                                    
    "authenticationType": "jwt",                                                                                             
    "hibernateCache": "ehcache",                                                                                             
    "clusteredHttpSession": false,                                                                                           
    "websocket": false,                                                                                                      
    "databaseType": "sql",                                                                                                   
    "devDatabaseType": "postgresql",                                                                                         
    "prodDatabaseType": "postgresql",                                                                                        
    "searchEngine": false,                                                                                                   
    "messageBroker": false,                                                                                                  
    "serviceDiscoveryType": false,                                                                                           
    "buildTool": "maven",                                                                                                    
    "enableSocialSignIn": false,                                                                                             
    "enableSwaggerCodegen": false,                                                                                           
    "jwtSecretKey": "replaced-by-jhipster-info",                                                                             
    "clientFramework": "angularX",                                                                                           
    "useSass": false,                                                                                                        
    "clientPackageManager": "yarn",                                                                                          
    "applicationType": "monolith",                                                                                           
    "testFrameworks": [],                                                                                                    
    "jhiPrefix": "jhi",                                                                                                      
    "enableTranslation": false                                                                                               
  }                                                                                                                          
}                                                                                                                            

JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory


JDL entity definitions

                                                                                                                        

Environment and Tools

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

git version 2.13.0.windows.1

node: v6.11.3

npm: 5.4.2

bower: 1.8.0

yeoman: 2.0.0

yarn: 1.0.2

Browsers and Operating System

OS: windows 7

Most helpful comment

If you are working in your current project directory, just change it to /src/app and then run the command 'ng g c your-component' and your component will be generated. It worked for me.

All 24 comments

It looks you are not in the right directory -> in which directory do you run those commands?

i'm in project's root directory JHIPSTER-projects/demoissue
$ pwd
/e/my_path/JHIPSTER-projects/demoissue

@SeitArt that should be good, I need to test

I just tested, no problem at all... Probably a version error - can you do a ng --version? I'm using 4.3.2.
I'm doing this on the master branch but I don't think we changed anything recently there.

I'm closing as I can't reproduce.

my ng version
$ ng --version
@angular/cli: 1.4.2
node: 6.11.3
os: win32 x64
@angular/common: 4.3.2
@angular/compiler: 4.3.2
@angular/core: 4.3.2
@angular/forms: 4.3.2
@angular/http: 4.3.2
@angular/platform-browser: 4.3.2
@angular/platform-browser-dynamic: 4.3.2
@angular/router: 4.3.2
@angular/cli: 1.4.2
@angular/compiler-cli: 4.3.2
typescript: 2.5.2

That's strange I had 4.3.2 - is it installed globally?

i reinstalled angular-cli to 1.4.9

npm install -g @angular/cli
$ ng --version
@angular/cli: 1.4.9
node: 6.11.3
os: win32 x64
@angular/common: 4.3.2
@angular/compiler: 4.3.2
@angular/core: 4.3.2
@angular/forms: 4.3.2
@angular/http: 4.3.2
@angular/platform-browser: 4.3.2
@angular/platform-browser-dynamic: 4.3.2
@angular/router: 4.3.2
@angular/cli: 1.4.9
@angular/compiler-cli: 4.3.2
typescript: 2.5.2

i have the same error with ng g c sidebar

Error: Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule.
Could not find an NgModule for the new component. Use the skip-import option to skip importing components in NgModule.

As Julien, I can't reproduce.
But after investigating, the problem comes directly from angular/cli: look at this ticket https://github.com/angular/angular-cli/issues/7960 -> it's related to Windows7 and the shell you used to execute the command

As it's not on our side, I'm closing this.

in .angular-cli.json in part "apps" replace "./src" to "src"

"apps": [ { "root": "src", "outDir": "dist",

I have the same message error in Windows 10. I have tested in MacOS (10.12.5) and it works

in .angular-cli.json in part "apps" replace "./src" to "src"

Just want to let everyone know that was my issue as well. I'm on Amazon Linux (using c9)

Hello I got the same error:
"ng generate component home
Could not find an NgModule. Use the skip-import option to skip importing in NgModule."

I got this error using Angular 6 and this error is why I changed the root value in angular.json, it was:
root: "src", I changed again it to root: "" and the error was fixed

If you are working in your current project directory, just change it to /src/app and then run the command 'ng g c your-component' and your component will be generated. It worked for me.

why can we just run the command in project directory? my angular.json root is "root":"" but it still can generate a new component when i working in the project directory

You can use "ng new you_project_name" copy angular.json paste to your original project directory.

If you're on Angular 6, in root folder of your project

cd/src/app then ng g c test --spec=false

I upgraded everything for my project and uninstalled/installed everything couple of time to get this issue fixed, but no luck. On creating component using Angular CLI, getting the following error - "Could not find an NgModule. Use the skip-import option to skip importing in NgModule".
Versions on my system are - ng --version
Angular CLI: 6.0.8
Node: 8.11.3
OS: win32 x64
Angular: 6.0.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package Version

@angular-devkit/architect 0.6.8
@angular-devkit/build-angular 0.6.8
@angular-devkit/build-optimizer 0.6.8
@angular-devkit/core 0.6.8
@angular-devkit/schematics 0.6.8
@angular/cli 6.0.8
@ngtools/webpack 6.0.8
@schematics/angular 0.6.8
@schematics/update 0.6.8
rxjs 6.2.2
typescript 2.7.2
webpack 4.8.3

Exception -

ng g c favourite

Could not find an NgModule. Use the skip-import option to skip importing in NgModule.

Angular.json file code here -

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

What could be the issue? Any help please! Thanks

Can confirm this issue, generating components is partly broken with 6.0.8, it seems to work when you navigate to _src/app_.

you can still do it from the root directory of the project, if you follow these steps

project name is the one in angular.json under "projects"

If you are working in your current project directory, just change it to /src/app and then run the command 'ng g c your-component' and your component will be generated. It worked for me.

On doing this, it shows:
Error: Path "" must be absolute.
Path "" must be absolute.

I was using ionic and tried everything. But this is the only thing that worked.
Specifying the root module like this...

ionic g page page-name --module app.module.ts

where g can be generate and page can be component too.

Notice the syntax of --module, because for me --module='app.module.ts' didn't work.
Hope this helps someone.

when I try to pull other's code, ionic-cli or ng-cli will generate parts of fill such as *-base*-ours*-theirs. I delete them and I can use ng g c * successfully.

If you cannot generate a new component try the following:
Open app.module.ts the main entry point of your angular app project
and make sure you have done the iports as follows:

import { NgModule } from '@angular/core';
...
...
...

imports: [
NgModule,

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SudharakaP picture SudharakaP  ·  3Comments

DanielFran picture DanielFran  ·  3Comments

marcelinobadin picture marcelinobadin  ·  3Comments

pascalgrimaud picture pascalgrimaud  ·  3Comments

trajakovic picture trajakovic  ·  4Comments