Search code examples
node.jsreactjsmernconcurrently

My full stack application won't run NodeJS and React concurrently


Running the client server and the backend server independently will produce no errors but mongoDB exits automatically when client and server run concurrently

npm start
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `concurrently "npm run client" "npm run server"`
[0] 
[0] > [email protected] client /Users/owner/Documents/projects/roster
[0] > react-scripts start
[0] 
[1] 
[1] > [email protected] server /Users/owner/Documents/projects/roster
[1] > cd src/server nodemon index.js
[1] 
[1] npm run server exited with code 0
[0] ℹ 「wds」: Project is running at http://192.168.0.4/
[0] ℹ 「wds」: webpack output is served from 
[0] ℹ 「wds」: Content not from webpack is served from /Users/owner/Documents/projects/roster/public
[0] ℹ 「wds」: 404s will fallback to /
[0] Starting the development server...

A snippet of my package.json. I'm running npm start from the root folder roster.

"scripts": {
    "start": "concurrently \"npm run client\" \"npm run server\" ",
    "server": "cd src/server nodemon index.js",
    "client": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"

A picture of my project structure

require('dotenv').config();
const express = require('express');
const handle = require('./handlers');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const app = express();

app.use(bodyParser.json());
app.use(cors());
app.use(express.json());

const uri = process.env.URI;
mongoose.connect(uri, {useNewUrlParser: true, useCreateIndex: true});
const connection = mongoose.connection;
connection.once('open', () => {
  console.log("MongoDB database connection established")
})

const votesRouter = require('./routes/votes')

app.use('/votes', votesRouter);
require('dotenv').config();

app.get('/', (req, res) => res.send('hello world'));


app.use(handle.notFound);
app.use(handle.errors);

app.listen(port, console.log(`Server running on port ${port}`));

Solution

  • Add && to your server script

    "server": "cd src/server && nodemon index.js"