ladjs / dotenv-parse-variables Goto Github PK
View Code? Open in Web Editor NEWParse dotenv files for Boolean, Array, and Number variable types, built for Lad
Home Page: https://lad.js.org
License: MIT License
Parse dotenv files for Boolean, Array, and Number variable types, built for Lad
Home Page: https://lad.js.org
License: MIT License
Looks like inline comments in .env aren't handled?
e.g a .env containing the following:
LIMITER_MAX_CALL=50 # limit number of calls to this value per LIMITER_TIME_INTERVAL
returns in env
{
...
LIMITER_MAX_CALL="50 # limit number of calls to this value per LIMITER_TIME_INTERVAL"
...
}
Ideally everything from "#" onward should be stripped when parsing.
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)
If my .env has a variable that should be a string, but consists only of digits:
AWS_ACCESS_KEY_ID="123"
It will get parsed to number. How may I precent this? Are the quotation marks ignored?
Have you considered supporting parsing objects?
e.g. SOME_VARIABLE={"test": "hello"}, currently, we get SOME_VARIABLE: '{"test": "hello"}', but since that string is JSON parsible, would be nice to parse it into an object.
What's wrong with boolean parse?
require('dotenv').load();
const dotenvParseVariables = require('dotenv-parse-variables');
console.log(process.env);
process.env = dotenvParseVariables(process.env);
console.log(process.env);
result:
{
"DEV": "true",
"WWW": "http://localhost:3000",
"PORT": "3000",
"MONGODB_DSN": "mongodb://127.0.0.1/test",
"MONGODB_DEBUG": "true"
}
{
"DEV": "true",
"WWW": "http://localhost:3000",
"PORT": "3000",
"MONGODB_DSN": "mongodb://127.0.0.1/test",
"MONGODB_DEBUG": "true"
}
Currently when creating an array with just one item, it creates an array of two:
CORS_ORIGINS=myurl.com,
=> ['myurl.com', '']
It would be nice to be able to create an array even if there is just one item.
I'm probably doing something really wrong, but i can't find what...
import dotenv from 'dotenv'
import dotenvParseVariables from 'dotenv-parse-variables'
let env = dotenv.config()
if (env.error || !env.parsed) throw env.error
env = dotenvParseVariables(env.parsed, { assignToProcessEnv: true, overrideProcessEnv: true })
console.log(process.env)
My .env file:
PORT=3000
JWT_SECRET=token
What I got from process.env:
{
PORT: '3000', // String
JWT_SECRET: 'token'
}
What I expect:
{
PORT: 3000, // Number
JWT_SECRET: 'clinica'
}
What I'm doing wrong?
I have a token to access a third party API. This token contains a comma ",". In my .env file the token is saved as:
TOKEN=abcde,fghij
When I parse the .env variables with dotenv-parse-variables, the token is parsed as an array, e.g.
['abcde', 'fghij'] But of course I just want a string. Is this possible? Also with an asterisk, I am not able to get the desired result. Can you please help me?
Great library!
I'm using this with react-native-config, which includes a helper function in its configuration object. This function must first be manually stripped before passing the config to dotenv-parse-variables
, otherwise it will throw an error: TypeError: value.indexOf is not a function
.
As a convenience, it would be great if dotenv-parse-variables
ignored any functions in the object being parsed.
Thanks!
Node.js version: 20
Description: Big numbers like "12345678901234567" are parsed incorrectly to number
"12345678901234567" becomes 12345678901234568 (number)
"12345678901234567" becomes 12345678901234567 (bigint)
or
"12345678901234567" stays as "12345678901234567" (string)
.env:
ENV_VAR="12345678901234567"
Not relevant to this lib
Is there a quick way to add the parsed variables to process.env
, or do I have to create my own solution?
The README gives an example with console.log(process.env);
which outputs type-casted variables e.g. BAZ: 2 vs BAZ: '2'.
This is not the case - any variable written back to process.env will be output as a string, and it does not appear possible to set and then read a type-casted variable from process.env.
The assignToProcessEnv and overrideProcessEnv work, they set the value to process.env as per the logic, but the type is lost.
You need to use the output of dotenvExtended.load() to see the type-casted variables, however this does not contain anything existing from process.env - so there is no place to get an entire process.env variable list with type-cast.
The way i have achieved this is by passing a copy of process.env into the package e.g.
const loadedDotEnv = dotenvExtended.load({
silent: false,
errorOnMissing: true
});
const env = require('dotenv-parse-variables')(Object.assign({}, process.env));
console.log(env);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.