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

Can't access environment variable in app files- returns undefined #10283

Closed
7 tasks done
alessiogmonti opened this issue Sep 29, 2022 · 1 comment
Closed
7 tasks done
Labels
invalid This doesn't seem right

Comments

@alessiogmonti
Copy link

alessiogmonti commented Sep 29, 2022

Describe the bug

I have created a .env file in the root directory with the variable "VITE_ETHERS" :

VITE_ETHERS = {
    targetChainId: 'id',
    targetChainName: 'chainname',
  }

these are the entire content of the file.

console.log( import.meta.env.VITE_ETHERS.targetChainName) returns undefined.

I am not trying to access this variable outside my app source code, so I don't think I need to use loadEnv() in vite.config.js

Reproduction

https://stackblitz.com/edit/vitejs-vite-fqjcrb?file=main.js

System Info

System:
    OS: macOS 11.6.8
    CPU: (4) x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
    Memory: 32.04 MB / 8.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 18.2.0 - ~/.nvm/versions/node/v18.2.0/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.9.0 - ~/.nvm/versions/node/v18.2.0/bin/npm
  Browsers:
    Brave Browser: 105.1.43.93
    Chrome: 105.0.5195.125
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react: ^2.1.0 => 2.1.0 
    vite: ^3.1.0 => 3.1.3

Used Package Manager

npm

Logs

No response

Validations

@alessiogmonti alessiogmonti changed the title Can't access environment variable in app files- returns undefined or NaN Can't access environment variable in app files- returns undefined Sep 29, 2022
@sapphi-red
Copy link
Member

VIte's .env does not support multiline values (#10149).
So import.meta.env.VITE_ETHERS has {. Also even if it is supported env variables are string and JSON.parse will be needed.

@sapphi-red sapphi-red added invalid This doesn't seem right and removed pending triage labels Sep 29, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants