我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。
在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006
我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。
在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006
当前回答
对于windows操作系统,可直接在cmd命令下执行此命令
set PORT=3001 && npm start
其他回答
在你的包裹里。在脚本中使用——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"
}
如果你在Dockerfile中运行npm start,你不能在docker运行中映射端口,比如做一些类似-p 3001:3000的事情,这是有效的:
FROM node
ENV PORT=3001
# whatever here, COPY .. etc.
CMD npm start
或者你可以在docker build中传递端口号作为参数:
FROM node
ARG PORT=${PORT}
ENV PORT=${PORT}
# whatever here, COPY .. etc.
CMD npm start
在docker build中使用——build-arg
docker build --build-arg PORT=3001 .
您可以在启动应用程序时找到默认端口配置
yourapp/scripts/start.js
向下滚动并将端口更改为您想要的任何端口
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 4000;
希望这对你有所帮助;)
在我的包里换衣服。“export PORT=3001 && react-scripts start”也适用于我,我在macOS 10.13.4上
只需在webpack.config.js中更新一点:
devServer: {
historyApiFallback: true,
contentBase: './',
port: 3000 // <--- Add this line and choose your own port number
}
然后再次运行NPM start。