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 react front-end February 19, 2024 How to smoothly scroll to a specific content or element in React?

To implement scrolling to a specific content or element on click in a React application, you can follow these steps:

question typescript react September 28, 2024 How to add a custom element to a Next.js/Typescript project?

Let’s say I have a custom element setapp-badge that I want to use in my tsx files. If I go ahead and use it, the compiler will throw an error and Next.js will fail to build. It seems the problem might be a combination of how Next.js, TypeScript, and custom elements work together. Therefore, let’s try an approach that avoids the namespace/module issues while ensuring custom elements are recognized in a Next.js/TypeScript project.

question nextjs react July 9, 2024 How to create global types in Typescript in a Next.js app?

The env.d.ts file is typically used for global type declarations, especially for environment variables and other globally available types. This file is automatically included in the TypeScript compilation if it’s referenced in tsconfig.json.