Hi,
I am trying to use pm2 on a dev server with your npm start up script react-scripts start
Do you have any recommendations on where we could execute pm2 process on node.js running this script? normally we use pm2 start server.js --watch
Can you please explain the use case?
pm2 is meant for production usage.
npm start in Create React App is meant only for development as mentioned in its output message.
hi @gaearon
Our client requested we continuously run in dev mode on a server, so their beta testers can see error logs in the console. as well as the redux state logs to report back to our team.
So I wasn't sure exactly the path I can pass over to PM2 that will actually run this file, same goes for running in production I should add for the future.
Thanks so much for replying!
The command itself is in node_modules/.bin
. npm adds it to PATH automatically inside scripts
.
So the full command you want to run is node_modules/.bin/react-scripts start
. Although it would be better to just run npm start
in the project folder.
I hope this helps!
@gaearon is there a way to just execute the file? I just worry that it would run node and pm2 at the same time, I believe it runs node already under the hood with pm2 command
Sorry, I don't really know how pm2 works. I provided the script path so it should be enough for you. If you're not sure how to pass it to pm2 please refer to its documentation or ask on StackOverflow. Cheers! Let me know if there's any other info I could provide you with.
problem is that npm start doesn't run as a daemon, so it's hard to detach and monitor. We need a way to run react-script as a daemon.
Can you create an issue describing your proposal?
I just ran an ejected copy successfully in pm2 like this:
pm2 start scripts/start.js --name "myapp"
I imagine that for non-ejected apps, and based on the comments above, it would be executed like this:
pm2 start node_modules/.bin/react-scripts/start.js --name "myapp"
@sam3k 's command failed for me but I got it to run with a slight adjustment pm2 start node_modules/react-scripts/scripts/start.js
--name "myapp"
It makes no sense to run npm start with pm2. The start script is only meant for development (because it starts a bloated development server). You need to build, then serve the app using a server meant for production, like Node with Express. Here's what you can do:
.env
file in your root directory:PORT=3002
ABSOLUTE_STATIC_PATH=C:/path/to/project/build/static/
server.js
in your root directory:require('dotenv').config();
const express = require('express');
const compression = require('compression');
const path = require('path');
const app = express();
app.use(compression());
app.use('/static', express.static(process.env.ABSOLUTE_STATIC_PATH));
app.get('/', (req, res) => {
res.sendFile(path.join(process.env.ABSOLUTE_STATIC_PATH, '../index.html'));
});
const PORT = process.env.PORT || 3002;
app.listen(PORT, '0.0.0.0', (err) => {
if (err) { console.log(err); }
console.info(`==> 馃寧 app listening on http://localhost:${PORT}.`);
});
package.json
:{
"scripts": {
"prod": "pm2 start ./server.js --name appName",
}
}
yarn prod
I agree the question was a little misguided, but @savovs answer puts one on the right path and goes beyond that by explaining the process very concisely. Above and beyond the call of duty! Thanks!
add this deploy to your script in package.json
"deploy": "pm2 start ./server.sh --name yourAppName",
Then in the same directory as the package.json, create an executable server.sh:
echo "Serving yourAppName!"
serve -s build
Don't forget to make server.sh an executable by running:
chmod +x server.sh
Now it's party time! Deploy your app by running
npm run deploy
Done!
Most helpful comment
@sam3k 's command failed for me but I got it to run with a slight adjustment
pm2 start node_modules/react-scripts/scripts/start.js
--name "myapp"