how to config custom process.env in Node.js All In One
how to config custom process.env in Node.js All In One
process.env
APP_ENV
NODE_ENV
https://nodejs.org/api/process.html#process_process_env
GITHUB_API_ACCESS_TOKEN
#!/usr/bin/env node
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-0
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link https://www.cnblogs.com/xgqfrms/p/13500721.html#4658099
* @solutions
*
*/
const log = console.log;
log(`process.env =\n`, process.env)
log(`process.env.GITHUB_API_ACCESS_TOKEN =\n`, process.env.GITHUB_API_ACCESS_TOKEN)
// const access_token = process.env.GITHUB_API_ACCESS_TOKEN;
btoa(`process.env.GITHUB_API_ACCESS_TOKEN`)
// "cHJvSV9BQXYuY2Vzcy5lbn0FQRY0NFU1NfVE90lUSFVCZXLRU4="
# bin cli
$ chmod +x ./env.js
# OR
$ chmod 777 ./env.js
$ export GITHUB_API_ACCESS_TOKEN=cHJvSV9BQXYuY2Vzcy5lbn0FQRY0NFU1NfVE90lUSFVCZXLRU4== && ./env.js
# Linux / macOS, using export command
$ export GITHUB_API_ACCESS_TOKEN=cHJvSV9BQXYuY2Vzcy5lbn0FQRY0NFU1NfVE90lUSFVCZXLRU4==
# Windows, using set command
$ set GITHUB_API_ACCESS_TOKEN=cHJvSV9BQXYuY2Vzcy5lbn0FQRY0NFU1NfVE90lUSFVCZXLRU4==
临时
全局环境变量
- Linux /
macOS
# 设置 NODE_ENV 环境变量
$ export NODE_ENV=production
# 清除 NODE_ENV 环境变量, 覆盖为空
$ export NODE_ENV=
# ✅
$ unset NODE_ENV
- Windows
# 设置 NODE_ENV 环境变量
$ set NODE_ENV=production
# 清除 NODE_ENV 环境变量
$ set NODE_ENV=
永久
全局环境变量(⚠️ 不推荐,不够灵活)
修改系统的配置文件 $PATH
- vim
- VS Code
- GUI
# 当前用户
$ vim ~/.zshrc
# OR
$ vim ~/.bash_profile
# 在文件进行环境变量的设置或修改
# export NODE_ENV_PROD = production
# export NODE_ENV_DEV = development
# 修改后,需要刷新
$ source ~/.zshrc
# OR
$ source ~/.bash_profile
Flutter & Dart
github-user-language-stats bug
# cli
$ npm i -g github-user-language-stats
# process.env.GITHUB_API_ACCESS_TOKEN
$ export GITHUB_API_ACCESS_TOKEN=<your token> && gh-lang-stat <github username>
# demo
$ export GITHUB_API_ACCESS_TOKEN=cHJvSV9BQXYuY2Vzcy5lbn0FQRY0NFU1NfVE90lUSFVCZXLRU4== && gh-lang-stat xgqfrms
set
& unset
set [ {+|-}options | {+|-}o [ option_name ] ] ... [ {+|-}A [ name ] ]
[ arg ... ]
Set the options for the shell and/or set the positional
parameters, or declare and set an array. If the -s option is
given, it causes the specified arguments to be sorted before
assigning them to the positional parameters (or to the array
name if -A is used). With +s sort arguments in descending
order. For the meaning of the other flags, see zshoptions(1).
Flags may be specified by name using the -o option. If no option
name is supplied with -o, the current option states are printed:
see the description of setopt below for more information on the
format. With +o they are printed in a form that can be used as
input to the shell.
If the -A flag is specified, name is set to an array containing
the given args; if no name is specified, all arrays are printed
together with their values.
If +A is used and name is an array, the given arguments will
replace the initial elements of that array; if no name is
specified, all arrays are printed without their values.
The behaviour of arguments after -A name or +A name depends on
whether the option KSH_ARRAYS is set. If it is not set, all
arguments following name are treated as values for the array,
regardless of their form. If the option is set, normal option
processing continues at that point; only regular arguments are
treated as values for the array. This means that
set -A array -x -- foo
sets array to `-x -- foo' if KSH_ARRAYS is not set, but sets the
array to foo and turns on the option `-x' if it is set.
If the -A flag is not present, but there are arguments beyond
the options, the positional parameters are set. If the option
list (if any) is terminated by `--', and there are no further
arguments, the positional parameters will be unset.
If no arguments and no `--' are given, then the names and values
of all parameters are printed on the standard output. If the
only argument is `+', the names of all parameters are printed.
For historical reasons, `set -' is treated as `set +xv' and `set
- args' as `set +xv -- args' when in any other emulation mode
than zsh's native mode.
$ man zshbuiltins
# $ man zshbuiltins | grep set
# Unknown locale, assuming C
$ locale
LANG=""
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=
# fix
$ export LANG="en_US.UTF-8"
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
https://pubs.opengroup.org/onlinepubs/007904875/utilities/set.html
unset [ -fmv ] name ...
Each named parameter is unset. Local parameters remain local
even if unset; they appear unset within scope, but the previous
value will still reappear when the scope ends.
Individual elements of associative array parameters may be unset
by using subscript syntax on name, which should be quoted (or
the entire command prefixed with noglob) to protect the
subscript from filename generation.
If the -m flag is specified the arguments are taken as patterns
(should be quoted) and all parameters with matching names are
unset. Note that this cannot be used when unsetting associative
array elements, as the subscript will be treated as part of the
pattern.
The -v flag specifies that name refers to parameters. This is
the default behaviour.
unset -f is equivalent to unfunction.
https://pubs.opengroup.org/onlinepubs/007904875/utilities/unset.html
refs
https://www.cnblogs.com/xgqfrms/p/13500721.html#4658099
https://nodejs.org/api/process.html#process_process_env
https://github.com/kentcdodds/cross-env
https://www.cnblogs.com/sorex/p/6200940.html
https://segmentfault.com/a/1190000011683741
https://github.com/xgqfrms/2020-interview-notes/blob/master/test/env.js
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13501449.html
未经授权禁止转载,违者必究!