Open Browser Automatically After ng serve in Angular
In this article
Running ng serve in Angular normally starts the dev server, but you still have to manually open a browser tab and type localhost:4200. This guide shows you how to make the browser open automatically every time you run ng serve, saving you a few seconds on each build. You can achieve this by adding a --open flag to the command, or by configuring Angular CLI defaults in angular.json.
What You'll Need
- An Angular project created with Angular CLI (version 6 or later).
- Node.js and npm installed on your machine.
- A terminal or command prompt.
Step-by-Step
- Open your terminal in the root directory of your Angular project (where
angular.jsonis located). - Run the basic command with the open flag: type
ng serve --open(or the short formng serve -o) and press Enter. The browser will open automatically athttp://localhost:4200once the server is ready. - Make it permanent for all future
ng servecommands (optional but recommended): openangular.jsonin a text editor. - Find the
serveconfiguration: look for"projects"→"your-project-name"→"architect"→"serve"→"options". - Add the
"open"property: inside the"options"object, add"open": true. Save the file. - Test it: run
ng serve(without any flags). The browser should open automatically. - If you use a custom port, add
"port": 4200(or your preferred port) in the same"options"object. This ensures the browser opens the correct URL.
Common Mistakes
- Typo in the flag: using
--openinstead of--open(with two dashes) or using-oincorrectly. The correct forms are--openor-o. - Editing the wrong
angular.json: make sure you're editing the project-levelangular.json, not the workspace root or a different project's file. - Adding the
openproperty to the wrong section: it must be underserve→options, not underbuildortest. - Forgetting to save the file: after editing
angular.json, save it before runningng serve. - Using an older Angular version: if you're on Angular CLI 5 or earlier, the configuration is in
.angular-cli.jsonand the property is"defaults": { "serve": { "open": true } }.
Tips
- Use the short flag:
ng serve -ois faster to type than--open. - Combine with other flags: e.g.,
ng serve -o --port 4300will open the browser atlocalhost:4300. - Set a default port: if you always use the same port, add
"port": 4200inangular.jsonso you don't need to specify it each time. - Use npm scripts: add a script in
package.jsonlike"start": "ng serve -o"and then runnpm start. - For Angular CLI 14+, you can also use
ng serve --open --host 0.0.0.0to open on a network-accessible address if needed.
Troubleshooting
- Browser doesn't open: check if you're using the correct flag (
--openor-o). Also check that the server actually started successfully (look for "Compiled successfully" in the terminal). If there are compilation errors, the browser won't open. - Browser opens but shows an error: make sure the port is correct. If you have multiple projects, the default port might be different. Check the terminal output for the actual URL.
- Changes to
angular.jsonnot taking effect: restart the terminal or runng serveagain. Sometimes the CLI caches configuration; a fresh run should pick it up. - Using a proxy or firewall: if the browser opens but can't connect, check your firewall settings or proxy configuration. Localhost should be allowed by default.
- If you're using a different browser: Angular CLI uses your default browser. To change it, set the default browser in your OS settings.
Frequently Asked Questions
Does ng serve --open work on all operating systems?
Yes, it works on Windows, macOS, and Linux. The CLI uses the system's default browser.
Can I make it open a specific URL like localhost:4200/login?
No, the --open flag only opens the root URL. To open a specific route, you'd need to use a custom script or a tool like open after the server starts.
Will this affect production builds?
No, ng serve is only for development. Production builds use ng build, which doesn't open a browser.
What if I have multiple projects in one workspace?
You need to specify the project: ng serve my-project --open. Or edit the angular.json for each project individually.
Can I disable the auto-open if I set it permanently?
Yes, run ng serve --open=false to override the setting for that run. Or remove the "open": true from angular.json.
Does this work with Angular Universal or SSR?
For SSR, you'd use npm run dev:ssr, which may not support the --open flag. Check your project's scripts.