angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

ng generate error

Open nginx17 opened this issue 8 years ago • 72 comments

Versions

Angular CLI: 6.0.0 Node: 8.9.1 OS: linux x64 Angular: 6.0.0 ... animations, cdk, cli, common, compiler, compiler-cli, core ... forms, http, language-service, material, platform-browser ... platform-browser-dynamic, platform-server, router

Package Version

angular-devkit/architect 0.6.0 angular-devkit/build-angular 0.6.0 angular-devkit/build-optimizer 0.6.0 angular-devkit/core 0.6.0 angular-devkit/schematics 0.6.0 ngtools/webpack 6.0.0 schematics/angular 0.6.0 schematics/update 0.6.0 rxjs 6.1.0 typescript 2.7.2 webpack 4.6.0

Repro steps

ng g component home

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

ng g component home --skip-import (same as generate everything)

Success generate component but wrong path not inside "src" but create new folder in root "home/user/PATH/app-id/home/home.component.*

screenshot from 2018-05-05 04-30-43

nginx17 avatar May 04 '18 21:05 nginx17

That didn't work for me, hence more digging and now works without moving projects in angular.json.

grimace avatar Jun 25 '18 17:06 grimace

Fix here, everyone.

https://github.com/rafasantos/matchandtrade-web-ui/commit/a5e6b6085157285900c6a3cfdc5e0ceccda3a9fa

dkteam avatar Jun 26 '18 10:06 dkteam

Thanks @dkteam 👏 It worked for me 🎉

ivyblossom avatar Jun 26 '18 14:06 ivyblossom

For me worked that I changed the path. I was in src folder.
cd ../ fixed this :D

ZdebK avatar Jun 27 '18 08:06 ZdebK

@grimace works fine :)

PawelOwczarekFalcon avatar Jul 03 '18 17:07 PawelOwczarekFalcon

@grimace was very helpful, thanks

agroupp avatar Jul 09 '18 12:07 agroupp

Very much like @grimace 's workaround above.

For me, I was able to leave the project order the same in angular.json and only change the e2e project's"root" path.

{
  "projects": {
    "myGreatApp": {
      "root": ""
    },
    "myGreatApp-e2e": {
      "root": "e2e"
      }
  }
}    

"projects"."myGreatApp-e2e"."root" changes from "" to "e2e" allowing the ng g module test command to build the module in the correct location. We don't use e2e so I don't know what else this breaks. Easy enough, as a workaround, to change and put back just before building an new module.

Angular 6.0.6 Angular CLI 6.0.8 angular-devkit/schematics 0.6.8

chrisBosse avatar Jul 17 '18 14:07 chrisBosse

Deleting the "e2e" in angular.json under: "app-e2e": { "root": "", "sourceRoot": "e2e", "projectType": "application"

to

"app-e2e": { "root": "", "sourceRoot": "", "projectType": "application",

fixed my problem!

jgswiss avatar Jul 25 '18 16:07 jgswiss

Same problem on Windows, after upgrading to the latest cli (6.1.1). The solution that worked for me was suggested by @srkt

  1. Open angular.json (it should be right in the root of your app)
  2. Find "projects" property.
  3. Inside the project property you will see two children properties: "yourappname-app" and "yourappname-app-e2e"
  4. Switch their order (cut the "yourname-app-e2e" property and their children, and place them above "yourappname-app" property)
  5. I had to restart VS Code for it to start working (you may need to restart your terminal or whatever you're using)

Eternal21 avatar Jul 27 '18 12:07 Eternal21

I'm not having any problem changing the root folder from src to client with CLI version 6.1 (installed globally) on Windows 10 x64. Here is what I did:

ng new ng-test --style scss --skip-install

Save the patch file root-change.diff:

diff --git a/angular.json b/angular.json
index 56f7e2e..2d30500 100644
--- a/angular.json
+++ b/angular.json
@@ -5,7 +5,7 @@
   "projects": {
     "ng-test": {
       "root": "",
-      "sourceRoot": "src",
+      "sourceRoot": "client",
       "projectType": "application",
       "prefix": "app",
       "schematics": {
@@ -18,16 +18,16 @@
           "builder": "@angular-devkit/build-angular:browser",
           "options": {
             "outputPath": "dist/ng-test",
-            "index": "src/index.html",
-            "main": "src/main.ts",
-            "polyfills": "src/polyfills.ts",
-            "tsConfig": "src/tsconfig.app.json",
+            "index": "client/index.html",
+            "main": "client/main.ts",
+            "polyfills": "client/polyfills.ts",
+            "tsConfig": "client/tsconfig.app.json",
             "assets": [
-              "src/favicon.ico",
-              "src/assets"
+              "client/favicon.ico",
+              "client/assets"
             ],
             "styles": [
-              "src/styles.scss"
+              "client/styles.scss"
             ],
             "scripts": []
           },
@@ -35,8 +35,8 @@
             "production": {
               "fileReplacements": [
                 {
-                  "replace": "src/environments/environment.ts",
-                  "with": "src/environments/environment.prod.ts"
+                  "replace": "client/environments/environment.ts",
+                  "with": "client/environments/environment.prod.ts"
                 }
               ],
               "optimization": true,
@@ -71,17 +71,17 @@
         "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",
+            "main": "client/test.ts",
+            "polyfills": "client/polyfills.ts",
+            "tsConfig": "client/tsconfig.spec.json",
+            "karmaConfig": "client/karma.conf.js",
             "styles": [
-              "src/styles.scss"
+              "client/styles.scss"
             ],
             "scripts": [],
             "assets": [
-              "src/favicon.ico",
-              "src/assets"
+              "client/favicon.ico",
+              "client/assets"
             ]
           }
         },
@@ -89,8 +89,8 @@
           "builder": "@angular-devkit/build-angular:tslint",
           "options": {
             "tsConfig": [
-              "src/tsconfig.app.json",
-              "src/tsconfig.spec.json"
+              "client/tsconfig.app.json",
+              "client/tsconfig.spec.json"
             ],
             "exclude": [
               "**/node_modules/**"
@@ -128,4 +128,4 @@
     }
   },
   "defaultProject": "ng-test"
-}
\ No newline at end of file
+}
diff --git a/client/tsconfig.app.json b/client/tsconfig.app.json
index 8ea061e..7e02a41 100644
--- a/client/tsconfig.app.json
+++ b/client/tsconfig.app.json
@@ -5,7 +5,7 @@
     "types": []
   },
   "exclude": [
-    "src/test.ts",
+    "client/test.ts",
     "**/*.spec.ts"
   ]
 }

Apply the patch file and install vendor:

git apply root-change.diff
npm i

All the following commands works:

ng serve --open
ng build --prod
ng test
ng e2e

Component generation will correctly generate the component in the new root folder:

ng g c home --dry-run
CREATE client/app/home/home.component.html (23 bytes)
CREATE client/app/home/home.component.spec.ts (614 bytes)
CREATE client/app/home/home.component.ts (262 bytes)
CREATE client/app/home/home.component.scss (0 bytes)
UPDATE client/app/app.module.ts (388 bytes)

$ ng g m core --dry-run
CREATE client/app/core/core.module.spec.ts (259 bytes)
CREATE client/app/core/core.module.ts (188 bytes)

gremo avatar Jul 28 '18 09:07 gremo

My fix was fairly simple:

Problem: I was running the command ng generate component components/home (create the "home" component in the components directory) from the location of angular-project/src/.

Solution: Back out into the root of your angular project and specify the path you want the component to generate in like so:

ng generate component src/components/home

The above command should create a "home" component in the components/ directory inside the src/ directory.

Hope that helps!

zachgoll avatar Sep 15 '18 16:09 zachgoll

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.