我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。

在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006


当前回答

对于windows,使用cmd

set PORT=3001 && npm start

其他回答

在调用命令时给出端口号,而不需要更改应用程序代码或环境文件中的任何内容,怎么样?这样就可以从几个不同的端口运行和服务相同的代码库。

如:

$ export PORT=4000 && npm start

您可以用您喜欢的端口号来代替上面的示例值4000。

您可以在包中使用相同的表达式。Json脚本。

如:

"start": "export PORT=4000 response -scripts start"

但对于后者,您将需要更改包。但是,对于前者,除了从命令行调用的端口值外,您不会更改任何内容。

在我的包里换衣服。“export PORT=3001 && react-scripts start”也适用于我,我在macOS 10.13.4上

在你的包裹里。在脚本中使用——port 4000或set port =4000,如下例所示:

包中。json (Windows):

"scripts": {
    "start": "set PORT=4000 && react-scripts start"
}

包中。json (Ubuntu):

"scripts": {
    "start": "export PORT=4000 && react-scripts start"
}

你可以在package.json中修改你的脚本:

- MacOs:

"scripts": {
     "start": "PORT=9002 react-scripts start",
     "build": "react-scripts build",
     ...     
}

在窗户

  "scripts": {
      "start": "set PORT=9002 && react-scripts start",
      "build": "react-scripts build",
      ...
}

对于windows,使用cmd

set PORT=3001 && npm start