'How to automatically make the browser open localhost after ng serve in angular?

Is there any way to make my terminal to open localhost after ng serve command is completed in angular 8.



Solution 1:[1]

Use the following command :

ng serve -o

or

ng serve --open

If you would like to add some option when you want to run your Angular projects such as opening a browser or running your project on a specific port like 4600 or another option you will have a long command

Add the following command in the package.json under the scripts section:

"start-app": "ng serve --open --port 4600"

Finally to run

npm run start-app

UPDATED

Angular's CLI internally uses opn npm package when you pass that -o or --open flag

Currently, the CLI doesn't support passing more options to the opn package, therefore it will open the site using your system's default browser.

Solution 2:[2]

set it up in angular.json

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ng-router:build",
            "open": true,
            "port": 4200
          },
          "configurations": {
            "production": {
              "browserTarget": "ng-router:build:production"
            }
          }
        },

"open": true, this works well

Solution 3:[3]

You can

  1. edit package.json and
  2. add -o switch here "start": "ng serve -o" it is in scripts.

enter image description here

Note: you have to run it using npm like npm run start

Solution 4:[4]

While serving the app just add an extra flag ng serve -o It'll do the job.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 JackChouMine
Solution 3 Dharman
Solution 4 snsakib