Warm tip: This article is reproduced from stackoverflow.com, please click
frontend javascript package.json reactjs yarnpkg

How to run two react projects together in my local

发布于 2020-04-08 09:26:55

My project sturture is as follows :

Projects --- package.json 
   |-------Project1 ---- package.json
   |-------Project2 ---- package.json

if I run yarn start from cd Projects/Project1, project1 react app runs.

if I run yarn start from cd Projects/Project2, project2 react app runs.

for running the app from Projects, I need to specify project name : eg: yarn start-Project1

I want to run both my projects from the top most directory using yarn start. I want to run the projects on different ports.

If I change the package.json in "Projects" to this

"start": "cd Projects/Project1 && export PORT=3001 && cd ../Project2 && export PORT=3000 && react-scripts start",

It is only running of the project. Thank you.

Questioner
Tanu
Viewed
34
suddjian 2020-02-01 07:19

This is because the && operator runs commands in sequence, meaning each command runs after the previous command exits. Your Project1 doesn't exit, it just sits there running indefinitely.

You need to run both your commands in parallel. One way to do this is to use & instead of &&. & will start the first command in the background instead of waiting for it to exit.

When using & you should be careful of the order of operations. You can use parentheses to group sequential/parallel commands as necessary. I think this command might work for you:

(cd Projects/Project1 && export PORT=3001) & (cd ../Project2 && export PORT=3000) & react-scripts start