Giter Site home page Giter Site logo

Comments (13)

barbogast avatar barbogast commented on July 23, 2024

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.

af avatar af commented on July 23, 2024

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.

af avatar af commented on July 23, 2024

master now has your suggested change, could you give it a try and let me know if it works for you now?

from envalid.

barbogast avatar barbogast commented on July 23, 2024

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.

af avatar af commented on July 23, 2024

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.

barbogast avatar barbogast commented on July 23, 2024

Hehe, nice idea. Yes, I'll test it. I'm not at home today, so it will be tomorrow.

from envalid.

barbogast avatar barbogast commented on July 23, 2024

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:

  1. Use a custom reporter and
  • either conditionally require reporter from index.js
  • or conditionally require chalk from reporter.js
  1. Try to get has-flag to handle process.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.

SimenB avatar SimenB commented on July 23, 2024

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.

barbogast avatar barbogast commented on July 23, 2024

I just created a PR for has-flag. Depending if it is accepted we can add the conditional require for reporter.js.

from envalid.

af avatar af commented on July 23, 2024

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.

SimenB avatar SimenB commented on July 23, 2024

I think envalid can conditionally require chalk in order to make a workaround available

from envalid.

barbogast avatar barbogast commented on July 23, 2024

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.

barbogast avatar barbogast commented on July 23, 2024

I just created a new PR: barbogast#2

from envalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.