Using angular-cli with the ng serve
command, how can I specify a default port so I do not need to manually pass the --port
flag every time?
The answer provided by elwyn is correct. But you should also update protractor config for e2e.
In angular.json,
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"port": 9000,
"browserTarget": "app:build"
}
}
In e2e/protractor.conf.js
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./src/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
baseUrl: 'http://localhost:9000/'
}
The project configuration file angular.json
is able to handle multiple projects (workspaces) which can be individually served.
ng config projects.my-test-project.targets.serve.options.port 4201
Where the my-test-project
part is the project name what you set with the ng new
command just like here:
$ ng new my-test-project
$ cd my-test-project
$ ng config projects.my-test-project.targets.serve.options.port 4201
$ ng serve
** Angular Live Development Server is listening on localhost:4201, open your browser on http://localhost:4201/ **
Legacy:
I usually use the ng set
command to change the Angular CLI settings for project level.
ng set defaults.serve.port=4201
It changes change your .angular.cli.json and adds the port settings as it mentioned earlier.
After this change you can use simply ng serve
and it going to use the prefered port without the need of specifying it every time.