【问题标题】:angular-cli server - how to specify default portangular-cli 服务器 - 如何指定默认端口
【发布时间】:2016-09-06 09:25:54
【问题描述】:

使用 angular-cli 和 ng serve 命令,如何指定默认端口,这样我就不需要每次都手动传递 --port 标志?

我想更改默认端口 4200。

【问题讨论】:

    标签: angular angular-cli


    【解决方案1】:

    更新 @angular/cli@9.x: 及以上

    angular.json 中,您可以为每个“项目”指定一个端口

    "projects": {
        "my-cool-project": {
            ... rest of project config omitted
            "architect": {
                "serve": {
                    "options": {
                        "port": 1337
                    }
                }
            }
        }
    }
    

    所有可用选项:

    https://angular.io/guide/workspace-config#project-tool-configuration-options

    或者,您可以在每次运行 ng serve 时指定端口,如下所示:

    ng serve --port 1337

    使用这种方法,您可能希望将其放入 package.json 中的脚本中,以便每次运行/与团队中的其他人共享配置更容易:

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

    旧版:

    @angular/cli final 更新:

    angular-cli.json 中可以默认指定端口:

    "defaults": {
      "serve": {
        "port": 1337
      }
    }
    

    旧版:

    在 angular-cli@1.0.0-beta.22-1 中测试

    angular-cli 中的服务器来自ember-cli 项目。 要配置服务器,请在项目根目录中创建一个.ember-cli 文件。在其中添加您的 JSON 配置:

    {
       "port": 1337
    }
    

    重新启动服务器,它将在该端口上提供服务。

    这里指定了更多选项: http://ember-cli.com/#runtime-configuration

    {
      "skipGit" : true,
      "port" : 999,
      "host" : "0.1.0.1",
      "liveReload" : true,
      "environment" : "mock-development",
      "checkForUpdates" : false
    }
    

    【讨论】:

    【解决方案2】:

    在 Angular 2 cli@2.3.1 中,

    要在不同的端口上运行新项目,一种方法是在运行 ng serve 命令时指定端口。

    ng serve --port 4201
    

    或者其他方式,您可以编辑您的 package.json 文件脚本部分并将端口附加到您的 start 变量,如下所述,然后只需运行“npm start”

     "scripts": {
    
        "ng": "ng",
        "start": "ng serve --port 4201",
        ... : ...,
        ... : ....
    }
    

    这种方式在您不需要每次都明确定义端口的情况下要好得多。

    【讨论】:

      【解决方案3】:

      改用 npm 脚本...编辑您的 package.json 并将命令添加到脚本部分。

      {
        "name": "my new project",
        "version": "0.0.0",
        "license": "MIT",
        "angular-cli": {},
        "scripts": {
          "ng": "ng",
          "start": "ng serve --host 0.0.0.0 --port 8080",
          "lint": "tslint \"src/**/*.ts\" --project src/tsconfig.json --type-check && tslint \"e2e/**/*.ts\" --project e2e/tsconfig.json --type-check",
          "test": "ng test",
          "pree2e": "webdriver-manager update --standalone false --gecko false",
          "e2e": "protractor"
        },
        "private": true,
        "dependencies": {
          "@angular/common": "^2.3.1",
          "@angular/compiler": "^2.3.1",
          "@angular/core": "^2.3.1",
          "@angular/forms": "^2.3.1",
          "@angular/http": "^2.3.1",
          "@angular/platform-browser": "^2.3.1",
          "@angular/platform-browser-dynamic": "^2.3.1",
          "@angular/router": "^3.3.1",
          "core-js": "^2.4.1",
          "rxjs": "^5.0.1",
          "ts-helpers": "^1.1.1",
          "zone.js": "^0.7.2"
        },
        "devDependencies": {
          "@angular/compiler-cli": "^2.3.1",
          "@types/jasmine": "2.5.38",
          "@types/node": "^6.0.42",
          "angular-cli": "1.0.0-beta.26",
          "codelyzer": "~2.0.0-beta.1",
          "jasmine-core": "2.5.2",
          "jasmine-spec-reporter": "2.5.0",
          "karma": "1.2.0",
          "karma-chrome-launcher": "^2.0.0",
          "karma-cli": "^1.0.1",
          "karma-jasmine": "^1.0.2",
          "karma-remap-istanbul": "^0.2.1",
          "protractor": "~4.0.13",
          "ts-node": "1.2.1",
          "tslint": "^4.3.0",
          "typescript": "~2.0.3"
        }
      }
      

      然后执行npm start

      【讨论】:

      • 这是永久的正确方法;所以你不必每次都为 --port 指定命令行参数。此答案中的 --host 参数也是通过 Vagrant(或任何远程系统)运行 Angular 4 所必需的,因此这会用一块石头杀死 2 只鸟。
      【解决方案4】:

      您现在可以在默认值下的 .angular-cli.json 中指定端口:

      "defaults": {
        "styleExt": "scss",
        "serve": {
          "port": 8080
        },
        "component": {}
      }
      

      在 angular-cli v1.0.6 中测试

      【讨论】:

      • 谢谢!!!同样的事情已经用 @angular/cli: 1.3.0 进行了测试。
      【解决方案5】:

      对于@angular/cli v6.2.1

      项目配置文件angular.json能够处理多个项目(工作区),可以单独服务。

      ng config projects.my-test-project.targets.serve.options.port 4201

      my-test-project 部分是您使用ng new 命令设置的项目名称,如下所示:

      $ 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/ **
      

      旧版:

      我通常使用ng set 命令来更改项目级别的 Angular CLI 设置。

      ng set defaults.serve.port=4201
      

      它会更改您的 .angular.cli.json 并将端口设置添加为 it mentioned earlier

      在此更改之后,您可以简单地使用ng serve,它会使用首选端口,而无需每次都指定它。

      【讨论】:

      • 在 Angular 6.0 中,我得到 'set/get has been deprecated for the config command',所以这不再有效。
      【解决方案6】:

      Angular CLI: 7.1.4而言,有两种常见的方法可以实现更改默认端口

      没有。 1

      angular.json 中,将--port 选项添加到serve 部分并使用ng serve 启动服务器。

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

      没有。 2

      package.json 中,将--port 选项添加到ng serve 并使用npm start 启动服务器。

        "scripts": {
          "ng": "ng",
          "start": "ng serve --port 8000",
          "build": "ng build",
          "test": "ng test",
          "lint": "ng lint",
          "e2e": "ng e2e"
        }, 
      

      【讨论】:

        【解决方案7】:

        elwyn 提供的答案是正确的。但你也应该更新 e2e 的量角器配置。

        在 angular.json 中,

        "serve": {
              "builder": "@angular-devkit/build-angular:dev-server",
              "options": {
                "port": 9000,
                "browserTarget": "app:build"
              }
            }
        

        在 e2e/protractor.conf.js 中

        exports.config = {
            allScriptsTimeout: 11000,
            specs: [
                './src/**/*.e2e-spec.ts'
            ],
            capabilities: {
                'browserName': 'chrome'
            },
            directConnect: true,
            baseUrl: 'http://localhost:9000/'
        }
        

        【讨论】:

          【解决方案8】:

          当你想使用 NodeJS 环境变量来指定 Angular CLI 开发服务器端口时,可能会有这种情况。一种可能的解决方案是将 CLI 开发服务器运行到单独的 NodeJS 脚本中,该脚本将读取端口值(例如从 .env 文件中)并使用它执行 ng serveport 参数:

          // run-env.js
          const dotenv = require('dotenv');
          const child_process = require('child_process');
          
          const config = dotenv.config()
          const DEV_SERVER_PORT = process.env.DEV_SERVER_PORT || 4200;
          
          const child = child_process.exec(`ng serve --port=${DEV_SERVER_PORT}`);
          child.stdout.on('data', data => console.log(data.toString()));
          

          然后你可以 a) 直接通过 node run-env 运行这个脚本,b) 通过更新 package.json 通过 npm 运行它,例如

          "scripts": {
            "start": "node run-env"
          }
          

          run-env.js 应该提交给 repo,.env 不应该。可以在这篇文章中找到有关该方法的更多详细信息:How to change Angular CLI Development Server Port via .env

          【讨论】:

            【解决方案9】:

            对于 Angular 版本 12 这对我有用。在angular.json这样更新

            "serve": {
                      "builder": "@angular-devkit/build-angular:dev-server",
                      "configurations": {
                        "production": {
                          "browserTarget": "angular-website:build:production"
                        },
                        "development": {
                          "browserTarget": "angular-website:build:development",
                          "port": 5000
                        }
                      },
                      "defaultConfiguration": "development"
                    },
            

            【讨论】:

              猜你喜欢
              • 2022-12-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-07-01
              • 2016-05-02
              • 2018-11-04
              相关资源
              最近更新 更多