ladjs/dotenv-parse-variables

Example from readme.md is broken

Grawl opened this issue · 8 comments

Grawl commented
import dotenv from 'dotenv';
import dotenvParseVariables from 'dotenv-parse-variables';

let env = dotenv.config({});
env = dotenvParseVariables(env);
➜ gulp --tasks
[10:13:58] Requiring external module babel-register
/Users/…/Sites/…/node_modules/dotenv-parse-variables/lib/index.js:39
  if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
            ^

TypeError: value.toLowerCase is not a function
    at parseKey (/Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:30:13)
    at /Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:10:35
    at Array.forEach (native)
    at exports.default (/Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:8:20)
    at Object.<anonymous> (/Users/…/Sites/…/gulp-config.js:5:7)
    at Module._compile (module.js:569:30)
    at loader (/Users/…/Sites/…/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/…/Sites/…/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)

I've reproduced this and working on a fix now.

The reason why this happens is because you do not have a .env file in the directory you're running your script from. I'll update the documentation for this to catch the error, but you should definitely have a .env file...

const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenv.config({})
if (env.error) throw env.error;
env = dotenvParseVariables(env);
const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenv.config({})
+if (env.error) throw env.error;
env = dotenvParseVariables(env);

I had the same issue with the example. The solution for me was to pass env.parsed instead of env to dotenvParseVariables. Like:

const dotenv = require('dotenv');
const dotenvParseVariables = require('dotenv-parse-variables');

let env = dotenv.config({})
if (env.error) throw env.error;
env = dotenvParseVariables(env.parsed);

@rodw1995 this is because you're missing a .env file, no??

@niftylettuce No, I have .env file. The variables of the file are PORT and ENABLE_HTTPS. When I load it with dotenv.config({}); the result is:

{ parsed:
   { PORT: '3000',
     ENABLE_HTTPS: '1'} }

So I need to pass env.parsed to dotenvParseVariables and then it's working!

I'am working on a Windows 10 operating system. The .ENV file is in the root of the project and it's loaded from src/server.js

But it's in the dotenv docs that the result of the loaded content is in result.parsed. So I get the expected result, right?

@rodw1995 thx for lmk, just updated the Readme + tests. I was on v2.x of dotenv with the tests, and the latest version of dotenv is v4.x, so that's why it wasn't working properly (v4.x uses env.parsed, not just env like older versions do).