DEV Community

Cover image for Simplest Way to Install Babel Plugins in Create React App
Anson Heung
Anson Heung

Posted on • Edited on

14 3

Simplest Way to Install Babel Plugins in Create React App

The traditional way of installing Babel plugins in a Create React App project requires you to eject the app via running npm run eject. However, ejecting may be a bad idea because it’s a one-way operation that exposes complex configurations files.

In this article, I’ll show you how to install Babel plugins WITHOUT ejecting your Create React App.

Steps

  1. Install react-app-rewired and customize-cra:

    npm install react-app-rewired customize-cra --save-dev
    # or
    yarn add react-app-rewired customize-cra --dev
    

    Purpose of these two packages
    • react-app-rewired: Overrides Create React App webpack configs without ejecting.
    • customize-cra: Provides a set of utilities to customize Create React App config, such as reading from .babelrc. Requires react-app-rewired as a dependency.

  2. Install your Babel plugin(s). Suppose our Babel plugin is called babel-plugin-myPlugin:

    npm install babel-plugin-myPlugin --save-dev
    # or
    yarn add babel-plugin-myPlugin --dev
    
  3. Open package.json located in the project root and edit the "scripts" field:

     "scripts": {
    -  "start": "react-scripts start",
    -  "build": "react-scripts build",
    -  "test": "react-scripts test",
    +  "start": "react-app-rewired start",
    +  "build": "react-app-rewired build",
    +  "test": "react-app-rewired test",
       "eject": "react-scripts eject"
     },
    
  4. Create .babelrc at the root of the project (where package.json is located) and add the following code. Replace babel-plugin-myPlugin with the actual name of your plugin.

    {
      "plugins": ["babel-plugin-myPlugin"]
    }
    
  5. Create config-overrides.js at the root of the project and add the following code:

    // Overrides create-react-app webpack configs without ejecting
    // https://github.com/timarney/react-app-rewired
    
    const { useBabelRc, override } = require("customize-cra");
    module.exports = override(useBabelRc());
    
  6. Run npm start or yarn start to start the development server and see if everything works properly.

Without Using .babelrc

In the above, Step 4 requires you to create a new .babelrc file to register Babel plugins. Alternatively, you can skip Step 4 (i.e., no need to create .babelrc) by directly registering Babel plugins inside config-overrides.js:

// Overrides create-react-app webpack configs without ejecting
// https://github.com/timarney/react-app-rewired

const { addBabelPlugins, override } = require("customize-cra");
module.exports = override(
  ...addBabelPlugins(
    "babel-plugin-myPlugin"
    /* Add plug-in names here (separate each value by a comma) */
  )
);
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! If you find it useful, don’t forget to like and share this post 🙌

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (10)

Collapse
 
minicatscb profile image
minicatsCB

You can also pass options to the plugin as normal. Example for plugin babel-plugin-macros in .babelrc:

{
    "plugins": [
        ["babel-plugin-macros", {"fontawesome-svg-core": {"license": "free"}}]
    ]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sbrjt profile image
Shub

Thanks :) I was trying to use preact/signals-react in a CRA project but couldn't as CRA doesn't allow babel. This came in handy!

Collapse
 
walterfcarvalho profile image
Valter f Carvalho

Very good simple objective funcional awesome explanation, works fine and was exactaly what I've beeen findind. thanks a lot.

Collapse
 
bhps69 profile image
bhps69

whats the' .... ' after module.exports?

Collapse
 
ansonh profile image
Anson Heung

Sorry which code block are you referring to? The last or the second last code block?

Collapse
 
walterfcarvalho profile image
Valter f Carvalho

just create on file called config-override.js on root application directory and paste those lines.

Collapse
 
bhps69 profile image
bhps69

yes, i have pasted them , but i dint understand what those three dots mean? is it a new line continuation marker?

Thread Thread
 
ansonh profile image
Anson Heung

The three dots (...) is the JavaScript spread syntax.

Collapse
 
yueveron profile image
Huang Yue

How to do the same thing with typescript?

Collapse
 
bhps69 profile image
bhps69

The code for config-over
Module.exports=override ( ?

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay