How to run react application after reboot - reactjs

I run this script as startup application (ubuntu)
#!/bin/bash
npm run start
and i get error
Module.createRequire is not a function
If i run it normally from terminal - it s ok.
How can i fix it?

Related

Build, npm serve, npm forever: how to keep alive a deployed site ( react + nodejs)

I deployed a website (React + node.js) using a VDS (hostvds).
I installed apache 2, npm serve and npm forever.
The problem:
I can't keep alive frontend and backend at same time when i quit puTTy..
What i did to deploy the application:
-To run the backend, I use: forever server.js (using VDS console)
-To run the frontend, in /var/www/html folder, where i moved my front build folder, I use serve build (using puTTy)
Everything works perfectly, but when i quit puTTy the frontend stop to work.
Could someone tell me how to run and keep alive frontend?
Thanks
The problem you're facing is that the command you run in the front is attached to the tty and when you close the connection the command dies as well. This is not happening on the back because the forever tool detach it so it can effectively run forever. Your question can be summarized as "How to run multiple commands in detached mode?" A quick search give some results that can achieve what you are looking for, for example using screen. Yo have multiple approaches:
Op1: Using Screen
# run backend command
screen -dm "npm start"
# run frontend command
screen -dm "npm start"
Note that the screen command is used to create new sessions and detach them from the tty. So nohup could handle your issue.
and
Op2: Using systemd service
Another, and more robust way is using services of systemd and handling the lifecycle using systemctl command. In this way you can define restart policies (autorestart when failed) and also autostart when the machine reboots. You would have to create two different units, one for back and one for front.
Create the files
/etc/systemd/system/backend.service
[Unit]
Description=My backend
[Service]
Type=simple
Restart=always
User=nobody
Group=nobody
WorkingDirectory=/your/back/dir
ExecStart=/usr/bin/npm start
[Install]
WantedBy=multi-user.target
/etc/systemd/system/frontennd.service
[Unit]
Description=My frontennd
[Service]
Type=simple
Restart=always
User=nobody
Group=nobody
WorkingDirectory=/your/front/dir
ExecStart=/usr/bin/npm start
[Install]
WantedBy=multi-user.target
Once the files are created you can handle the service lifecycling with systemctl:
Run the apps:
systemctl start [backend|frontend]
Stop the apps:
systemctl stop [backend|frontend]
Check status:
systemctl status [backend|frontend]
To enable the autostart on boot just enable the service(s) using systemctl enable [backend|frontend]. You can disable it using `systemctl disable [backend|frontend].
Op3: Static frontend
Doing the options 1 and 2 will solve your issue, but have in mind you are serving a frontend using npm when it could be build to static files and served using apache2 directly, which will reduce cpu/memory consumption and it would be much faster. This is just regarding the frontend, the backend is dynamic and it needs the option 1 or 2.
As you mention it I assume you know how apache2 works, so just build the frontend application to generate plain html, css and js files, then move them to the apache2 folder and it will serve the files to the users for you.
cd /your/front/folder
npm run build
cp -r build/ /var/www/html
More info on how to build the statics here
Summary
Running commands in a shell will attach them and if you close the shell they will die unless you detach them. You can use detaching tools like screen or nohup, or you can change the approach for this specific scenario and use services to handle the lifecycle (apache2 is also a service).
Why don't you try to use forever for the front-end as well? If I remember well, the whole point of the forever service is to keep the command running even if you stop the terminal. I would try something like forever start -c "npm start".

Stop React dev server while continuing parent script execution

I need a test.sh (bash)/test.ps1 (powershell) shell script of the following structure:
<build preparation>
npx react-scripts start
<build cleanup>
The build cleanup step should run after I stop the localhost development server.
Currently, if I send Ctrl+C to the terminal, it stops both the dev server as well as the parent script, so the build cleanup step is not executed.
I did not find similar questions online (I am not sure what to search). Preferably, I need both PowerShell and Linux solutions. My only guess is somehow the react-scripts command should "trap" the shell Ctrl+C invocation, and self-exit safely, but I do not know how to do that unless react-scripts implements it on their own.
In PowerShell you can use a try{}finally{} construct to ensure the cleanup is run on interruption:
# <build preparation>
try {
npx react-scripts start
}
finally {
# <build cleanup>
}

React app rewired doesnt run under port 1024

I have this React application that needs to run under port 443. Usually it works fine but now i got a new computer and for some odd reason i cant run it anymore.
I have two ways for this. A custom script in the package.json file and it should also work when ran with sudo. sudo npm start or npm run https.
The port is defined in the .env file by default.
This is what my package.json looks like
When i run sudo npm start the expected behaviour is for it to run, without sudo, it should warn me that Admin permissions are required. This is what is happening with sudo aswell.
$ sudo npm start
or with the other command
$ npm run https
Not quite sure what to do in this situation. Anyone have any idea how to make it work ?
Running this command fixed the issue for me.
sudo sysctl net.ipv4.ip_unprivileged_port_start=443

Unable to view the React app started with 'sudo npm start'

I need to run my local React app at banker-dev.mh*b.my on port number 443.
This is to enable the app to call a third party API
I've set my local .env file with the following:
HTTPS=true
HOST=banker-dev.mh*b.my
PORT=443
When I run npm start, I encountered the message:
? Admin permissions are required to run a server on a port below 1024.
Would you like to run the app on another port instead? (Y/n)
So I tried sudo npm start but hit error sudo: npm: command not found
I then sudo installed npm by referring to this: sudo: npm: command not found
Now I'm able to run sudo npm start
The app is started as I can see from the code editor.
But it's not opening a new browser window with the app loading as it usually does with my normal npm start
And I noticed a weird thing: it's trying to bind to the host dev.mh*b.my instead of banker-dev.mh*b.my
My hosts file contains the following:
127.0.0.1 localhost
127.0.1.1 yogesnsamy-ThinkPad-E490s
127.0.0.1 banker-dev.mh*b.my
127.0.0.1 dev.mh*b.my
I'm doing something wrong, but I'm not sure what. I'm attaching a video reference of what's happening: https://drive.google.com/file/d/1TYcX3wG8yBFjSukvVAkYZDloPJE3JS8T/view?usp=sharing
Sorry that part where the app starts to load takes long (0:34 to 1:00). After it does, I try to view the app on the browser but fail.

docker executable file not found in $PATH" react image

I am trying to run a freshly create react app made with create-react-app in docker. I have a Docker file which succesfully allows me to build the image, but when I try to run it I get:
PS C:\Users\Bertinator\desktop\gamelist\client> docker run client4 .
container_linux.go:265: starting container process caused "exec: \".\":
executable file not found in $PATH"
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from
daemon: oci runtime error: container_linux.go:265: starting container
process caused "exec: \".\": executable file not found in $PATH".
ERRO[0001] error waiting for container: context canceled
PS C:\Users\Bertinator\desktop\gamelist\client>
This is my Docker file, which is place in the root folder of my create-react-app project:
FROM node:7.8.0
ENV NPM_CONFIG_LOGLEVEL warn
COPY . .
RUN npm run build --production
RUN npm install -g serve
CMD serve -s build
EXPOSE 5000
Are you running from a Linux docker image on a Windows machine? That does not work.
Ref http://training.play-with-docker.com/beginner-linux/
(...) Linux containers require the Docker host to be running a Linux kernel. For example, Linux containers cannot run directly on Windows Docker hosts. The same is true of Windows containers - they need to run on a Docker host with a Windows kernel.
When you say docker run client4 . you are asking docker to run that container and execute the . (dot) command which does not make sense. That is why you are getting the first error.

Resources