我用 expo 创建了一个 react native 应用程序。我安装了 dotenv 包,我想制作一个 .env 文件并使用它,但我收到一条错误消息
Unable to resolve @env from ...
。我已经阅读了大量关于在 expo 中使用 .env 文件的文章和博客文章,但我不知道如何修复它。
代码如下:
babel.config.js
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo", "module:react-native-dotenv"],
};
};
.env
PORT=8082
App.js
import React from "react";
import {View,Text} from "react-native";
import {PORT} from "@env";
export function App() {
return (
<View><Text>{PORT}</Text></View>
);
}
答案 0 :(得分:3)
尝试将 "module:react-native-dotenv"
放在 plugins
中,如下所示:
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [["module:react-native-dotenv"]],
};
};
之后不要忘记重启你的 expo/npm 服务器。