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

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

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

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


posted @   xgqfrms  阅读(8)  评论(10编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-09-11 Rust 字符串插值 All In One
2022-09-11 wasmer All In One
2021-09-11 项目复盘 All In One
2020-09-11 free pdf ebooks All In One
2020-09-11 前端页面性能监控和数据上报
2020-09-11 OAuth 2.0 All In One
2020-09-11 pure CSS waterfall layout
点击右上角即可分享
微信分享提示