Solve the conflict between NativeWind and react-native-dotenv in React Native
P粉135799949
P粉135799949 2024-03-29 20:48:45
0
1
431

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) and they were working until I tried to implement react-native-dotenv.

The problem occurs when I update 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 };
};

Inside the plug-in...

If I remove nativewind/babel, the project loads expo and works as expected (no styles).

If I remove "module:react-native-dotenv..., the project loads expo and works as expected, but without the Dotenv functionality.

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

Uncaught TypeError: nativewind__WEBPACK_IMPORTED_MODULE_0__.NativeWindStyleSheet is not defined jsunitlessNumbers.js: 76 Webpack 48 unitlessNumbers.js:76"

I also tried separating the plugins into different files and re-importing them into babel.config.js without success.

I used the .babelrc file as well as the babel.config.js but no luck either.

P粉135799949
P粉135799949

reply all(1)
P粉766520991

I once encountered a similar problem, one of the possible solutions is not to use the module:react-native-dotenv plugin, you can try to use babel-plugin-module-resolver Package to parse paths to .env files, .

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

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

Wish you good luck~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template