function MissingEnvVarsError(allowEmptyValues, dotenvFilename, sampleFilename, missingVars, error) {
Error.call(this);
this.name = this.constructor.name;
this.missing = missingVars;
this.sample = sampleFilename;
this.message = 'The folowing variables are defined in ' + sampleFilename + ' but are not defined in the environment: ' + missingVars
.join(', ') + '.\n' +
'Make sure to add them to ' + dotenvFilename + ' or directly to the environment.';
if (!allowEmptyValues) {
var emptyValuesMessage = 'If you expect any of these missing variables to be empty, you can use the allowEmptyValues option
:\n\n' +
"require('dotenv-safe').load({\n" +
' allowEmptyValues: true\n' +
'});';
this.message += '\n' + emptyValuesMessage;
}
if (error) {
var errorMessage = 'Also, the following error was thrown when trying to read variables from ' + dotenvFilename + ': ' +
error.message;
this.message += '\n' + errorMessage;
}
Error.captureStackTrace(this, this.constructor);
}n/a
function Error() { [native code] }n/a
config = function (options) {
options = options || {};
var dotenvResult = dotenv.load(options);
if (dotenvResult.error) {
dotenvResult.parsed = {};
}
var sample = options.sample || '.env.example';
var sampleVars = dotenv.parse(fs.readFileSync(sample));
var allowEmptyValues = options.allowEmptyValues || false;
var processEnv = allowEmptyValues ? process.env : compact(process.env);
var missing = difference(Object.keys(sampleVars), Object.keys(processEnv));
if (missing.length > 0) {
throw new MissingEnvVarsError(allowEmptyValues, options.path || '.env', sample, missing, dotenvResult.error);
}
// Key/value pairs defined in example file and resolved from environment
var required = Object.keys(sampleVars).reduce(function (acc, key) {
acc[key] = process.env[key];
return acc;
}, {});
var result = {
parsed: dotenvResult.parsed,
required: required
};
if (dotenvResult.error) {
result.error = dotenvResult.error;
}
return result;
}...
var options = {};
process.argv.forEach(function (val) {
var matches = val.match(/^dotenv_config_(.+)=(.+)/);
if (matches) {
options[matches[1]] = matches[2];
}
});
require('.').config(options);
})();
...load = function (options) {
options = options || {};
var dotenvResult = dotenv.load(options);
if (dotenvResult.error) {
dotenvResult.parsed = {};
}
var sample = options.sample || '.env.example';
var sampleVars = dotenv.parse(fs.readFileSync(sample));
var allowEmptyValues = options.allowEmptyValues || false;
var processEnv = allowEmptyValues ? process.env : compact(process.env);
var missing = difference(Object.keys(sampleVars), Object.keys(processEnv));
if (missing.length > 0) {
throw new MissingEnvVarsError(allowEmptyValues, options.path || '.env', sample, missing, dotenvResult.error);
}
// Key/value pairs defined in example file and resolved from environment
var required = Object.keys(sampleVars).reduce(function (acc, key) {
acc[key] = process.env[key];
return acc;
}, {});
var result = {
parsed: dotenvResult.parsed,
required: required
};
if (dotenvResult.error) {
result.error = dotenvResult.error;
}
return result;
}...
```dosini
# .env, private
SECRET=topsecret
TOKEN=
```
```js
require('dotenv-safe').load();
```
Since the provided `.env` file does not contain all the variables defined in
`.env.example`, an exception is thrown:
```
MissingEnvVarsError: The folowing variables are defined in .env.example but are not defined in the environment: TOKEN, KEY.
...function parse(src) {
var obj = {}
// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
// matching "KEY' and 'VAL' in 'KEY=VAL'
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]
// default undefined or missing values to empty string
var value = keyValueArr[2] ? keyValueArr[2] : ''
// expand newlines in quoted values
var len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()
obj[key] = value
}
})
return obj
}...
config: function (options) {
options = options || {};
var dotenvResult = dotenv.load(options);
if (dotenvResult.error) {
dotenvResult.parsed = {};
}
var sample = options.sample || '.env.example';
var sampleVars = dotenv.parse(fs.readFileSync(sample));
var allowEmptyValues = options.allowEmptyValues || false;
var processEnv = allowEmptyValues ? process.env : compact(process.env);
var missing = difference(Object.keys(sampleVars), Object.keys(processEnv));
if (missing.length > 0) {
throw new MissingEnvVarsError(allowEmptyValues, options.path || '.env', sample, missing, dotenvResult.error);
}
...function MissingEnvVarsError(allowEmptyValues, dotenvFilename, sampleFilename, missingVars, error) {
Error.call(this);
this.name = this.constructor.name;
this.missing = missingVars;
this.sample = sampleFilename;
this.message = 'The folowing variables are defined in ' + sampleFilename + ' but are not defined in the environment: ' + missingVars
.join(', ') + '.\n' +
'Make sure to add them to ' + dotenvFilename + ' or directly to the environment.';
if (!allowEmptyValues) {
var emptyValuesMessage = 'If you expect any of these missing variables to be empty, you can use the allowEmptyValues option
:\n\n' +
"require('dotenv-safe').load({\n" +
' allowEmptyValues: true\n' +
'});';
this.message += '\n' + emptyValuesMessage;
}
if (error) {
var errorMessage = 'Also, the following error was thrown when trying to read variables from ' + dotenvFilename + ': ' +
error.message;
this.message += '\n' + errorMessage;
}
Error.captureStackTrace(this, this.constructor);
}n/a
function Error() { [native code] }n/a
function Error() { [native code] }n/a
function captureStackTrace() { [native code] }n/a