3

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! :)

3
  • create-react-app.dev/docs/deployment Commented Aug 23, 2019 at 21:47
  • 1
    Thank you! I did read it, but due to my limited English and programming knowledge, need help t understand it. Please help me. Commented Aug 23, 2019 at 21:49
  • 1
    create-react-app uses Babel and Webpack -> They are responsible for your development work flow. After you create a production build it optimizes your code and puts it into the build folder. NOTE: Take a look in it, it changes the folder structure a lot, so that's why you get different paths. But with create-react-app everything should work properly right out of the box. I hope that makes sense. Cheers. Commented Aug 23, 2019 at 22:30

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! You made it clearer! I'm using firebase as my website hoster. Now I (kinda) understand how it is working. :)
@BudaÖrs I'm glad that I can help. Please, check out the update I made. it includes instructions for deploying your app to firebase. Have a good one.
Thank you very much! You really did help a lot! :))
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.