{"id":260482,"date":"2023-01-30T09:44:01","date_gmt":"2023-01-30T01:44:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=260482"},"modified":"2022-12-30T15:45:11","modified_gmt":"2022-12-30T07:45:11","slug":"env-linux-dotenv","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/env-linux-dotenv.html","title":{"rendered":"\u9879\u76ee\u4e2d\u5e38\u7528\u7684 .env \u6587\u4ef6\u539f\u7406\u6e90\u7801\u5206\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86\u9879\u76ee\u4e2d\u5e38\u7528\u7684 .env \u6587\u4ef6\u539f\u7406\u6e90\u7801\u89e3\u6790\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

dotenv \u662f\u4e00\u4e2a\u7528\u4e8e\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u7684\u5e93\uff0c\u5728 Node.js \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7b80\u5316\u5bf9\u73af\u5883\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u8d77\u5230\u4e86\u5f88\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n

\u5982\u4f55\u4f7f\u7528<\/strong><\/div>\n

\u4f7f\u7528 dotenv \u5e93\uff0c\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a .env \u7684\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u5b58\u50a8\u73af\u5883\u53d8\u91cf\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 dotenv \u5e93\u5c06\u8fd9\u4e9b\u53d8\u91cf\u52a0\u8f7d\u5230 Node.js \u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n

\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728 .env \u6587\u4ef6\u4e2d\u5b58\u50a8\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n

DB_HOST=localhost\r\nDB_USERNAME=user\r\nDB_PASSWORD=password<\/pre>\n

\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u8fd9\u4e9b\u53d8\u91cf\u52a0\u8f7d\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff1a<\/p>\n

require('dotenv').config();\r\nconst dbHost = process.env.DB_HOST;\r\nconst dbUsername = process.env.DB_USERNAME;\r\nconst dbPassword = process.env.DB_PASSWORD;<\/pre>\n
\u6e90\u7801\u89e3\u6790<\/strong><\/div>\n

\u9605\u8bfb\u6e90\u7801\u4e4b\u524d\uff0c\u53ef\u4ee5\u731c\u6d4b dotenv \u6240\u505a\u7684\u5de5\u4f5c\u6709\u5982\u4e0b\u51e0\u70b9\u3002<\/p>\n

  • \u8bfb\u53d6 .env \u6587\u4ef6<\/li>\n
  • \u89e3\u6790\u6587\u4ef6<\/li>\n
  • \u5c06\u89e3\u6790\u51fa\u7684\u53d8\u91cf\u8d4b\u503c\u7ed9 process.env<\/li>\n

    \u6765\u770b\u4e0b\u6e90\u7801\u662f\u5982\u4f55\u5b8c\u6210\u4e0a\u8ff0\u529f\u80fd\u7684\u3002<\/p>\n

    \u8bfb\u53d6\u6587\u4ef6<\/strong><\/span><\/div>\n
    function config (options) {\r\n  let dotenvPath = path.resolve(process.cwd(), '.env')\r\n  let encoding = 'utf8'\r\n  const debug = Boolean(options && options.debug)\r\n  const override = Boolean(options && options.override)\r\n  if (options) {\r\n    if (options.path != null) {\r\n      dotenvPath = _resolveHome(options.path)\r\n    }\r\n    if (options.encoding != null) {\r\n      encoding = options.encoding\r\n    }\r\n  }\r\n}<\/pre>\n

    \u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf dotenvPath\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u4f7f\u7528 path.resolve \u51fd\u6570\u5904\u7406\u540e\u7684\u8def\u5f84\u3002<\/p>\n

    path.resolve \u51fd\u6570\u4f1a\u4ece\u53f3\u5230\u5de6\u4f9d\u6b21\u904d\u5386\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\u3002\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f process.cwd\uff0c\u5b83\u8fd4\u56de Node.js \u8fdb\u7a0b\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5b57\u7b26\u4e32 '.env'\uff0c\u5b83\u8868\u793a\u8981\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u67e5\u627e\u7684\u6587\u4ef6\u540d\u3002<\/p>\n

    \u4e4b\u540e\u4f1a\u8fdb\u884c\u4e00\u4e9b\u53c2\u6570\u7684\u5224\u65ad\uff0c\u5982\u679c\u53c2\u6570\u4e2d\u6709path\u8fd9\u4e2a\u53d8\u91cf\uff0c\u5219\u4f7f\u7528_resolveHome\u51fd\u6570\u5904\u7406\uff1a<\/p>\n

    function _resolveHome (envPath) {\r\n  return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath\r\n}<\/pre>\n

    os.homedir \u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n

    _resolveHome \u51fd\u6570\u53ef\u7528\u4e8e\u5c06\u4ee5\u6ce2\u6d6a\u53f7\u5f00\u5934\u7684\u8def\u5f84\u89e3\u6790\u4e3a\u4e3b\u76ee\u5f55\u7684\u5b9e\u9645\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5982\u679c envPath \u7b49\u4e8e '~\/documents\/file.txt'\uff0c\u5219\u51fd\u6570\u5c06\u8fd4\u56de '\/home\/user\/documents\/file.txt'\uff08\u5728\u57fa\u4e8e Unix \u7684\u7cfb\u7edf\u4e0a\uff09\u6216 'C:\\Users\\user\\documents\\file.txt'\uff08\u5728 Windows \u4e0a\uff09\u3002<\/p>\n

    \u89e3\u6790\u6587\u4ef6<\/strong><\/span><\/div>\n
    \/\/ \u4f7f\u7528 `fs.readFileSync` \u51fd\u6570\u4ee5\u6307\u5b9a\u7684\u7f16\u7801\u65b9\u5f0f\u4ece\u6587\u4ef6\u7cfb\u7edf\u4e2d\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\r\nconst parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }))\r\n\/\/ \u89e3\u6790\u6587\u4ef6\r\nfunction parse (src) {\r\n  const obj = {}\r\n  \/\/ \u8f6c\u4e3astring\u7c7b\u578b\r\n  let lines = src.toString()\r\n  \/\/ \u5c06\u6362\u884c\u7b26\u8f6c\u6362\u4e3a\u76f8\u540c\u7684\u683c\u5f0f\r\n  lines = lines.replace(\/\\r\\n?\/mg, '\\n')\r\n  let match\r\n  while ((match = LINE.exec(lines)) != null) {\r\n    const key = match[1]\r\n    \/\/ Default undefined or null to empty string\r\n    let value = (match[2] || '')\r\n    \/\/ Remove whitespace\r\n    value = value.trim()\r\n    \/\/ Check if double quoted\r\n    const maybeQuote = value[0]\r\n    \/\/ Remove surrounding quotes\r\n    value = value.replace(\/^(['\"`])([\\s\\S]*)\\1$\/mg, '$2')\r\n    \/\/ Expand newlines if double quoted\r\n    if (maybeQuote === '\"') {\r\n      value = value.replace(\/\\\\n\/g, '\\n')\r\n      value = value.replace(\/\\\\r\/g, '\\r')\r\n    }\r\n    \/\/ Add to object\r\n    obj[key] = value\r\n  }\r\n  return obj\r\n}<\/pre>\n

    \u9996\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f LINE \u6765\u5339\u914d\u5b57\u7b26\u4e32 lines \u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n

    const LINE = \/(?:^|^)\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^'])*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\\r\\n]+)?\\s*(?:#.*)?(?:$|$)\/mg<\/pre>\n

    \u8fd9\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u76ee\u7684\u662f\u5339\u914d\u7c7b\u4f3c\u4e8e\u73af\u5883\u53d8\u91cf\u7684\u884c\u3002\u5b83\u53ef\u4ee5\u5339\u914d\u4ee5\u4e0b\u683c\u5f0f\u7684\u884c\uff1a<\/p>\n

    VARNAME=value\r\nVARNAME: value\r\nexport VARNAME=value\r\nexport VARNAME: value<\/pre>\n

    \u6700\u540e\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u53d8\u91cf\u7684\u5bf9\u8c61\u3002<\/p>\n

    \u8d4b\u503c\u64cd\u4f5c<\/strong><\/span><\/div>\n
    try {\r\n  \/\/ Specifying an encoding returns a string instead of a buffer\r\n  const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }))\r\n  Object.keys(parsed).forEach(function (key) {\r\n    if (!Object.prototype.hasOwnProperty.call(process.env, key)) {\r\n      process.env[key] = parsed[key]\r\n    } else {\r\n      if (override === true) {\r\n        process.env[key] = parsed[key]\r\n      }\r\n      if (debug) {\r\n        if (override === true) {\r\n          _log(`\"${key}\" is already defined in \\`process.env\\` and WAS overwritten`)\r\n        } else {\r\n          _log(`\"${key}\" is already defined in \\`process.env\\` and was NOT overwritten`)\r\n        }\r\n      }\r\n    }\r\n  })\r\n  return { parsed }\r\n} <\/pre>\n

    \u62ff\u5230\u89e3\u6790\u540e\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528 Object.keys(parsed) \u83b7\u53d6\u6240\u6709\u7684\u952e\uff0c\u7136\u540e\u4f7f\u7528forEach\u5faa\u73af\u5c06\u6240\u6709\u7684\u952e\u6dfb\u52a0\u5230process.env \u4e2d\u3002<\/p>\n

    dotenv \u7684\u529f\u80fd\u7528\u4e00\u53e5\u8bdd\u6765\u6982\u62ec\u5c31\u662f\uff1a\u89e3\u6790env\u6587\u4ef6\u5c06\u5176\u53d8\u91cf\u6dfb\u52a0\u5230process.env\u4e2d\uff0c\u5176\u4e2d\u89e3\u6790\u90e8\u5206\u4e3b\u8981\u662f\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d4\u79cd\u683c\u5f0f\u7684\u952e\u503c\u5bf9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    dotenv \u662f\u4e00\u4e2a\u7528\u4e8e\u52a0\u8f7d\u73af\u5883\u53d8\u91cf\u7684\u5e93\uff0c\u5728 Node.js \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7b80\u5316\u5bf9\u73af\u5883\u53d8\u91cf\u7684\u8bbf\u95ee\u3002\u5728\u65e5 […]<\/p>\n","protected":false},"author":1482,"featured_media":85409,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260482","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=260482"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260482\/revisions"}],"predecessor-version":[{"id":260485,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/260482\/revisions\/260485"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85409"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=260482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=260482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=260482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}