我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。
在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006
我的项目是基于创建-反应-应用程序。NPM启动或yarn启动默认将在端口3000上运行应用程序,在package.json中没有指定端口的选项。
在这种情况下,如何指定自己选择的端口?我想同时运行这个项目的两个(用于测试),一个在端口3005,另一个是3006
当前回答
只需在webpack.config.js中更新一点:
devServer: {
historyApiFallback: true,
contentBase: './',
port: 3000 // <--- Add this line and choose your own port number
}
然后再次运行NPM 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 .
您可以指定一个名为PORT的环境变量来指定服务器将在其上运行的端口。
$ export PORT=3005 #Linux
$ $env:PORT=3005 # Windows - Powershell
你可以使用cross-env来设置端口,它可以在Windows、Linux和Mac上工作。
yarn add -D cross-env
然后是包装。Json的开始链接可以是这样的:
"start": "cross-env PORT=3006 react-scripts start",
您需要更新您的软件包。json来指定不同的PORT
在脚本部分,替换start命令如下:—
确保所提到的PORT可以自由监听
"start": "export PORT=3001;react-scripts开始”
您的应用程序将从http://localhost:3001开始
谢谢
只需在webpack.config.js中更新一点:
devServer: {
historyApiFallback: true,
contentBase: './',
port: 3000 // <--- Add this line and choose your own port number
}
然后再次运行NPM start。