我想用5000而不是4200。

我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:

{
   "port": 5000
}

但我的应用程序仍然运行在4200,而不是5000


当前回答

在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
                    }
                }
            }
        }
    }
}

(本例中只显示相关属性。)

其他回答

永久的: 转到/ server.js nodel_modules / angular-cli /命令 搜索var defaultPort = process.env.PORT || 4200;把4200改成你想要的任何东西。 现在就跑: ng serve——port 4500(你可以把4500改成任何你想要的端口)

在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.

虽然上面的答案中已经有很多有效的解决方案,但这里是一个使用visual Studio Code的Angular 7项目的可视化指南和具体解决方案(也可能是更早的版本,但不保证)。定位模式。在图像树中显示的目录中添加Json,并更改port——> default下的整数,以永久更改浏览器中的端口。

修改nodel_modules/angular-cli/commands/server.js是个坏主意,因为当你安装新版本的angular-cli时,它会被更新。相反,你应该在package中指定ng serve——port 5000。Json是这样的:

"scripts": {
    "start": "ng serve --port 5000"
}

你也可以用——host 127.0.0.1指定主机

对于角10+:

导航到angular。json文件。 搜索“serve”字段。 在“服务”下面是“选项”字段(如果不存在,添加选项字段)。 将这一行添加到“options”: "port": 4333(你想要的任何端口)。

例子:

        "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "port": 4333
      },
      "configurations": {
        "production": {
          "browserTarget": "dashboard:build:production"
        }
      }
    },