xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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==

临时全局环境变量

  1. Linux / macOS
# 设置 NODE_ENV 环境变量
$ export NODE_ENV=production

# 清除 NODE_ENV 环境变量, 覆盖为空
$ export NODE_ENV=
# ✅
$ unset NODE_ENV

  1. Windows
# 设置 NODE_ENV 环境变量
$ set NODE_ENV=production

# 清除 NODE_ENV 环境变量
$ set NODE_ENV=

永久全局环境变量(⚠️ 不推荐,不够灵活)

修改系统的配置文件 $PATH

  1. vim
  2. VS Code
  3. 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://stackoverflow.com/questions/77058657/why-both-the-set-and-unset-commands-dont-work-as-expected-in-my-macos-terminal#comment135847407_77058657

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



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(245)  评论(6编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-08-14 React Native & CodePush & App Center
2019-08-14 Chrome & install App & PWA
2019-08-14 js trailing comma & JSON All In One
2019-08-14 webIM & IM
2019-08-14 Flatten Array & Array.flat() & Array.flatMap() All In One
2016-08-14 Building Your First App && Meet Android Studio
点击右上角即可分享
微信分享提示