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

.env file don't load in config #246

Closed
SimoMay opened this issue May 16, 2022 · 1 comment · Fixed by #347
Closed

.env file don't load in config #246

SimoMay opened this issue May 16, 2022 · 1 comment · Fixed by #347

Comments

@SimoMay
Copy link

SimoMay commented May 16, 2022

Environment

Node v16.15.0
npm 8.5.5
nitropack ^0.4.4

Reproduction

Reproducing this is simple, FYI, I'm using Nitro without Nuxt.
If you follow the documentation you would end up with something like this in nitro.config.ts:

import { defineNitroConfig } from 'nitropack'

export default defineNitroConfig({

  runtimeConfig: {
    supabase: {
      api_url: '',
      api_anon_key: ''
    }
  }
  // extends: [
  //   'config.dev.ts'
  // ]
})

And .envfile:

NITRO_SUPABASE_API_URL='xx'
NITRO_SUPABASE_API_ANON_KEY='yy'

Trying to read the values in a route with:

export default () => {
  const config = useRuntimeConfig()
  return config
}

you get empty values (the defaults).

{
  "app": {
    "baseURL": "/"
  },
  "nitro": {
    "routes": {}
  },
  "supabase": {
    "api_url": "",
    "api_anon_key": ""
  }
}

While the expected behavior is to find the values from the .env file:

{
  "app": {
    "baseURL": "/"
  },
  "nitro": {
    "routes": {}
  },
  "supabase": {
    "api_url": "xx",
    "api_anon_key": "yy"
  }
}

Describe the bug

I figured from reading the code, that Nitro suppose to get values from .env using unjs/c12 & dotenv while looping for values in the file that start with NITRO_ prefix, but it doesn't work (at least on my local)

I found a solution that works for me, which is importing dotenv at the start of nitro.config.ts, which solves this issue.

If this is the standard way to do so, then I assume an example of it in the documentation would be helpful (it took me a long time to figure it out myself)

If this is not the standard way, which is what I believe to be so, then importing dotenv in the beginning of the [src/options.ts](https://github.com/unjs/nitro/blob/b153cea1f2e0805088bdc512f490539cccec69c4/src/options.ts) could solve the issue.

Additional context

What solved it for me is adding import 'dotenv/config' at the top:

import 'dotenv/config'
import { defineNitroConfig } from 'nitropack'

export default defineNitroConfig({

  runtimeConfig: {
    supabase: {
      api_url: '',
      api_anon_key: ''
    }
  }
...

Logs

No response

@SimoMay
Copy link
Author

SimoMay commented May 16, 2022

Discussed in #238

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

Successfully merging a pull request may close this issue.

1 participant