# useDotenv Understanding

**URL:** <https://forum.serverless.com/t/usedotenv-understanding/18352>\
**Category:** Serverless Framework\
**Created:** [February 23, 2023, 9:52pm UTC](https://forum.serverless.com/t/usedotenv-understanding/18352 "2023-02-23T21:52:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![redeemefy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/redeemefy/32/6915_2.png) [@redeemefy](https://forum.serverless.com/u/redeemefy)\
**Post date:** [February 23, 2023, 9:52pm UTC](https://forum.serverless.com/t/usedotenv-understanding/18352/1 "2023-02-23T21:52:23Z")

</div>

I’m trying to understand how `useDotenv` really works. It is not clear to me where those env variables are available when you have the `useDotenv: true` in your `serverless.ts`. My use case is to use those variable inside the `serverless.ts` itslef for dynamically create db connections based on `.env.{stage}` as the blog post states [here](https://www.serverless.com/framework/docs/environment-variables).

```typescript
...
useDotenv: true,

...
provider: {
  ...
  stage: '${opt:stage, "development"}',
  ...,
  environment: {
    DB_NAME: '${env:DB_NAME}',
    DB_USER: '${env:DB_USER}',
    DB_PASSWORD: '${env:DB_PASSWORD}',
    READER: '${env:READER}',
    WRITER: '${env:WRITER}',
    DB_PORT: '${env:DB_PORT}',
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
  }
  ...
}

```

But I’m getting an error that `Cannot resolve serverless.ts: Variables resolution errored with

```shell
Cannot resolve serverless.ts: Variables resolution errored with:
  - Cannot resolve variable at "provider.environment.DB_NAME": Value not found at "env" source,
  - Cannot resolve variable at "provider.environment.DB_USER": Value not found at "env" source,
  - Cannot resolve variable at "provider.environment.DB_PASSWORD": Value not found at "env" source,
  - Cannot resolve variable at "provider.environment.READER": Value not found at "env" source,
  - Cannot resolve variable at "provider.environment.WRITER": Value not found at "env" source,
  - Cannot resolve variable at "provider.environment.DB_PORT": Value not found at "env" source

```

- Where do I have access to those variable assuming that the file got loaded and parsed by serverless?
- If `useDotenv` doesn’t give access to those variables inside the `serverless.ts`… Are some solutions that I do have access to those variable?

---

<div class="post-metadata">

**Author:** ![jayz14](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jayz14](https://forum.serverless.com/u/jayz14)\
**Post date:** [May 30, 2023, 6:42am UTC](https://forum.serverless.com/t/usedotenv-understanding/18352/2 "2023-05-30T06:42:46Z")

</div>

hi @redeemefy  
Where you able to resolve this? I am facing similar issue.

---

<div class="post-metadata">

**Author:** ![dazes97](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dazes97/32/7962_2.png) [@dazes97](https://forum.serverless.com/u/dazes97)\
**Post date:** [July 24, 2024, 6:00pm UTC](https://forum.serverless.com/t/usedotenv-understanding/18352/3 "2024-07-24T18:00:11Z")

</div>

I resolved this by setting useDotenv: true under the service: xxxxxx, and by creating a .env file in the directory containing serverless.yml.
