I managed to choose the browser that will be opened, but it wasn't with browserTarget.
Here’s a detailed explanation. All changes are made in the package.json file...
This doesn’t work:
{
// ...
"scripts": {
"start": "ng serve && start http://localhost:4200"
},
// ...
}
ng serve
never finishes, which means that start
will never start. To run ng serve and also start the browser, you need to install two dependencies. Here’s the step-by-step process:
1. Install the dependencies:
npm install npm-run-all wait-on --save-dev
npm-run-all is a library that allows running multiple processes simultaneously, while wait-on monitors the URL to start the browser when it’s ready.
2. Add the desired browser commands to scripts and modify start to run the processes simultaneously:
// ...
"config": {
"serve-url": "http://localhost:4200"
},
"scripts": {
// ...
"ng-serve": "ng serve",
"start": "run-p ng-serve open-in-chrome",
"open-in-browser": "ver && (wait-on %npm_package_config_serve-url% && start %npm_package_config_serve-url%) || (wait-on $npm_package_config_serve-url && open $npm_package_config_serve-url)",
"open-in-edge": "ver && (wait-on %npm_package_config_serve-url% && start msedge %npm_package_config_serve-url%) || (wait-on $npm_package_config_serve-url && open $npm_package_config_serve-url)",
"open-in-chrome": "ver && (wait-on %npm_package_config_serve-url% && start chrome %npm_package_config_serve-url%) || (wait-on $npm_package_config_serve-url && open chrome $npm_package_config_serve-url)",
"open-in-firefox": "ver && (wait-on %npm_package_config_serve-url% && start firefox %npm_package_config_serve-url%) || (wait-on $npm_package_config_serve-url && open firefox $npm_package_config_serve-url)",
"open-in-opera": "ver && (wait-on %npm_package_config_serve-url% && start opera %npm_package_config_serve-url%) || (wait-on $npm_package_config_serve-url && open opera $npm_package_config_serve-url)",
// ...
},
// ...
The ver
command, as far as I know, only exists on Windows. This means that if it exists it will run the first part with the Windows variables, otherwise it will use the Linux/Mac variables.
3. Now just modify the start script to open-in-xxx like:
"start": "run-p ng-serve open-in-firefox"
Maybe there are ways to refactor this code with variables or cross-env
sources:
npm npm-run-all
npm wait-on