Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

65
Views
How to edit a JSON file of a React production build

In my developer build I have a JSON file with one data, I want to be able to change that data in the production build. I used npm run build to make the build. And I can't find that JSON file. I'm making this website for someone so I want to make it easy to change the page content by using JSON.

// src/json/download.json
{
    "file" :  "download.png"
}
This is how I get the json file
const file = require("../json/download.json").file

const filePath = "download/" + file;

const Nav = () => {
    return (
        <Link className="dcm0cujv48 abled" to={filePath} target="_blank" download={file}>
            <p className="fs-14 fw-700 wspwho0gps">
                <i class="fas fa-cloud-download-alt fs-14"></i>
                Download
            </p>
            <p className="yb08fmpwlp"></p>
        </Link>
    )
}
7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

you can check if you are in developement or production

function isProduction(){
return process.env.NODE_ENV === "production"
}

then in your code

const filePath = "download/" + isProduction() ? pathToProductionFile : pathToLocalFile;

then in your scripts you need to create another build scripts with the flag "NODE_ENV=production"

Or setup multiple .env files, check out https://www.npmjs.com/package/dotenv for example .env.local

file=download.png

and .env.production

file=production-download.png

then in your code you can use

process.env.file 

as a variable that holds the value of your file and will differ from local to production

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.