3

I created a new Angular 10 app on a git feature branch. I set up the basic framework for it, then merged that feature branch into release. I created a new feature branch from release to start working on an enhancement and when I tried to run ng serve, I got this error:

An unhandled exception occurred: Cannot find module '@angular-devkit/build-angular/package.json'

Looking at this SO answer (https://stackoverflow.com/a/51581991/787958), the solution was to run npm update. This worked for me, but it seems like a major pain to need to do this on every new git branch. Is there something I can change in the gitignore file so that this is not necessary or is this just a standard pain that everyone has to deal with?

ScubaSteve
  • 7,724
  • 8
  • 52
  • 65
  • 1
    did you by accident remove node_modules directory ?? xD – Antoniossss Oct 20 '20 at 16:10
  • No, but since you said that, I looked at the node_modules folder in VS Code and it has gray text while all other folders and files are white text. I wonder if that means that it is not being picked up by git to be checked in. – ScubaSteve Oct 20 '20 at 16:59
  • If your feature branch has a different version of angular and the `node_modules` are not being tracked by git (which it probably shouldn't), then you would need to update the packages by doing `npm install`. I typically just delete the `node_modules` folder and do a clean install just to be on the save side. – mrcolombo Oct 20 '20 at 17:14
  • Just to clarify, the release branch had nothing in it prior to merging feature branch A into it. After the merge, feature branch B was created from release and would be the same version of Angular as feature branch A. (Since it basically is feature branch A.) How to solve the problem isn't the question. As I stated, `npm update` fixed the issue. The question is, is there a way around this so that I don't need to run `npm update` every time I create a new feature branch from release? – ScubaSteve Oct 20 '20 at 17:24
  • it should definetely be commited thus it is git ignored and probably this is why vcs code shows it as gray(i use intellij) – Antoniossss Oct 20 '20 at 17:37

1 Answers1

3

As of why you node_modules is grayed (I saw you comment), it's because it's not tracked by git which is a good thing, because you don't want to save all dependencies to your repository (there's like 50K files in there). This means that if you ever clone the project, you'll have to use npm install.

I believe this would explain your problem. You might have clone the project and so you would have to use npm install to install dependencies. The reason why npm update did work it's because it compares the stable version of all dependencies you have in your package.json and upgrade to that stable version and then it installs all the dependencies.

Hope this help you understand a little better.

Leccho
  • 467
  • 5
  • 23
  • Ok, so I won't track node_modules, good to know. But, you are saying this is normal to need to run `npm install` with every new feature branch? – ScubaSteve Oct 20 '20 at 17:42
  • No I don't think it's. I just think you didn't had node_modules to begin with. It's normal to use `npm install` when you clone your repository. Try to create a new branch and tell me if you still have the problem where you have to use `npm install`. – Leccho Oct 20 '20 at 17:45
  • The repo was completely empty. I created my first branch from release (a.k.a. nothing), then created the app. I ran ng serve plenty of times with the first branch without issue. I committed everything, then merged the feature branch into release. I then created the second feature branch from release. This is when I ran npm update to fix it. I did test creating a third branch from release and ng serve ran without issue. – ScubaSteve Oct 20 '20 at 17:59
  • Thank you for explaining not to track node_modules and why I should use npm install instead of npm update. Just not sure why I needed to do it on the second feature branch. – ScubaSteve Oct 20 '20 at 18:01
  • Yes so maybe a problem occured during installation and some dependencies did not install. Cool that it work as expected now. I think you can mark this as the official answer too. – Leccho Oct 20 '20 at 18:09