dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

Fails for react-native app

Open bhanushukla opened this issue 8 years ago • 5 comments

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.

bhanushukla avatar Feb 05 '18 17:02 bhanushukla

no, this package purposely is focused on being used in the Node environment

maxbeatty avatar Feb 06 '18 05:02 maxbeatty

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.

enderandpeter avatar Feb 18 '19 23:02 enderandpeter

This one works pretty well with Expo.

waleedAlmubarak avatar May 16 '23 10:05 waleedAlmubarak

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.

austinlxx avatar Jun 01 '23 11:06 austinlxx

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, });

kwaref avatar Jan 02 '24 23:01 kwaref