Search code examples
javascriptreact-nativebabeljsnpxbabel-cli

npx babel not reading configuration from babel.config.js


When running npx babel index.js from the command line, I was hoping I would see my babel configurations being applied from babel.config.js

However it does not seem the case as was wondering why this might be?

// babel.config.js
module.exports = function babel(api) {
 api.cache(true);
   return {
     presets: ['module:metro-react-native-babel-preset'],
     plugins: [
       [
         'babel-plugin-root-import',
         {
           rootPathSuffix: './src',
           rootPathPrefix: '~/',
         },
       ],
     ],
   };
 };

// index.js
import { AppRegistry } from 'react-native';
import App from '~/App';
AppRegistry.registerComponent("App Name", () => App)

// Expected output from npx babel index.js
import { AppRegistry } from 'react-native';
import App from './src/App'; // Note the change from '~' to './src' using babel-plugin-root-import
AppRegistry.registerComponent("App Name", () => App)

I noticed in the npx babel --help it stated that --no-babelrc flag ignores configuration from .babelrc and .babelignore files. Does this suggest that babel.config.js files aren't considered when calling this command?

Cheers


Solution

  • babel.config.js config change is introduced in babel 7; so if you are using babel 6.*, it doesn't understand project wide configuration yet; either use .babelrc or upgrade to babel 7 to be able to use new features; I'd did the upgrade its pretty smooth and painless, just make sure you have clean git directory ( in case of emergency :) and do it.