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 react front-end March 9, 2024 How to create css styles for specific components in React?

You can use React Context to manage styles for specific components or groups of components. It involves creating a context that provides style information to its consumer components. Here’s a more detailed explanation of how you can implement this approach:

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: