Fails for react-native app
The package at "./node_modules/dotenv/lib/main.js" attempted to import the Node standard library module "fs". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo
Can this package be provided without dependency on NOde standard library so the it can be used with React Native also.
no, this package purposely is focused on being used in the Node environment
It took me quite a bit of painful cycles of trial and error, but I have found this package to be the .env solution that we react-native people are looking for... until everything changes a month from now, of course.
Don't bother trying to get regular dotenv to work. I have sunk down the rabbit hole, climbed back out, and have nothing but horror stories to share.
This one works pretty well with Expo.
As of Expo 49 (still in alpha on this date), we can prefix env variables with EXPO_PUBLIC_ within an .env file and they will appear on the client. See documentation.
If you are also building for web, you must switch to Metro bundler instead of webpack.
There is a way of using dotenv with expo. Do not try to access environment variables like process.env.APOLLO_URI outside the app.config.js file. Instead use the Constants.manifest.extra. In addition, do not import the dotenv library outside the app.config.js file or you will most likely face errors. this is an example app.config.js (by deafault it is app.json, you need to transform it)
import 'dotenv/config'
export default { name: "rate-repository-app", slug: "rate-repository-app", version: "1.0.0", orientation: "portrait", icon: "./assets/icon.png", userInterfaceStyle: "light", splash: { image: "./assets/splash.png", resizeMode: "contain", backgroundColor: "#ffffff" }, updates: { fallbackToCacheTimeout: 0 }, assetBundlePatterns: [ "**/*" ], ios: { supportsTablet: true }, android: { adaptiveIcon: { foregroundImage: "./assets/adaptive-icon.png", backgroundColor: "#FFFFFF" } }, web: { favicon: "./assets/favicon.png" }, extra: { env: process.env.ENV, apolloUri: process.env.APOLLO_URI }, }
then, the variables you defined in the extra object can be used by importing COnstants like the example:
import Constants from 'expo-constants';
const httpLink = createHttpLink({ uri: Constants.manifest.extra.apolloUri, });