如何在package.json中使用concurrently同时运行前后端npm脚本?
Let's break down the issues in your current setup and fix them step by step:
1. Fix the client Script in Root package.json
Your current client script has an invalid --suffix flag (likely a typo) that's throwing things off. The correct command to trigger the client's start script from the root project is:
"client": "npm start --prefix client"
The --prefix client tells npm to target the package.json inside your client folder and run its configured start script.
2. Fix the dev Script for Parallel Execution
The biggest mistake here is using &&—this runs commands serially, meaning it waits for the server to finish before trying to start the client. Concurrently is built for parallel runs, so you just need to pass your commands as separate quoted strings, no && needed. Update it to:
"dev": "concurrently \"npm run server\" \"npm run client\""
If your shell supports single quotes, you can simplify it to avoid escaped quotes:
"dev": "concurrently 'npm run server' 'npm run client'"
3. Verify Prerequisites
Before testing, make sure:
- You've installed
concurrentlyas a dev dependency in your root project:npm install --save-dev concurrently - Your
clientfolder has a validpackage.jsonwith a workingstartscript (this is standard if you created the client with Create React App or similar tools).
Full Corrected scripts Section
Here's how your final root package.json scripts should look:
"scripts": { "start": "node server.js", "server": "nodemon server.js", "client": "npm start --prefix client", "dev": "concurrently \"npm run server\" \"npm run client\"" }
Testing It Out
Run the dev script from your root directory:
npm run dev
This should spin up both your backend server (with nodemon for auto-reloads on code changes) and frontend client simultaneously.
内容的提问来源于stack exchange,提问作者Im not Xavier

