Comments (13)
Hmm... completely removing dotenv
would be a breaking change which would potentially force a lot of users to adjust their code. Another way to solve this issue would be to inline the require
calls for fs
and dotenv
into extendWithDotEnv()
. Then the breaking change could be avoided.
from envalid.
Cool idea, I would also like to see the library be environment-agnostic. Adding dotenv is a relatively recent change, but it does make things significantly more convenient for the primary use case (server-side env validation). So I definitely want to keep it in, but am very much open to an option to disable it for use cases like yours
from envalid.
master
now has your suggested change, could you give it a try and let me know if it works for you now?
from envalid.
Thanks a lot for working on this :-)
Unfortunately it doesn't work. I didn't actually try my suggestion (sorry), and it turns out that the packager of react-native somehow analyzes the code before it is packaged and will stumble on the inlined require
calls even without executing them. There are quite some issues in the react-native repository about this behavior but I couldn't find a nice solution.
So the only solution I can think of right now is to completely remove the dependency. But I could understand if you don't want to do this.
from envalid.
Ah that's right, I remember that gotcha from RN. Can you try latest master locally, but change require('dotenv')
to require('dot' + 'env')
? (and do something similar for 'fs'
) I seem to recall that RN doesn't support dynamic require statements, but I'm not sure if it would throw an error in this case
from envalid.
Hehe, nice idea. Yes, I'll test it. I'm not at home today, so it will be tomorrow.
from envalid.
Allright, I got it working, but it isn't nice.
The require-calls work with
const fs = require('f'+'s')
const dotenv = require('d'+'otenv')
But then the next error occurs when chalk
is required by reporter.js
. chalk
requires supports-color
which requires has-flag
which tries to do process.argv.indexOf
. And process.args
is undefined in react-native.
So I see the following solutions:
- Use a custom reporter and
- either conditionally require
reporter
fromindex.js
- or conditionally require
chalk
fromreporter.js
- Try to get
has-flag
to handleprocess.argv
being undefined.
Fixing has-flag
is probably a good thing since it's a trivial change and it feels right that has-flag
should be able to handle this case. Depending how the packages depend on each other (~ or ^) it could take a while until the change is available in chalk.
Which option do you prefer?
from envalid.
Conditionally requiring the reporter can probably be done. Since you can provide your own, this might be a good thing either way.
I find it weird that react-native is so strict on process usage though. React has a widespread use of process.env
, why can't it just set process.argv
in the same way?
from envalid.
I just created a PR for has-flag
. Depending if it is accepted we can add the conditional require for reporter.js
.
from envalid.
Looks like the has-flag PR isn't going forward. I can see the maintainer's point– this is kind of a flaw with how react-native only partially stubs out the process
object.
Since this looks like it'd be a pretty invasive change, I'd recommend forking envalid instead and removing the parts that are problematic with RN. If RN fixes this issue down the road, we can revisit making envalid work better with it.
from envalid.
I think envalid can conditionally require chalk in order to make a workaround available
from envalid.
Alright, I just created a fork with the changes required to get it working in react-native. The changes are minimal but the solution doesn't look nice.
I just made a PR in case you want to pull it.
Anyway, thanks for your support!
from envalid.
I just created a new PR: barbogast#2
from envalid.
Related Issues (20)
- Add parse options to str() HOT 1
- array validator HOT 1
- 7.3.0 breaking change? HOT 11
- Add option to load validator specs from .example.env file HOT 1
- Access value of env object by name HOT 1
- envalid in react-native for preview crashes without giving description of missing env HOT 2
- testOnly setting variable to Symbol instead of failing HOT 5
- Pick from validators HOT 2
- str({ devDefault: undefined }) generates an incorrect type HOT 2
- Require environment variables only if `NODE_ENV` is not set to `test` HOT 2
- Skipping env var validation in tests HOT 1
- Prevent process exit during tests HOT 1
- makeExactValidator is not exported HOT 1
- Combine envalid with Docker secrets
- Issue with envalid in test. HOT 1
- Typescript, typing the environment variables returns never HOT 1
- Waiting for an async reporter callback HOT 3
- How to see description and help for optional env? HOT 4
- Add a warn option
- devDefault does not work when NODE_ENV is undefined HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from envalid.