create-react-app npm run build command - reactjs

I have a built a small React application with create-react-app, and it runs fine from the local server after running npm start. OK so far.
However, when I run npm run build, the process appears to execute correctly (creates build folder, which contains the bundled js file and the index.html file), but when I open index.html in my browser it renders nothing. What am I missing?
Aside: I also tried uploading it to a remote server and when I went to the URL the browser came back with...
Forbidden: You don't have permission to access / on this server.
...if anyone has any idea how to resolve this I'd also appreciate it.

However, when I run npm run build, the process appears to execute correctly (creates build folder, which contains the bundled js file and the html.index file), but when I open index.html in my browser it renders nothing. What am I missing?
When you run npm run build, it prints the relevant instructions:
You can’t just open index.html because it is supposed to be served with a static file server.
This is because most React apps use client-side routing, and you can’t do that with file:// URLs.
In production, you can use Nginx, Apache, Node (e.g. Express), or any other server to serve static assets. Just make sure that if you use client-side routing, you serve index.html for any unknown request, like /*, and not just for /.
In development, you can use pushstate-server for this. It works with client-side routing well. This is exactly what the printed instructions suggest you to do.
I also tried uploading it to a remote server and when I went to the URL the browser came back with Forbidden: You don't have permission to access / on this server.
You need to upload the contents of the build folder, not the build folder itself. Otherwise the server can’t find your index.html because it is inside build/index.html, and so it fails. If your server doesn’t detect a top-level index.html, please refer to your server’s documentation on configuring files served by default.

you can't run the production build by clicking on index.html, you have to modify your script like bellow.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"deploy": "serve -s build"
}
after running npm run-script build, run npm run-script deploy, you will get some thing like this, this is where you can load your production build.
npm install -g serve before run npm run-script deploy.

Here You can solve this problem in 2 possible ways.
1.Change the routing history to "hashHistory" instead of browserHistory in the place of
<Router history={hashHistory} >
<Route path="/home" component={Home} />
<Route path="/aboutus" component={AboutUs} />
</Router>
Now build the app using the command
sudo npm run build
Then place the build folder in your var/www/ folder, Now the application is working fine with addition of # tag in each and every url. like
localhost/#/home
localhost/#/aboutus
Solution 2 : Without # tag using browserHistory,
Set your history = {browserHistory} in your Router,Now build it using sudo npm run build.
You need to create the "conf" file to solve the 404 not found page,
the conf file should be like this.
open your terminal type the below commands
cd /etc/apache2/sites-available
ls
nano sample.conf
Add the below content in it.
<VirtualHost *:80>
ServerAdmin admin#0.0.0.0
ServerName 0.0.0.0
ServerAlias 0.0.0.0
DocumentRoot /var/www/html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/html/">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
Now you need to enable the sample.conf file by using the following command
cd /etc/apache2/sites-available
sudo a2ensite sample.conf
then it will ask you to reload the apache server,using
sudo service apache2 reload or restart
then open your localhost/build folder and add the .htaccess file with content of below.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ / [L,QSA]
Now the app is working normally.
Note: change 0.0.0.0 ip to your local ip address.
I hope it is helpful to others.

open index.html file.
scroll near end and you will see
<script src="/static/js/2.b5125f99.chunk.js"></script><script src="/static/js/main.fcdfb32d.chunk.js"></script></body></html>
just add a dot in front of the two src attribute:
<script src="./static/js/2.b5125f99.chunk.js"></script><script src="./static/js/main.fcdfb32d.chunk.js"></script></body></html>
also if you have any styles you must also scroll near beginning where you will see:
<link href="/static/css/main.1eabf3ab.chunk.css" rel="stylesheet">
and also place a dot in front of the href attribute
<link href="./static/css/main.1eabf3ab.chunk.css" rel="stylesheet">
NOTE: filename may/will not be the same for you
also be sure to change this back when using a server or just run npm run build again(i have no idea what happens if you dont)

After building your application through create-react-app create-react-app
Run command npm run build After that run command npm install -g serve & finally serve -s build
More detailed information can be found from here create-react-app-deployment

Just enter these two commands in your terminal:
npm run build
npx serve -s build
After this just go to localhost:5000

'Web Server for Chrome' extension is super easy to use. Install it and set the directory to the ~/my-react-app/build/

cd to your build folder,
python -m SimpleHTTPServer 8080 to start a server on port 8080,
go to address localhost:8080/index.html in your browser.

I tried to run the same command and react app was also showing white screen.
main js file was taking the relative path to the file and showing error when I open js file in new browser "Your file was not found"
I make it to absolute path and its working fine.

https://www.taniarascia.com/getting-started-with-react/ and The final script can be found on the GitHub.
npm run build -> will create a folder build which include the index.html and static folder. By just clicking the index.html will open on a new browser's tab. But it shows only blank page. Why? because it's need a server to make it works.
Solution :
Move the file on other folder. Open the folder using VC Code. Open the index.html and right click -> select : Open with live server. A new tabs opened on your browser with related ip address and port.
Or you can run it on localhost using xampp :
Open the XAMPP server. Place it on localhost. And it must work.

Related

Cannot open a React app in the browser after dockerising

I'm trying to dockerise a react app. I'm using the following Dockerfile to achieve this.
# base image
FROM node:9.4
# set working directory
WORKDIR /usr/src/app
# install and cache app dependencies
COPY package*.json ./
ADD package.json /usr/src/app/package.json
RUN npm install
# Bundle app source
COPY . .
# Specify port
EXPOSE 8081
# start app
CMD ["npm", "start"]
Also, in my package.json the start script is defined as
"scripts": {
"start": "webpack-dev-server --mode development --open",
....
}
I build the image as:
docker build . -t myimage
And I finally run the image, as
docker run IMAGE_ID
This command then runs the image, however when I go to localhost:8080 or localhost:8081 I dont see anything.
However, when I go into the docker container for myimage, and do curl -X GET http:localhost:8080 I'm able to access my react app.
I also deployed this on google-kubernetes and exposed a load-balancer service on this. However, the same thing happened, I cannot access the react-app on the exposed endpoint, but when I logged into the container, and made curl request, I was getting back the index.html.
So, how do I run the image of this docker image so that I could access the application through a browser.
When you use EXPOSE in Dockerfile it simply states that the service is listening on the specified port (in your case 8081), but it does not actually create any port forwarding.
To actually forward traffic from host machine to the service you must use the -p flag to specify port mapping
For example:
docker run -d -p 80:8080 myimage would start a container and forward requests to localhost:80 to the containers port 8080
More about EXPOSE here https://docs.docker.com/engine/reference/builder/#expose
UPDATE
So usually when you are developing node applications locally and run webpack dev-server it will listen on 127.0.0.1 which is fine since you intend to visit the site from the same machine as it is hosted. But since in docker the container can be thought of as a separate instance that means you need to be able to access it from the "outside" world which means that it is necessary to reconfigure the dev-server to listen on 0.0.0.0 (which basically means all IP addresses assigned to the "instance")
So by updating the dev-server config to listen on 0.0.0.0 you should be able to visit your application from your host machine.
Link to documentation: https://webpack.js.org/configuration/dev-server/#devserverhost

node HTTP-Server is not working properly?

when i install http-server in c: drive it install automatically in d: drive.
here is the result:-
C:\Users\Kuncham>npm install http-server -g
D:\usr\local\http-server -> D:\usr\local\node_modules\http-server\bin\http-server
D:\usr\local\hs -> D:\usr\local\node_modules\http-server\bin\http-server
+ http-server#0.11.1
updated 1 package in 1.131s
after installation when i run http-server in my project folder it will start the http-server and also we can access the link given in command prompt. the browser will show only files not run the my angular application.
Install http-server using npm install -g http-server command
Generate a build by using ng build --prod command
Go to dist/project_name from cmd
Type http-server -p 8080 on cmd
Type http://localhost:8080/ on browser
Perform an ng build on the directory and point your http-server at the resulting dist/ folder, which contains the compiled HTML and Javascript files your browser can consume.
More information: Angular - Deployment
I just installed http-server today and my problem was it just wasn't serving the content at all when I went to localhost:8080. The command prompt would flash for a second and disappear. I resolved my problem by hitting http://192.168.40.78:8080. http-server lists two urls you could hit. The 192 one worked, but not the localhost one.
specify the file when running http-server
http-server [path] [options]
[path] defaults to ./public if the folder exists, and ./ otherwise.

React dockerized app: routes not working

I am new to Docker and I am trying to dockerize a React app using this Dockerfile:
Dockerfile
FROM node:latest
LABEL autor="Ed de Almeida"
RUN apt-get update && apt-get install -y apache2 tree
RUN mkdir /tmp/myapp
COPY . /tmp/myapp
RUN cd /tmp/myapp && npm install
RUN cd /tmp/myapp && npm run build
RUN cd /tmp/myapp/build && cp -Rvf * /var/www/html
RUN cd /var/www && chown -Rvf www-data:www-data html/
EXPOSE 80
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
CMD /usr/sbin/apache2ctl -D FOREGROUND
This app uses react-router v4 and it is working perfectly well in my computer when I run it with npm start. It also works fine at Heroku, but there I had to add a static.json file in order to make the routes work, or else I would have only route '/' working and all other routes (like '/admin') would give me a 404.
static.json
{
"root": "build/",
"routes": {
"/**": "index.html"
}
}
By now I have five routes at Heroku:
http://businessfy.herokuapp.com/
http://businessfy.herokuapp.com/admin
http://businessfy.herokuapp.com/admin/usuarios
http://businessfy.herokuapp.com/admin/financeiro
http://businessfy.herokuapp.com/admin/equipe
and they are running fine, going where it expected.
It happend that when I start the container created with the image generated by the Dockerfile above, I only may access '/' and all the routes that are working fine will give me a 404.
I tried to add the static.json file, but it didn't work at all. First I just copied it to the application root directory, just like at Heroku. Nothing changed. Then I tried to import it from my index.js file. Again, nothing changed.
What is the problem here? Am I missing something at Docker? I'm assuming I am, because I am used to React but new to Docker.
Any hints?

How do I serve index.html by default in my http-server

I have created a basic AngularJS app in node environment. I am using http-server module for serving the files to browser. Everything is working fine except for the fact that I can't get to serve index.html by default when the server launches.
I checked out npm registry for more options to http server module, and also tried looking for a relevant question on SO but still not able to get what I desire.
Is it possible at all to specify the file to pick up while server starts.
My server basically starts at localhost:8080 while I would like localhost:8080/index.html
My start script is http-server -a localhost -p 8080 -c-1. If I do something like http-server -a localhost -p 8080 -c-1 index.html, to my surprise it opens the index.html file but serves it on file protocol and not on localhost.
What am I doing wrong here.
P.S. I visited Angular JS seed and there official example says http-server -a localhost -p 8080 -c-1 ./app. However, when I do this I get error Windows can't find specified path, although my structure is similar to the seed.
My structure:
dir
--app.js
--index.html
--node_modules
--package.json
Make sure you are building your project first and generating an output ./dist folder or similar.
Then try this command instead:
http-server -p 8080 ./dist -o http://localhost:8080/index.html
You are missing the http:// in your url.
Add a -f /index.html or whatever the document is.
In my case for example:
http-server -c-1 -f /index.html
For me what solved the problem was, I clicked the link through the CLI after doing http-server [path].
In my case I ran http-server . (which has an index.html in the directory), then logs out this.
Starting up http-server, serving .
http-server version: 14.1.1
http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none
Available on:
http://192.168.8.133:8080
http://127.0.0.1:8080
http://172.23.128.1:8080
Hit CTRL-C to stop the server
Now I just used the link in the line below Available on: which is:
http://192.168.8.133:8080, works for me
http://127.0.0.1:8080, did not work for me, it download file instead
http://172.23.128.1:8080, works!
Such a weird behaviour of http-server.
Try to put you static file inside a new directory public. and run you server http-server
- app.js
-public
--index.html
-package.json

Is There Any Location Specific in Apache2

I'm using Apache2 running on Ubuntu-12.04 machine and I run my files through apache2 pointing to my folder in the local file system like
$ cd etc/apache2/
$ vi sites-enabled/000-default
And I change the location pointing to my folders in
DocumentRoot /home/user/foo/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/user/foo/>
What is the default location of Apache2 web root where if I would just copy my files the Apache server would pick it. For running hundreds of files through Apache2 each and every time I keep doing the above steps of changing the location manually. Is there any other best way of doing it.
It would be great if anyone could help me out.
If do an ls -l inside /etc/apache2/sites-enabled/ you'll see that 000-default is a symlink to /etc/apache2/sites-available/. The idea being that you create site definition files in sites-enabled, then symlink to them from sites-available... making it very simple to enable and disable sites as needed.
In fact, Ubuntu provides the a2ensite command to enable sites, and a2dissite to disable sites. This means all you have to do is create the vhost file in sites-available, then run a2ensite to enable it.
Take a look at https://help.ubuntu.com/12.04/serverguide/httpd.html for more details.

Resources