Loading...

How to disable minification in a React app?

question react front-end
Ram Patra Published on August 21, 2024

To disable minification in a React app created with create-react-app (CRA) using npm, you need to modify the build process. However, CRA does not directly expose Webpack configuration without ejecting, but you can still achieve this without ejecting by using the GENERATE_SOURCEMAP environment variable and a custom build script.

Steps to Disable Minification

  1. Create a .env or .env.local File: In the root of your project, create a file named .env or .env.local if it doesn’t already exist.

  2. Add the GENERATE_SOURCEMAP Variable: Add the following line to the .env file to prevent CRA from minifying your code:

    GENERATE_SOURCEMAP=false
    

    This variable instructs the build process to include source maps and can effectively disable minification, making the code more readable.

  3. Clean the dist directory: The npm build process doesn’t fully clear the dist directory so this step is essential.

    rm -rf ./dist
    
  4. Build your project:

    To build your React app without minification, run:

    npm run build
    
  5. Start your project:

    The below command may vary so please check your scripts property inside the package.json file.

    npm run dev
    

Important Notes

  • Source Maps: Disabling minification via GENERATE_SOURCEMAP=false includes source maps, making it easier to debug the code.
  • Performance: The non-minified code is not optimized for production, so this approach should only be used for debugging.

This method allows you to disable minification without ejecting or modifying CRA’s internal Webpack configuration.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on August 21, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question nextjs react July 19, 2024 How to get the current path in Next.js?

To get the current path in a Next.js application, you can use the usePathname hook from next/navigation module. This hook allows you to access the current pathname within your components. Here’s how you can use it:

question nextjs react July 18, 2024 How to programmatically navigate to a page in Next.js?

In Next.js, you can programmatically navigate to different pages using the useRouter hook from the next/navigation module. This hook provides a convenient way to navigate between pages within your Next.js application. Below is an example demonstrating how to use the useRouter hook to programmatically navigate to another page.

question nextjs react February 9, 2024 Main advantages of using the Link component in Next.js over Anchors and Buttons

The main advantage of using the Link component in Next.js for client-side navigation is its optimized prefetching behavior, which enhances the performance and user experience of your web application. Here’s a breakdown of the key advantages: