我想用5000而不是4200。

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

{
   "port": 5000
}

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


当前回答

对我来说有效的解决方法是

ng serve --port 4401    

(你可以把4401改成任何你想要的号码)

然后启动浏览器-> http://localhost:4401/

基本上,我有两个应用程序,在上述方法的帮助下,现在我能够在我的开发环境中同时运行它们。

其他回答

简单设置在包中。json文件

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

然后运行命令

npm start

您可以输入以下命令更改应用程序端口,

Ng serve——端口4200

此外,对于永久设置,您可以通过编辑angular-cli更改端口。json文件

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

修改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指定主机

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

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

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

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

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