@aetherinox/noxenv
v1.0.0
Published
Set and use environment variables across different platforms.
Downloads
226
Maintainers
Readme
About
Most Windows-based command prompts / terminals will have issues when you attempt to set env variables utilizing NODE_ENV=production
; unless you are using Bash on Windows. Coupled with the issue that Windows and *NIX have different ways of utilizing env variables such as
- Windows:
%ENV_VAR%
- *NIX:
$ENV_VAR
noxenv
gives you the ability to have a single command at your disposal; without the hassle of worrying about different platforms. Specify your env variable as you would on *nix, and noxenv will take care of the conversion for Windows users.
[!NOTE]
noxenv
only supports Node.js v14 and higher.
Installation
This module is distributed via npm which is bundled with node. To utilize this moduke, add it to your project's package.json
-> devDependencies
:
"devDependencies": {
"@aetherinox/noxenv": "^1.0.0"
},
To install it via the npm command-line as a devDependency
:
npm i --save-dev @aetherinox/noxenv
Usage
Some examples have been provided below to show various ways of using noxenv:
{
"scripts": {
"build": "noxenv NODE_ENV=production webpack --config build/webpack.config.js",
"build-rollup": "noxenv NODE_ENV=production rollup -c",
"development": "noxenv NODE_ENV=development npm start",
"production": "noxenv NODE_ENV=production SERVER_IP=http://127.0.0.1 npm start",
"test": "noxenv BABEL_ENV=test jest test/app",
"start-dev": "noxenv NODE_ENV=development PORT_ENV=2350 npm run build && node dist/src/main.js",
"openssl-legacy": "noxenv NODE_OPTIONS=\"--openssl-legacy-provider\" tsc -p tsconfig.json"
}
}
Inside your module, you can call these env variables with something similar to the below example:
const TEST = { api: "f4dcc990-f8f7-4343-b852-a2065b4445d5" };
const PROD = { api: "d1ac1eb8-7194-4095-8976-04be09378611" };
let target;
if (process.env.BABEL_ENV === "test") {
target = TEST;
} else if (process.env.BABEL_ENV === "prod") {
target = PROD;
}
console.log(`Your API key is ${target} in ${process.env.BABEL_ENV} mode`);
In the above example, variables such as BABEL_ENV
will be set by noxenv
.
You can also set multiple environment variables at a time:
{
"scripts": {
"release-beta": "noxenv RELEASE=beta ENV=dev PORT=7732 npm run release && npm run start",
}
}
Additionally; you can split the command into several actions, or separate the env variable declarations from the actual command execution; note the following example:
{
"scripts": {
"main": "noxenv USER_NAME=\"Joe\" npm run child",
"child": "noxenv-shell \"echo Hello $USER_NAME\""
}
}
In the above example, child
stores the actual command to execute, and main
sets the env variable that is going to be used. In this case, Joe
is the user we want to say hello to, so we store Joe's name in within the env variable USER_NAME
, and then at the end of main
, we call child
which does the actual greeting.
This means that you only need to call main
, and both will be ran. Additionally, it also means that you can also call the env variable using $USER_NAME
on Windows, even though the typical Windows syntax is %USER_NAME%
.
If you wish to pass a JSON string (such as when using ts-loader), you may do the following:
{
"scripts": {
"test": "noxenv TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} mocha --config ./test.js"
}
}
Pay attention to the triple backslash (\\\)
before the double quotes (")
and the absence of single quotes (')
. Both of these conditions MUST be met in order to work on both on Windows and *NIX.
noxenv
& noxenv-shell
noxenv
provides two binary files: noxenv
and noxenv-shell
.
| Binary | Description |
| --- | --- |
| noxenv
| Executes commands utilizing cross-spawn
|
| noxenv-shell
| Executes commands utilizing node's shell
. Useful when you need an env var to be set across an entire shell script, rather than a single command. Also used when wanting to pass a command that contains special shell characters that you need interpreted |
If you want to have the env variable apply to several commands in a series, you will need to wrap them in quotes and use noxenv-shell
, instead of noxenv
.
{
"scripts": {
"salutation": "noxenv-shell SALUTATION=Howdy NAME=Aetherinox \"echo $SALUTATION && echo $NAME\""
}
}
If you need to handle signal events within your project, use noxenv-shell
. An example use for this is when you want to capture the SIGINT
event invoked by pressing Ctrl + C
within your command-line interface.
Windows Users
Note that npm
uses cmd
by default and doesn't support command substitution, so if you want to leverage that, then you need to update your .npmrc
and set script-shell
to powershell.
Contributors ✨
The following people have helped get this project going: