Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - NativeWind and react-native-dotenv conflict

I am new to React Native and am using Tailwind CSS and trying to implement react-native-dotenv.

I have installed NativeWind and Tailwind (as I believe you need both), which has been working up until I tried to implement react-native-dotenv.

The problem occurs when I update my babel.config.js to the following:

module.exports = function (api) {
  api.cache(true);

  const presets = ["babel-preset-expo"];
  const plugins = [
    "nativewind/babel",
    [
      "module:react-native-dotenv",
      {
        moduleName: "@env",
        path: ".env",
      },
    ],
  ];
  return { presets, plugins };
};

Within the plugins...

If I remove nativewind/babel, the project loads with expo and works as intended (with no styling).

If I remove "module:react-native-dotenv..., the project loads with expo and works as intended with styling but no Dotenv functionality.

When I include BOTH of the plugins together in the babel.config.js file, it shows this error in the console:

Uncaught TypeError: nativewind__WEBPACK_IMPORTED_MODULE_0__.NativeWindStyleSheet is undefined js unitlessNumbers.js:76 Webpack 48 unitlessNumbers.js:76"

I've also tried to separate the plugins into different files and re-import them into babel.config.js with no luck.

I have used a .babelrc file along with babel.config.js with no luck either.

like image 541
foreff Avatar asked Feb 21 '26 06:02

foreff


1 Answers

I used to have similar problem, one of the possible solution is instead of using the module:react-native-dotenv plugin, you could try to use the babel-plugin-module-resolver package to resolve the path to the .env file, .

You can do this in your babel.config.js file:

return {
    presets: ["babel-preset-expo"],
    plugins: [
      "nativewind/babel",
      [
        "module-resolver",
        {
          alias: {
            "@env": "./.env",
          },
        },
      ],
    ],
  };

Good luck~

like image 108
Bryce Avatar answered Feb 23 '26 21:02

Bryce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!