[Node] Using dotenv to config env variables

本文介绍了一种利用Node.js中的dotenv包来管理应用程序环境变量的方法。通过将敏感信息存储在一个独立的.env文件中并使用dotenv加载这些配置,可以有效地保护如数据库连接、邮件设置等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Install:

npm install dotenv --save

 

For example, we can store the sensitive information or env related information in a 'veriables.env' file:

// variables.env

NODE_ENV=development
DATABASE=mongodb://xxxxx
MAIL_USER=xx
MAIL_PASS=xx
MAIL_HOST=xxx.io
MAIL_PORT=2525
PORT=7777
MAP_KEY=xxxx
SECRET=xxx
KEY=xxx

 

Then we can use dotenv to load those configurations:

// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });

 

Now all the env related information will be stored in:

process.env

 

选择的环境为 h5 H5-local 👽 Taro v3.6.2 stderr: ● Webpack █████████████████████████ setup (9%) compilation TaroH5Plugin ● Webpack █████████████████████████ building (23%) 1/4 entries 1/4 dependencie ● Webpack █████████████████████████ sealing (92%) asset processing ✖ Webpack ✖ Webpack Compiled with some errors in 13.55s <i> [webpack-dev-server] Project is running at: <i> [webpack-dev-server] On Your Network (IPv4): http://192.168.2.144:8080/ <i> [webpack-dev-server] Content not from webpack is served from 'E:\hy\mall\mini-program\dist\h5' directory <i> [webpack-dev-server] 404s will fallback to '/index.html' <i> [webpack-dev-middleware] wait until bundle finished: /mobile/ (node:25932) [DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH] DeprecationWarning: [hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details) (Use `node --trace-deprecation ...` to show where the warning was created) ✖ Errors: Error: plugin x failed to invoke plugin on 'Some("E:\\hy\\mall\\mini-program\\src\\app.config.ts")' → Watching... [2025/7/1 18:11:45] ℹ Listening at http://192.168.2.144:8080/mobile/ asset js/app.3753405147285bf9cda9.js 977 KiB [emitted] [immutable] (name: app) asset js/taro.3753405147285bf9cda9.js 179 KiB [emitted] [immutable] (name: taro) (id hint: taro) asset js/runtime.3753405147285bf9cda9.js 45.4 KiB [emitted] [immutable] (name: runtime) asset index.html 8.28 KiB [emitted] Entrypoint app 1.17 MiB = js/runtime.3753405147285bf9cda9.js 45.4 KiB js/taro.3753405147285bf9cda9.js 179 KiB js/app.3753405147285bf9cda9.js 977 KiB runtime modules 30 KiB 14 modules modules by path ./node_modules/core-js-pure/ 132 KiB 127 modules modules by path ./node_modules/@pmmmwh/react-refresh-webpack-plugin/ 43.9 KiB 22 modules modules by path ./node_modules/@tarojs/webpack5-runner/node_modules/webpack-dev-server/client/ 61.6 KiB 12 modules modules by path ./node_modules/html-entities/dist/ 67.8 KiB modules by path ./node_modules/html-entities/dist/esm/*.js 33.5 KiB 4 modules modules by path ./node_modules/html-entities/dist/commonjs/*.js 34.2 KiB 4 modules modules by path ./node_modules/webpack/hot/*.js 5.82 KiB ./node_modules/webpack/hot/only-dev-server.js 2.58 KiB [built] [code generated] ./node_modules/webpack/hot/log.js 1.74 KiB [built] [code generated] + 2 modules modules by path ./node_modules/react-refresh/ 21.8 KiB ./node_modules/react-refresh/runtime.js 222 bytes [built] [code generated] ./node_modules/react-refresh/cjs/react-refresh-runtime.development.js 21.6 KiB [built] [code generated] + 7 modules ERROR in ./src/app.config.ts plugin x failed to invoke plugin on 'Some("E:\\hy\\mall\\mini-program\\src\\app.config.ts")' ERROR in ./src/app.config.ts Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length') at WasmHash._updateWithBuffer (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\wasm-hash.js:108:23) at WasmHash.update (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\wasm-hash.js:51:8) at BatchedHash.update (E:\hy\mall\mini-program\node_modules\webpack\lib\util\hash\BatchedHash.js:50:14) at NormalModule.updateHash (E:\hy\mall\mini-program\node_modules\webpack\lib\NormalModule.js:1365:8) at Compilation._createModuleHash (E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3970:11) at Compilation.createModuleHashes (E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3927:25) at E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:3027:11 at Hook.eval [as callAsync] (eval at create (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:10:1) at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\Hook.js:20:14) at E:\hy\mall\mini-program\node_modules\webpack\lib\Compilation.js:2983:36 at eval (eval at create (E:\hy\mall\mini-program\node_modules\webpack\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:18:1) at E:\hy\mall\mini-program\node_modules\@tarojs\webpack5-runner\node_modules\html-webpack-plugin\lib\cached-child-compiler.js:237:53 at processTicksAndRejections (node:internal/process/task_queues:95:5) webpack 5.90.1 compiled with 2 errors in 13559 ms 升级tarojscli出现的问题
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值