Sibelius Seraphini Sibelius Seraphini - 3 months ago 23
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' }
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

Answer

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 config 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.

Comments