Programming

각도 포트를 4200에서 다른 포트로 변경하는 방법

procodes 2020. 5. 21. 21:43
반응형

각도 포트를 4200에서 다른 포트로 변경하는 방법


4200 대신 5000을 사용하고 싶습니다 ..

내가 시도한 것은 루트 이름으로 파일을 만들고 ember-cli아래 코드에 따라 JSON을 넣는 것입니다.

{
   "port": 5000
}

하지만 내 앱은 여전히 ​​5000 대신 4200에서 실행됩니다.


나를 위해 일한 해결책은

ng serve --port 4401    

(4401을 원하는 숫자로 변경할 수 있습니다)

그런 다음 브라우저를 시작하십시오-> http : // localhost : 4401 /

기본적으로 두 개의 응용 프로그램이 있었고 위의 접근 방식을 통해 개발 환경에서 두 응용 프로그램을 동시에 실행할 수 있습니다.


다음 명령을 입력하여 애플리케이션 포트를 변경할 수 있습니다.

ng serve --port 4200

또한 영구 설치를 위해 angular-cli.json 파일을 편집하여 포트를 변경할 수 있습니다

 "defaults": {
    "serve": {
      "port": 8080
    }
  }

nodel_modules/angular-cli/commands/server.js새 버전을 설치할 때 업데이트되므로 변경 하는 것은 좋지 않습니다 angular-cli. 대신 다음 과 같이 ng serve --port 5000지정해야 package.json합니다.

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

--host 127.0.0.1로 호스트를 지정할 수도 있습니다.


최신 버전의 CLI에서 변경 된 것 같습니다 (사용하고 있습니다 6.0.1). 내 프로젝트에 옵션을 ng serve추가하여 사용되는 기본 포트를 변경할 수있었습니다 .portangular.json

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "projects": {
        "my-project": {
            "architect": {
                "serve": {
                    "options": {
                        "port": 4201
                    }
                }
            }
        }
    }
}

이 예제에는 관련 속성 만 표시됩니다.


포트 설정 위치가 두 번 변경되었습니다.

Angular CLI 1을 사용하는 경우

변화 angular-cli.json

{
  "defaults": {
    "serve": {
      "host": "0.0.0.0",
      "port": 5000 
    }
  }
}

최신 Angular CLI를 사용하는 경우

변화 angular.json

"projects": {
    "project-name": {
        ...
        "architect": {
            "serve": {
                "options": {
                  "host": "0.0.0.0",
                  "port": 5000
                }
            }
        }
        ...
    }
}

파일을 변경하지 않고

명령을 실행

ng serve --host 0.0.0.0 --port 5000

아무도 최신 Angular CLI에 대한 답변을 업데이트하지 않았습니다. Angular CLI

With latest version of angular-cli in which angular-cli.json renamed to angular.json , you can change the port by editing angular.json file you now specify a port per "project"

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

Read more about it here


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.


you can also enter the below command in your angular cli where you normally enter npm start

ng serve --host "ip-address" --port "port-number"


  • For Permanent:

    Goto nodel_modules/angular-cli/commands/server.js Search for var defaultPort = process.env.PORT || 4200; and change 4200 to anything else you want.

  • To Run Now:

    ng serve --port 4500 (You an change 4500 to any number you want to use as your port)


you can also write this command: ng serve -p 5000


in angular 2++.

To change port you can run below command in terminal :

ng serve --host 0.0.0.0 --port 5000.

You can edit default port number that is configured in serve.js file.

Path will be project_direcory/node_modules/angular-cli/commands/serve.js.

Search for this line -> var defaultPort = process.env.PORT || 4200;
Replace with this line -> var defaultPort = process.env.PORT || 5000;


simply run ng serve --port 5000 --open


Run below command for other than 4200

ng serve --port 4500

In angular.json:

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

I am using angular-cli. This worked for me.


Although there are already numerous valid solutions in the above answers, here is a visual guide and specific solution for Angular 7 projects (possibly earlier versions as well, no guarantees though) using Visual Studio Code. Locate the schema.json in the directories as shown in the image tree and alter the integer under port --> default for a permanent change of port in the browser.

enter image description here


goto this file

node_modules\@angular-devkit\build-angular\src\dev-server\schema.json

and search port

"port": {
  "type": "number",
  "description": "Port to listen on.",
  "default": 4200
},

change default whatever you want and restart the server


Just Run

ng serve --port yourport

Example:

ng serve --port 4401


The code worked for me was as follows

ng serve --port ABCD

For Example

ng serve --port 6300

We have two ways to change default port number in Angular.

First way to cli command:

ng serve --port 2400 --open

Second way is by configuration at the location: ProjectName\node_modules\@angular-devkit\build-angular\src\dev-server\schema.json.

Make changes in schema.json file.

{
 "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
    },

참고URL : https://stackoverflow.com/questions/40717449/how-to-change-angular-port-from-4200-to-any-other

반응형