erisanolasheni / custom-env Goto Github PK
View Code? Open in Web Editor NEWCustom env is a library built to make development more feasible by allowing multiple .env configurations for different environments
Custom env is a library built to make development more feasible by allowing multiple .env configurations for different environments
i have MAIL_PASS=hC5$3425g@ in .env
process.env.MAIL_PASS return hC5@
what i have to do
Hi,
I'm using custom-env version 2.0.1.
When running tests I get console warnings like:
console.warn
No env file present for the current environment: test
Falling back to .env config
This is because I just have a .env file, and no explicit .env.test version (and no real desire to just duplicate these values in two places!)
Is there a way to hide these warnings?
i.e. like this maybe
require('custom-env').quiet().env();
or maybe as a configuration option (though, without named options this gets ugly pretty quickly!)
require(`custom-env`).env(true, null, true, true); // 4th argument would be quiet or not.
Another option might be to somehow provide a custom logger which would default to console?
require('custom-env').logger(MyLogger).env();
Happy to go with whichever option fits your style, and I can have a poke at a pull request if you like?
After goes to 1.0.2 version, the property value like 'abc$2011' can be loaded correctly, the value been loaded will only have "abc". Is this supposed to work in this way?
At least at 1.0.0, ther is no such kind of issue.
Hello,
I am having issues loading the .env file based on NODE_ENV
require('custom-env').env(true) is not working, neither require('custom-env').env() - the env() function requires certain arguments.
Thoughts?
Inside the file:
variable=abc$123
getting output as abc and not abc$123
Tried to keep the value in single quotes.
Tried to use \ before $ as to escape the special character
Tried combination of both
Tried abc$\123, it gives output as abc123, but not abc$123
My current setup:
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
dotenvExpand(dotenv.config())
console.info('APP_ENV:', process.env.APP_ENV)
Trying to introduce custom-env and load .env.{stage_name}
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
const customEnv = require('custom-env').env('prod')
dotenvExpand(customEnv.config())
console.info('APP_ENV:', process.env.APP_ENV)
It will print the .env variable, not the .env.prod variable.
P.S. I can't seem to find how to use import rather than require.
I noticed during development, custom-env
was always falling back to the default .env
file. It seems as though there's some edge cases when using under Windows.
When setting NODE_ENV
and running through npm, it appears that NODE_ENV
can get a whitespace character appended under certain conditions. This was causing custom-env
to fail finding the file because https://github.com/erisanolasheni/custom-env/blob/master/lib/main.js#L85 was failing.
This can be replicated with the following:
package.json
"scripts": {
"start": "set NODE_ENV=development && node"
}
Terminal
$ npm start
> console.log(`|${process.env.NODE_ENV}|`)
|development |
After struggling with figuring out why my environment variables weren't present for NODE_ENV=dev, I looked into the code and learned the following:
Despite explicitly setting NODE_ENV=dev, due to the special treatment of that environment specifier it loads .env first if it is present. (I had both .env and .env.dev.) I think this would be unexpected in most cases - more specification should usually take precedent over less specification.
The documentation states a few times that if the environment is not specified, it is assumed to be development. This is not strictly true - if no environment is specified it attempts to load .env. (Full stop.) It doesn't necessarily imply anything about the runtime environment, just which file it will look for.
IMO it would be better to make the filename lookup consistent for all environment specifiers, so there are no 'surprise' special cases. Specifically, if the environment is 'dev', then .env.dev is loaded if present. A nice fallback might be to look for .env as a fallback for both the case where the environment is not specified (as currently) and for the case where the environment-specific file is not present (e.g. if env=dev and .env.dev is not available, it tries to load .env as a second priority).
if no match found for current NODE_ENV it should simply load a default .env.default file
main.js: 76
if (filesInDir instanceof Array && filesInDir.length > 0)
instanceof returns false even if filesInDir is actually a nonempty array. Use instead:
if (Array.isArray(filesInDir) && filesInDir.length > 0)
This works correctly.
Having duplicate envname
error. please find below line
node_modules/custom-env/lib/main.js: Duplicate declaration "envname"
59 | // Check if dir exists and its a directory
60 | var dir = typeof (dir) == 'string' ? dir : process.cwd()
> 61 | var envname = typeof (envname) == 'string' ? envname : envname === true && process.env.NODE_ENV ? process.env.NODE_ENV : false
| ^
62 |
63 |
64 | var dirIsDirectory = fs.lstatSync(dir).isDirectory()
Hello thanks for this but I am a bit confused on how to make the env automatically change if in production or development, i don't want to always be changing the value.
How to do so ?
require("custom-env").env("development"); // change based on current environment as cant do an if check since its at the top of file.
Thanks.
I have my .env.prod file a directory that is different from the script where I call the following
I'm calling the code below in /var/www/pm/bin/worker.js
require("custom-env").env("prod", '/var/www/pm/');
I have the .env.prod script in /var/www/pm/.env.prod
I tried to tell custom-env that the .env.prod file is in /var/www/pm but it still gives me an error
No env file present for the current environment: staging
I'm I doing something wrong?
Is there a way to re-require another env file?
So for example:
require('custom-env').env('development')
//do some stuff
require('custom-env').env('local')
//do some stuff
For those of us using TypeScript, it would be useful to have TypeScript typings for this library.
Thank you for creating this library, as it does solve the issue of managing multiple environment configurations very elegantly.
dotenv-expand
allows developers to reference process.env variables inside their .env
files.
For example:
IP=127.0.0.1
PORT=1234
APP_URL=http://${IP}:${PORT}
Using the above example .env
file, process.env.APP_URL
would be http://127.0.0.1:1234
.
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.