AngularJs CLI An unhandled exception occurred:catch clause variable is not an Error instance, while creating angular component using ng g c my-compnnt

Viewed 1681

Getting below error while creating component using ng g c my-component

'An unhandled exception occurred: catch clause variable is not an Error instance See "path-to-file\angular-errors.log" for further details.'

And file contains below stack trace: [error] AssertionError [ERR_ASSERTION]: catch clause variable is not an Error instance at assertIsError (C:\Users\sam\node_modules@angular\cli\src\utilities\error.js:16:26) at GenerateCommandModule.runSchematic (C:\Users\sam\node_modules@angular\cli\src\command-builder\schematics-command-module.js:311:43) at async GenerateCommandModule.handler (C:\Users\sam\node_modules@angular\cli\src\command-builder\command-module.js:109:24)

No luck even after updating angular cli

npm uninstall -g angular-cli
npm uninstall --save-dev angular-cli
npm cache verify
npm install -g @angular/cli@latest

below is my package.json file:

{
  "name": "my-sample-frontend",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^14.1.0",
    "@angular/common": "^14.1.0",
    "@angular/compiler": "^14.1.0",
    "@angular/core": "^14.1.0",
    "@angular/forms": "^14.1.0",
    "@angular/platform-browser": "^14.1.0",
    "@angular/platform-browser-dynamic": "^14.1.0",
    "@angular/router": "^14.1.0",
    "rxjs": "~7.5.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.1.0",
    "@angular/cli": "~14.1.0",
    "@angular/compiler-cli": "^14.1.0",
    "@types/jasmine": "~4.0.0",
    "jasmine-core": "~4.2.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.0.0",
    "typescript": "~4.7.2"
  }
}
6 Answers

The issue is most likely due to the fact that you have two modules in src/app. So when you try to generate the component, the angular cli does not know where to register this new component.

And that is why the --skip-import option was a workaround for the issue. However the proper way to handle this would be specifying the module in the ng g c something like this:

ng g c my-component --module=app.module

And the problem is not happening anymore and you register your component in the module as it is intended

Yesterday i had the same problems. I still don`t know why this happen. But when I use --skip-import as parameter the command is running without any errors.

example:

ng g c modules/bla/component/test --skip-import 

This will create a new component with the name test in the folder modules/bla/component (relative to the current path) and skip all the imports in the closest module.

Don`t forget to add the component in the declations array in the linked module file.

I had the same issue.

I found that if the file is named with dots it gives error, for example: src/app/application.material.module.ts or src/app/application-material.module.ts

If you change the name to src/app/application-material-module.ts it works.

Regards!

I tried your command it's working for me.
Please check your path where you fire this command: ng g c my-component

ADD THE MODULE FLAG (--module=app) TO THE COMMAND

ng g c test2 --module=app
Related