react express pm2 deployment
Since you have both server and client together, I am assuming it is being developed in a monorepo.
For these kind of purposes I would suggest to go with yarn workspaces as it satisfies your first requirement on its own(npm install for both client and server).
To enable workspaces in yarn versions prior to 1.0, execute to enable it.
yarn config set workspaces-experimental true
Then add a package.json
in the folder(workspace root) outside server and client folders along with whatever other package.json
keys you require. Also install common dependencies directly here instead of installing them individually in both of the package.json files inside server and client. (Don't forget to delete node_modules
folder inside them as a new one would be created in workspace root with all dependencies installed together during yarn install).
{
"private": true,
"workspaces": ["server", "client"]
}
Add the following to your server index.js file.
app.use(express.static(path.join(__dirname, 'public')));
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/public/index.html'));
});
And add npm script to the package.json
in workspace root.
{
...
"scripts": {
"start": "yarn --cwd client react-scripts build && mv ./client/build ./server/public && node ./server/index.js"
}
...
}