我想用5000而不是4200。
我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:
{
"port": 5000
}
但我的应用程序仍然运行在4200,而不是5000
我想用5000而不是4200。
我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:
{
"port": 5000
}
但我的应用程序仍然运行在4200,而不是5000
当前回答
你也可以这样写:ng serve -p 5000
其他回答
在angular项目中更改端口号的方法:
Node_modules \ @angular-devkit\build-angular\src\dev-server\schemaJson文件
"port": {
"type": "number",
"description": "Port to listen on.",
"default": <<port number>> /* write your required port number here */
}
使用以下命令运行项目
ng serve --port <<port number>> --open
在角。json文件
"server": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "projectname:build",
"port": 5000 /* add this part */
}
adding this part and starting the server.
在CLI的最新版本(我使用的是6.0.1)中,情况似乎发生了变化。我可以通过在项目的angular.json中添加一个port选项来改变ng serve使用的默认端口:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"projects": {
"my-project": {
"architect": {
"serve": {
"options": {
"port": 4201
}
}
}
}
}
}
(本例中只显示相关属性。)
在Angular中,我们有两种改变默认端口号的方法。
cli命令的第一种方式:
ng serve --port 2400 --open
第二种方法是在以下位置进行配置:ProjectName\node_modules\@angular-devkit\build-angular\src\dev-server\schema.json。
在模式中进行更改。json文件。
{
"title": "Dev Server Target",
"description": "Dev Server target options for Build Facade.",
"type": "object",
"properties": {
"browserTarget": {
"type": "string",
"description": "Target to serve."
},
"port": {
"type": "number",
"description": "Port to listen on.",
"default": 2400
},
在包中。Json,查找ng serve命令。 将ng serve命令替换为ng serve——port 4201。 然后在终端使用npm启动或yarn启动。 它应该会起作用。
如果你有多个Angular app,你可以为它们指定不同的端口,并同时运行它们。
如果你想使用NodeJS环境变量来设置Angular CLI开发服务器的端口值,可以采用以下方法:
创建NodeJS脚本,该脚本可以访问环境变量(比如DEV_SERVER_PORT),并可以运行ng serve,使用该变量的——port参数值(child_process可能是我们的朋友):
const child_process = require('child_process');
const child = child_process.exec(`ng serve --port=${process.env.DEV_SERVER_PORT}`);
child.stdout.on('data', data => console.log(data.toString()));
更新包。Json提供这个脚本通过NPM运行 不要忘记设置DEV_SERVER_PORT环境变量(可以通过dotenv完成)
下面是对这种方法的完整描述:如何通过.env更改Angular CLI开发服务器端口。