I was working on a ReactJS project, which I created using the npx create-react-app app-name command. I used some images in the project as well.
My problem is that after I ran npm run build the file paths did not work.
What I mean is that instead of
<link href="./css/main.493343f3.chunk.css" rel="stylesheet">
The result was this:
<link href="/static/css/main.493343f3.chunk.css" rel="stylesheet">
It were be comfortable if I wouldn't have to rewrite the file paths manually, but fine I can do it.
The bigger problem is that as I mentioned I used IMG-s in my project.
The code what I used for the images:
import pic_1 from './pics/pic_1.jpg';
<img src={pic_1} alt=""/>
After compiling (running npm run build) it did not work. As I thought the file paths were not working.
"static/media/placeholder.3a8380d3.jpg"
Instead of
"./media/placeholder.3a8380d3.jpg"
I fixed it manually but it still didn't work.
What should I do differently?
How should I compile the code correctly?
Thank you for reading my question! :)
Let us try to deploy your app to Heroku:
Step 1: Create a React App
npx create-react-app hello-world
cd hello-world
Step 2: Create an Express JS server to serve your production build
In your repository, create a file called server.js and insert the following code:
const express = require('express');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
++In your package.json file, change the start script to the following:
start: "node server.js"
Step 3: Create a React production build
Heroku now runs the build command automatically when you deploy, but it’s a good idea to test the production build locally before deploying (especially your first time).
You can create a production build locally by running this command in your terminal:
npm run build
Step 5: Deploy to Heroku -[you can do this in two ways]:
a) Go to your Heroku account on their official site and create your app manually.
b) or just run heroku create yourAppName
After that you would do the following:
It's done using Git, so you have to do the following:
git init // to initialize a Git repo
git add . // to add the new files
git commit -m "Heroku deploy" // In order to commit your changes
// Now goes the comment to connect your local app to the one created on the Heroku
heroku git:remote -a nameOfYourApp // <- the one created on their website
git push heroku master // to push it to heroku master repo
Now you can run `heroku open` to see your app in the browser
Link for references
b) Using the zero-configuration approach. With this approach you can skip the express server (it's all done for you) -> Just type the following commands:
npm install -g create-react-app
create-react-app my-app
cd my-app
git init
heroku create -b https://github.com/mars/create-react-app-buildpack.git
git add .
git commit -m "react-create-app on Heroku"
git push heroku master
heroku open
Link for references
UPDATE:
Since you're using firebase here are the steps you should take:
Make sure you install firebase tools npm i -g firebase-tools
Create production build of your app by running npm run build
Go to https://console.firebase.google.com and create your "empathy" app
Run firebase init inside of your project and provide the following answers:
Are you ready to proceed? -> YES
From the list choose hosting
Choose your app which you previously created on firebase console
What do you want to use are your public folder -> build
Do you want to configure a single page application -> YES
Do you want to overwrite existing index.html folie -> NO
After that just run firebase deploy
at first, make sure your project runs truly
the make build to the application by fro both client and server if they are found
the push the project in GitHub
go to location of folder project
git init
git add .
git commit -m "deploying static--err, dynamic site to heroku"
git remote add origin remote repository URL
git push origin master
install heroku-cli tools then login
heroku login
Enter your Heroku credentials:
Email:
Password:
heroku apps:create nameproject
git push heroku master
git add .
git commit -m "A helpful message"
git push heroku master
tell me if it success or not
Related
I am trying to create a production build for online deployment of create-react-app with an express js backend using docker.
This is my docker file:
FROM node:12.18.3
WORKDIR /app
COPY ["package.json", "package-lock.json", "./"]
RUN npm install --production
COPY . .
RUN npm run build
EXPOSE 80
CMD ["npm", "start"]
I am using npm run build but still the react developer tools tells that it is a development build and not a production build.
Also in the sources all the project files are visible which should not be visible:
I have tried adding the below statement in the .env file in the root directory of the create-react-app but the project files are still visible:
GENERATE_SOURCEMAP=false
It will be helpful if anyone can guide me how do I fix this and create a production build.
Thank you
The reason why your source files are visible is simple. This is due to CMD ["npm", "start"]. It basically uses the webpack-dev-server and your docker image is build in the development environment. In order to deploy your app to production, you need a web server like nginx, to serve your static files, then create a docker image and run in the container, of your choice.
You need a web server like nginx to serve the files from the build folder. npm run build just creates the build folder it doesn't have any impact on what happens when you run npm start.
For those looking for a solution, the issue can be fixed by creating a server.js file in the project directory with the following content:
const express = require('express');
const serveStatic = require('serve-static');
const path = require('path');
const app = express();
// create middleware to handle the serving the app
app.use("/", serveStatic ( path.join (__dirname, '/build') ) );
// Catch all routes and redirect to the index file
app.get('*', function (req, res) {
res.sendFile(__dirname + '/build/index.html')
});
// Create default port to serve the app on
const port = process.env.PORT || 3000
app.listen(port);
also in the docker file add:
CMD ["node", "server.js"]
instead of
CMD ["npm", "start"]
npm run build
creates a build directory with a production build of your app. Inside the build/static directory will be your JavaScript and CSS files. Each filename inside of build/static will contain a unique hash of the file contents. This hash in the file name enables long term caching techniques.
Why my heroku app is serving app directory? This directory and it's files should not appear like you see in the picture. Should display only static directory.
I use rimraf npm package to remove sourcemaps (.map files) and when I use serve -s build command it works properly on localhost, it displays only the static directory. But when I deploy my files to heroku using heroku/nodejs buildpack it serves the files like in the picture.
Important to note: I use just create react app and don't use any http server like express.js
#edit
Ok I know where is the problem. On localhost I use serve -s build and it serves just build directory. When I use npm start on localhost it serves app and build like on the heroku. Because I use npm start on heroku. But how to switch this command to serve? Tried to replace the start script with "start": "serve -s build" but it works only on localhost.
Ok I solved this problem.
Right now I use npm start script which call nodemon ./server.js script which is just express server with get '/*' and send a index.html
No more serve or react-scripts start on production.
How can transform my react project into script to connect it to html page?
I am a new one in react please be tolerant. My boss demands to get completed script to connect it to html page without node and etc. What shall I do? Thank you.
Please check this url:
https://blog.bitsrc.io/react-production-deployment-part-3-heroku-316319744885
Also, Please check these steps:
In package.json, added this line to the scripts
"heroku-postbuild":
"NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run
build --prefix client".
Then added this
"engines": { "node" : "[your node version]" } after scripts.
In index.js, put the following code after your routes set up
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build",
"index.html"));
});
}
I assume that you use git for version control and already install Heroku.
Open your terminal, then Heroku login -> Heroku create -> git push Heroku
master. If you do not get any error, you are a success to deploy your app.
Hope you will get it to work.
In order to get rid of node, you need to first build your project. If you've initialized your project with create-react-app, run this command:
npm run build
A folder named 'build' will appear in your project root containing your production app. Now the build folder is ready to build and you can serve it with a static server like 'serve'. To install 'serve' via npm, do this:
npm install -g serve
that's it! you can serve it now:
serve -s build
You can find out more about deployment here:
https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#deployment
by using create-react-app
https://create-react-app.dev/docs/getting-started
https://create-react-app.dev/docs/deployment
dev: npm start or yarn start.
prod: npm run build or yarn build.
I am trying to publish my react app on github.Here are the steps that I have followed.
1-I have installed git on my windows.
2- In Visual Studios terminal I have written git init
3-Then I have created a repository on github called "cartdemo"
4-In my package.json I have changed homepage, made the private false and added "deploy": "gh-pages -d build
5- Again in VS terminal I have written git add .
6-git commit -m "Go Live"
7-git remote add origin https://github.com/rahman23/cartdemo.git
8-git push
Note: Here you can see the files https://github.com/rahman23/cartdemo
However when I hit the link https://rahman23.github.io/cartdemo/ I get a page where it is written
cartdemo
This project was bootstrapped with Create React App.
Available Scripts
In the project directory, you can run:
npm start
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Where did I do wrong?
In step 8, you need to tell git which repo and branch to push the project to. Since you added an origin, you would...
git push origin master
Since the project has now been pushed, add this to your package.json file.
"homepage" : "http://rahman23.github.io/cartdemo"
then run:
yarn build in the console, and try pushing it again...
I have created a test React application and I started it with the create-react-app. I am starting it with with yarn start, but that starts the debug version of the application. I did npm run build and it created the build folder, however when I do yarn start from the /build folder, it still starts the debug version of the application. I need this for testing performance with the optimized version. How can I solve this?
You can actually use static server to run build version of your app. It's doable with serve. You can test it with:
npm run build
npx serve -s build
Navigate inside the directory of your app first.
According to the official create-react-app website. When you run npm run build or yarn build you create a build directory with a production build of your app.
After running the command above the next thing you can do to check the build version of your app is to install serve to serve your static site on the port 5000 by default.
npm install -g serve
serve -s build
This will copy the link to your clipboard that you can paste in your browser and see the build version of your app.
You're trying to move from a development build to a production build with create-react-app you need to deploy it using a web server, I would recommend using Heroku or a droplet or you can use Netlify which has a simple set up procedure using the below commands:
cd project-name
npm run build
npm install netlify-cli -g
netlify deploy
Follow command line prompts and choose yes for new project and ./build
as your deploy folder and voila you have a production React app!
You can host the app locally using apache, nginx, express
If you want to run your app in browser with build files served locally from the filesystem (i.e., without a web server), you can put this in your package.json:
"homepage": ".",
Now
build your app with npm run build.
launch <your app>/build/index.html in the browser.
Note: This solution is not suggested if your app (or some routing library) is using the HTML5 pushState history API. https://facebook.github.io/create-react-app/docs/deployment#serving-apps-with-client-side-routing