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

Node.js .env file All In One

Node.js .env file All In One

built-in .env file support
Starting from Node.js v20.6.0, Node.js supports .env files for configuring environment variables.

# process.env ✅
$ node --env-file=config.env index.js

INI file format, each line containing a key-value pair for an environment variable.

// config.env
PORT=8080
USER=eric

you can access the following environment variable using process.env.VARIABLE

// index.js
console.log(`PORT =`, process.env.PORT);
console.log(`USER =`, process.env.USER);

https://nodejs.org/en/blog/release/v20.6.0
https://nodejs.org/zh-cn/blog/release/v20.6.0

https://github.com/nodejs/node/pull/48890

Node.js V20.3.1

import process from 'node:process';
// const process = require('node:process');

import { env } from 'node:process';

env.foo = 'bar';
console.log(env.foo);

$ node -e 'process.env.foo = "bar"' && echo $foo

import { env } from 'node:process';

env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined

https://nodejs.dev/en/api/v20/process/#processenv

image

proccess.env

# process
$ NODE_ENV=production node ./app.js

export

# global scope ENV
$ export NODE_ENV=production
$ node ./app.js

Linux bash shell environment variables

dotenv

# .env file
USER_ID="1234567"
NODE_ENV="development"
require('dotenv').config();

process.env.USER_ID;
// "1234567"
process.env.NODE_ENV;
// "development"

https://www.npmjs.com/package/dotenv

app-node-env

$ npm i -g app-node-env
# OR
$ yarn global add app-node-env

https://www.npmjs.com/package/app-node-env

demos

$ node -v
v20.8.0

error ❌

// index.js
console.log(`PORT =`, env.PORT);
console.log(`USER =`, env.USER);
// index.js
console.log(`PORT =`,  PORT);
console.log(`USER =`,  USER);

image

ok ✅

// index.js
console.log(`PORT =`, process.env.PORT);
console.log(`USER =`, process.env.USER);

image

https://github.com/xgqfrms/Raspberry-Pi/tree/master/Pi-4B/node-playground

https://github.com/xgqfrms/learning/issues/146

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

bin& scripts

package.json

{
  "name": "nodejs_playground",
  "version": "0.0.1",
  "description": "Node.js v20.6.x+",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "$npm_package_bin_npcli",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "npcli": "node --env-file=config.env index.js"
    "npcli_bug": "--env-file=config.env index.js"
  },
  "keywords": [
    "Node.js"
  ],
  "author": "xgqfrms",
  "license": "MIT"
}

  1. npm scripts 设置 $npm_package_bin_SHELL_SCRIPT_NAME ✅
  2. npmm bin 使用 node

https://www.cnblogs.com/xgqfrms/p/17693872.html#5216528

npx

eric@rpi4b:~/Desktop/node-playground $ ane PORT=8888
-bash: ane:未找到命令
eric@rpi4b:~/Desktop/node-playground $ npm-do ane PORT=8888
-bash: npm-do:未找到命令
eric@rpi4b:~/Desktop/node-playground $ npx ane PORT=8888

🚀 your node.js version = 18

🎮 creating...

🎉 finished!
eric@rpi4b:~/Desktop/node-playground $ ./node_modules/app-node-env/ane PORT=8888

🚀 your node.js version = 18

🎮 creating...

🎉 finished!
eric@rpi4b:~/Desktop/node-playground $

npx

http://disq.us/p/2w3phvp

https://2ality.com/2016/01/locally-installed-npm-executables.html

nvm

$ nvm ls

$ nvm ls-remote | grep "LTS"

$ nvm ls-remote  --lts | grep "Latest"

https://nodejs.dev/en/download/package-manager/#nvm

refs

https://nodejs.dev/en/learn/how-to-read-environment-variables-from-nodejs/

https://nodejs.dev/en/learn/nodejs-the-difference-between-development-and-production/



©xgqfrms 2012-2021

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

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


posted @ 2023-09-11 16:33  xgqfrms  阅读(6)  评论(10编辑  收藏  举报