我想用5000而不是4200。

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

{
   "port": 5000
}

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


当前回答

转到这个文件

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

搜索端口

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

更改默认值并重新启动服务器

其他回答

如果你想使用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开发服务器端口。

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

在package.json中设置你的首选项

-o或——open是在浏览器中自动运行应用程序

使用NPM start或ng serve运行应用程序

"scripts": {
"start": "ng serve --port 4500 -o"
}

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

没有人更新了最新的Angular CLI的答案。使用最新的Angular CLI

使用最新版本的angular-cli。Json重命名为angular。Json,你可以通过编辑angular改变端口。json文件 现在您可以为每个“项目”指定一个端口。

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

点击这里阅读更多信息