48

I don't want to use serve, I know it watches for changes, builds and serves. I want to build upon changes. According to "ng help", build takes parameter --watch

ng build Builds your app and places it into the output path (dist/ by default). --watch (Boolean) (Default: false) aliases: -w --watcher (String)

I tried both -w and --watcher but it just gives error.

>ng build -w

Path must be a string. Received null
Matthew Green
  • 10,161
  • 4
  • 36
  • 54
nurp
  • 1,239
  • 2
  • 14
  • 23

4 Answers4

67

I don´t know if it´s a bug or just not documented, but it seems that you need to add a output path for watching with ng build -o dist -w while dist is your output path.

Update:

The command is now: ng build -op dist -w

Update 2:

The command is now: ng build --output-path dist --watch

Stefan Negele
  • 1,092
  • 10
  • 10
  • right, it is a known issue: https://github.com/DavideViolante/Angular2-Full-Stack/issues/15 so this solves the problem: ng build -o dist -w – nurp Oct 24 '16 at 19:26
  • Thanks for the link. I updated my answer with your code snippet. – Stefan Negele Oct 24 '16 at 19:31
  • 2
    @StefanNegele Angular now has output path configuration in `angular.json` too, named as `outputPath`, under `options` variable. – Saddam Pojee Jan 04 '19 at 14:57
37

ng build --watch just worked for me

And if you are using npm run build update the package.json file as

"scripts":{"build":"ng build --watch"}

and run npm run build as usual

make sure that outDir param of your app is correctly set in your angular-cli.json

HexaCrop
  • 3,863
  • 2
  • 23
  • 50
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
7

The ng build --watch looks for the path: dist to watch the changes. But as per the new version of the Angular, the default output path will be dist/<project-name>.

So you need to mention the output directory through command line like

ng build --output-path dist --watch

or

you can change the default location in angular.json... -> options -> outputPath: dist/<project-name> to dist and simply run ng build --watch

Irshad
  • 488
  • 5
  • 6
0

The command to build & watch AngularJs: ng build --poll=2000 --watch

Muhammad Usama
  • 151
  • 1
  • 13