User Agents

Web browsers and everyday web tech, explained

Open Browser Automatically After ng serve in Angular

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open your terminal in the root directory of your Angular project (where angular.json is located).
  2. Run the basic command with the open flag: type ng serve --open (or the short form ng serve -o) and press Enter. The browser will open automatically at http://localhost:4200 once the server is ready.
  3. Make it permanent for all future ng serve commands (optional but recommended): open angular.json in a text editor.
  4. Find the serve configuration: look for "projects" → "your-project-name" → "architect" → "serve" → "options".
  5. Add the "open" property: inside the "options" object, add "open": true. Save the file.
  6. Test it: run ng serve (without any flags). The browser should open automatically.
  7. 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 --open instead of --open (with two dashes) or using -o incorrectly. The correct forms are --open or -o.
  • Editing the wrong angular.json: make sure you're editing the project-level angular.json, not the workspace root or a different project's file.
  • Adding the open property to the wrong section: it must be under serve → options, not under build or test.
  • Forgetting to save the file: after editing angular.json, save it before running ng serve.
  • Using an older Angular version: if you're on Angular CLI 5 or earlier, the configuration is in .angular-cli.json and the property is "defaults": { "serve": { "open": true } }.

Tips

  • Use the short flag: ng serve -o is faster to type than --open.
  • Combine with other flags: e.g., ng serve -o --port 4300 will open the browser at localhost:4300.
  • Set a default port: if you always use the same port, add "port": 4200 in angular.json so you don't need to specify it each time.
  • Use npm scripts: add a script in package.json like "start": "ng serve -o" and then run npm start.
  • For Angular CLI 14+, you can also use ng serve --open --host 0.0.0.0 to open on a network-accessible address if needed.

Troubleshooting

  • Browser doesn't open: check if you're using the correct flag (--open or -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.json not taking effect: restart the terminal or run ng serve again. 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.