Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't default export from(process.env) for environments that don't have process #155

Closed
smblee opened this issue Jul 6, 2022 · 6 comments

Comments

@smblee
Copy link

smblee commented Jul 6, 2022

Trying to use this lib with vite which uses import.meta.env as the default location for env vars and I get the following error

process is not defined
    at node_modules/env-var/env-var.js (env-var.js:55:23)

and looks like it's happening because the module exports from(process.env) by default and its failing since process doesn't actually exist within the vite environment i am in (sveltekit).

module.exports = from(process.env)

The code:

import { from } from 'env-var';

const env = from(import.meta.env);

export const API_BASE_URL = env.get('VITE_API_BASE_URL').required().asUrlString();
@evanshortiss
Copy link
Owner

Thanks for raising this issue @smblee. I think it's a good point, and the module probably shouldn't assume that it's operating in a Node.js environment.

One problem is that I think this will break backwards compatibility, unless the code is changed to use some sort of conditional check.

If you change the line causing the issue to this, does it fix the issue?

module.exports = from(typeof process === 'undefined' ? {} : process.env) 

@evanshortiss
Copy link
Owner

@smblee any luck with that fix? If so I can merge it into this module and push a patch release.

@evanshortiss evanshortiss mentioned this issue Jul 13, 2022
10 tasks
@evanshortiss
Copy link
Owner

evanshortiss commented Dec 25, 2022

This is available in the beta release of version 8. You can install and test it using npm install env-var@beta. Feel free to reopen this issue if necessary.

EDIT: See my latest comment in this thread for a fix with v7.x

@CassandraNewell
Copy link

hi @evanshortiss , thanks for your work on this! Wanted to let you know that the beta worked just fine in my Vite React app -- head's up to anyone reading this, I had to place import.meta.env in an object like so:

const env = from({ variables: import.meta.env });

@kenberkeley
Copy link

@evanshortiss May I ask when you will release the beta version?

@evanshortiss
Copy link
Owner

evanshortiss commented Aug 21, 2023

@kenberkeley I have released v7.4.0 with this fix. You can now do the following for Vite environments:

const env = from(import.meta.env);

Or in React with create-react-app, you need to explicitly reference each variable like:

const env = from({
  SOME_VARIABLE: process.env.REACT_APP_SOME_VARIABLE
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants