Sibelius Seraphini Sibelius Seraphini - 1 year ago 83
Javascript Question

.env do not work with AVA

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' }
return, 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:

Answer Source

Took a quick look, here's a PR that gets things working:

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 config file to automatically look in your project root directory:

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

  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.