0

Trying to generate a new component in one of my modules.

I navigate to [project]/src/app and enter

ng g c admin/list where admin is the existing module and list is the name of the new component I want to generate.

Result:

Cannot read property '0' of null

It seems to work with every other module. Must be something in my @ngModule but I can't see anything.

Chris Curnow
  • 643
  • 5
  • 15
  • What is it that you want to accomplish? Do you want to create another directory called `admin` and within that have a component called `list` Or do you just want `list.component` to exist in the already created admin dir? It would help if you provided an idea of what your App structure is and what you want it to become. – Narm Jun 07 '18 at 00:22
  • Sorry, I should have been clearer. I have an existing directory `admin` which has its own `admin.module`. I want to add a new component `list` to this directory and module. It works for other directory/modules but not for this one. – Chris Curnow Jun 07 '18 at 02:04
  • Have you tried using the standard syntax `ng g c list` if you're in the `../src/app/admin` directory? You don't need to include the `/admin` to create the component if you're in that directory already. – Narm Jun 07 '18 at 04:24
  • Thanks @Narm, I hadn't tried that. However I just did and got the same result. Interesting about being able to create the component directly if you are in (for instance) the `/admin` directory. I raised this as an issue back in August last year – https://github.com/angular/angular-cli/issues/7377. It is still listed as Open so I hadn't tried again. – Chris Curnow Jun 08 '18 at 08:14
  • What version of CLI are you using, v6? I apologize I just saw your tag right now, I overlooked that - if you're on v6 I will have to wave the white flag on this one. I'm still running Angular v5 with cli 1.7.3 and I've never hit this issue. – Narm Jun 08 '18 at 19:37

2 Answers2

0

The only thing that 'ng g' should do with your module is to add the component to the declarations list, so have you checked that your module has a declarations' array defined?

Apart from that, which Angular CLI version are you using? Because it should get rid of these cases, maybe upgrading the version would be also a good idea.

Siro
  • 3,179
  • 1
  • 10
  • 10
  • "The only thing that 'ng g' should do with your module is to add the component to the providers list"!?!? This is just plain wrong. Components are declared, services are provided, therefore components are added to `declarations: []` in the module not the providers array. – Narm Jun 07 '18 at 20:06
  • I agree with Narm here. 'ng g c' should generate a component and add it to the module – in the `declarations: []` section. In this module it is not doing either. However, it does in other modules. I will keep investigating. – Chris Curnow Jun 08 '18 at 08:18
  • Typo fixed. Have you checked the important part? – Siro Jun 08 '18 at 10:36
  • Hi Siro, I am using the very latest version of Angular and CLI. Just updated now. Yes the module has a "declarations" array. The module actually works fine. It's just that I can't add a new component to it with "ng g c". The components already there work. The module router works. I can add components to all my other modules. I think I will just have to leave this issue and come back to it when I have more time. – Chris Curnow Jun 14 '18 at 06:35
0

I figured it out today after I saw adding components simply adds them to the "declarations" list.

I had two modules, a sub module, and app.module. I realized I could add a component to my art module without issue, but I got the very unhelpful '0 of null' error when adding a component outside of that main module.

I inspected my app.module and noticed ts.lint was giving me a warning that each line was over 140 characters. I simply moved things around until tslint stopped warning me. Once that was fixed, I was able to create a component.