
Security News
npm βisβ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
@kth/default-envs
Advanced tools
Process env:s that are not configured on start up, but accessed * as envs in the application are added with there default values.Default paths and error pages for uri:s.
@kth/default-envs
Process env:s that are not configured on start up, but accessed via process.env.ENV_NAME in the application are added with there default values, as specified as a key-value object.
const defaultEnvs = require("@kth/default-envs");
const DEFAULTS = {
LOG_LEVEL: "info",
PORT: 80,
APPINSIGHTS_INSTRUMENTATIONKEY: "",
};
defaultEnvs.set(DEFAULTS);
console.log(process.env.PORT); // 80
If an env is set on startup it will be used, not the default.
APPINSIGHTS_INSTRUMENTATIONKEY="abc-123" node app.js
const defaultEnvs = require("@kth/default-envs");
const DEFAULTS = {
LOG_LEVEL: "info",
PORT: 80,
APPINSIGHTS_INSTRUMENTATIONKEY: "",
};
defaultEnvs.set(DEFAULTS);
console.log(process.env.APPINSIGHTS_INSTRUMENTATIONKEY); // abc-123
Some envs do not have defaults and must exist before starting your service.
defaultEnvs.required([], console) will print information about if a logger is passed, and if the env is missing when invoking required
throw an error Required env 'PASSWORD' does not exist. .
node app.js
const defaultEnvs = require("@kth/default-envs");
const DEFAULTS = {
USER: "admin",
URI: 'example.com'
};
defaultEnvs.set(DEFAULTS);
defaultEnvs.required(['PASSWORD']); // Exception:Required env 'PASSWORD' does not exist.
PASSWORD='s3cret' node app.js
const defaultEnvs = require("@kth/default-envs");
const DEFAULTS = {
USER: "admin",
URI: 'example.com'
};
defaultEnvs.set(DEFAULTS);
defaultEnvs.required(['PASSWORD']);
console.log(process.env.PASSWORD); // s3cret
If you pass a logger like console
or any other that implements logger functions debug, info or warn you will get information about what defaults are used when invoking defaultEnvs.set({}, logger);
TOKEN="xxxx-yyyy" PORT=3000 node app.js
const defaultEnvs = require("@kth/default-envs");
const DEFAULTS = {
LOG_LEVEL: "info",
PORT: 80,
APPINSIGHTS_INSTRUMENTATIONKEY: "",
};
defaultEnvs.set(DEFAULTS, console);
defaultEnvs.required(['TOKEN', 'PASSWORD']);
08:57:00.808Z INFO my-app: - Env 'LOG_LEVEL' is not set, defaulting to 'info'.
08:57:00.811Z INFO my-app: - Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
08:57:00.811Z INFO my-app: - β
Found required env 'TOKEN'
08:57:00.811Z INFO my-app: - π¨ Missing required env 'PASSWORD'
08:57:00.811Z WARN my-app: Required env 'PASSWORD' does not exist.
Exception: Required env 'PASSWORD' does not exist.
npm install
npm test
Default Envs
β Throw an error if a required env is missing.
β Do not throw an error if a required env exists.
β If a default value is set you can access it wia process.env.
β If a env is already set prior to running set(defaults), process.env will return it.
β After runnign unset() all defaults values are removed from process.env array.
β After runnign unset() all emvs set on startup are still availible.
β If a logger is passed to the set({}, logger), use it to log.
7 passing (10ms)
npm install
npm run ok
> default-envs-demo@0.0.1 ok
> PASSWORD='s3cret' TOKEN='xxxx-1111' APPLICATION_NAME='Super default-envs-demo π' node demo.js
- Env 'LOG_LEVEL' is not set, defaulting to 'info'.
- Env 'PORT' is not set, defaulting to '3000'.
- Env 'API_HOST' is not set, defaulting to 'https://api.kth.se'.
- Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
- β
Found required env 'PASSWORD'
- β
Found required env 'TOKEN'
Application name: Super default-envs-demo π
npm run fail
> default-envs-demo@0.0.1 fail
> TOKEN='xxxx-1111' node demo.js
- Env 'APPLICATION_NAME' is not set, defaulting to 'Demo-app'.
- Env 'LOG_LEVEL' is not set, defaulting to 'info'.
- Env 'PORT' is not set, defaulting to '3000'.
- Env 'API_HOST' is not set, defaulting to 'https://api.kth.se'.
- Env 'APPINSIGHTS_INSTRUMENTATIONKEY' is not set, defaulting to ''.
- π¨ Missing required env 'PASSWORD'
- β
Found required env 'TOKEN'
Required env 'PASSWORD' does not exist.
/Users/patricjansson/dev/kth/gita.sys.kth.se/default-envs/demo/node_modules/@kth/default-envs/index.js:69
throw message;
^
Required env 'PASSWORD' does not exist.
(Use `node --trace-uncaught ...` to show where the exception was thrown)
FAQs
Process env:s that are not configured on start up, but accessed * as envs in the application are added with there default values.Default paths and error pages for uri:s.
The npm package @kth/default-envs receives a total of 1 weekly downloads. As such, @kth/default-envs popularity was classified as not popular.
We found that @kth/default-envs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.