0

I'm receiving the following error, when I try to run my tests using ava with my project that uses dotenv-safe

{ [Error: ENOENT: no such file or directory, open '.env'] errno: -2, code: 'ENOENT', syscall: 'open', path: '.env' }
fs.js:549
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open '.env.example'

it looks like ava is not finding .env or .env.example files

is there any workaround or a solution for this?

this is a repo that u can easily reproduce the issue: https://github.com/sibelius/koa-env-ava

Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55

1 Answers1

2

Took a quick look, here's a PR that gets things working: https://github.com/sibelius/koa-env-ava/pull/1

What was happening was that dotenv-safe did not know where to find your .env and .env.example files, so it would look in the src directory and throw an error. I rewrote your src/config.js file to automatically look in your project root directory:

const root = require('path').join.bind(this, __dirname, '..');

require('dotenv-safe').load({
  path: root('.env'),
  sample: root('.env.example')
});

export const API_URL = process.env.API_URL;
export const SERVER_PORT = process.env.SERVER_PORT;

This way dotenv-safe knows where to load your .env files from.

JaKXz
  • 1,665
  • 1
  • 14
  • 34