[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-3153":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":17,"stars7d":18,"stars30d":19,"stars90d":16,"forks30d":16,"starsTrendScore":20,"compositeScore":21,"rankGlobal":10,"rankLanguage":10,"license":22,"archived":23,"fork":23,"defaultBranch":24,"hasWiki":23,"hasPages":25,"topics":26,"createdAt":10,"pushedAt":10,"updatedAt":37,"readmeContent":38,"aiSummary":39,"trendingCount":16,"starSnapshotCount":16,"syncStatus":15,"lastSyncTime":40,"discoverSource":41},3153,"dotenv","motdotla\u002Fdotenv","motdotla","Loads environment variables from .env for nodejs projects.","https:\u002F\u002Fwww.dotenv.org",null,"JavaScript",20460,941,98,2,0,3,8,41,9,43.92,"BSD 2-Clause \"Simplified\" License",false,"master",true,[27,5,28,29,30,31,32,33,34,35,36],"configuration-file","env","environment-variables","javascript","node","nodejs","secret-management","secret-manager","secrets","security-tools","2026-06-12 02:00:46","\u003Ca href=\"https:\u002F\u002Fdotenvx.com\u002F?utm_source=github&utm_medium=readme&utm_campaign=motdotla-dotenv&utm_content=banner\">\u003Cimg src=\"https:\u002F\u002Fdotenvx.com\u002Fdotenv-banner.png\" alt=\"dotenvx\" \u002F>\u003C\u002Fa>\n\n# dotenv [![NPM version](https:\u002F\u002Fimg.shields.io\u002Fnpm\u002Fv\u002Fdotenv.svg?style=flat-square)](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fdotenv) [![downloads](https:\u002F\u002Fimg.shields.io\u002Fnpm\u002Fdw\u002Fdotenv)](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fdotenv)\n\n\u003Cimg src=\"https:\u002F\u002Fraw.githubusercontent.com\u002Fmotdotla\u002Fdotenv\u002Fmaster\u002Fdotenv.svg\" alt=\"dotenv\" align=\"right\" width=\"200\" \u002F>\n\nDotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https:\u002F\u002Fnodejs.org\u002Fdocs\u002Flatest\u002Fapi\u002Fprocess.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](https:\u002F\u002F12factor.net\u002Fconfig) methodology.\n\n[Watch the tutorial](https:\u002F\u002Fwww.youtube.com\u002Fwatch?v=YtkZR0NFd1g)\n\n&nbsp;\n\n## Usage\n\nInstall it.\n\n```sh\nnpm install dotenv --save\n```\n\nCreate a `.env` file in the root of your project:\n\n```ini\n# .env\nHELLO=\"Dotenv\"\nOPENAI_API_KEY=\"your-api-key-goes-here\"\n```\n\nAs early as possible in your application, import and configure dotenv:\n\n```javascript\n\u002F\u002F index.js\nrequire('dotenv').config()\n\u002F\u002F or import 'dotenv\u002Fconfig' \u002F\u002F for esm\n\nconsole.log(`Hello ${process.env.HELLO}`)\n```\n```sh\n$ node index.js\n◇ injected env (2) from .env\nHello Dotenv\n```\n\nThat's it. `process.env` now has the keys and values you defined in your `.env` file.\n\n&nbsp;\n\n## Agent Usage\n\nInstall this repo as an agent skill package:\n\n```sh\nnpx skills add motdotla\u002Fdotenv\n```\n```sh\n# ask Claude or Codex to do things like:\nset up dotenv\nupgrade dotenv to dotenvx\n```\n\n&nbsp;\n\n## Advanced\n\n\u003Cdetails>\u003Csummary>ES6\u003C\u002Fsummary>\u003Cbr>\n\nImport with [ES6](#how-do-i-use-dotenv-with-import):\n\n```javascript\nimport 'dotenv\u002Fconfig'\n```\n\nES6 import if you need to set config options:\n\n```javascript\nimport dotenv from 'dotenv'\ndotenv.config({ path: '\u002Fcustom\u002Fpath\u002Fto\u002F.env' })\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>bun\u003C\u002Fsummary>\u003Cbr>\n\n```sh\nbun add dotenv\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>yarn\u003C\u002Fsummary>\u003Cbr>\n\n```sh\nyarn add dotenv\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>pnpm\u003C\u002Fsummary>\u003Cbr>\n\n```sh\npnpm add dotenv\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Monorepos\u003C\u002Fsummary>\u003Cbr>\n\nFor monorepos with a structure like `apps\u002Fbackend\u002Fapp.js`, put it the `.env` file in the root of the folder where your `app.js` process runs.\n\n```ini\n# app\u002Fbackend\u002F.env\nS3_BUCKET=\"YOURS3BUCKET\"\nSECRET_KEY=\"YOURSECRETKEYGOESHERE\"\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Multiline Values\u003C\u002Fsummary>\u003Cbr>\n\nIf you need multiline variables, for example private keys, those are now supported (`>= v15.0.0`) with line breaks:\n\n```ini\nPRIVATE_KEY=\"-----BEGIN RSA PRIVATE KEY-----\n...\nKh9NV...\n...\n-----END RSA PRIVATE KEY-----\"\n```\n\nAlternatively, you can double quote strings and use the `\\n` character:\n\n```ini\nPRIVATE_KEY=\"-----BEGIN RSA PRIVATE KEY-----\\nKh9NV...\\n-----END RSA PRIVATE KEY-----\\n\"\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Comments\u003C\u002Fsummary>\u003Cbr>\n\nComments may be added to your file on their own line or inline:\n\n```ini\n# This is a comment\nSECRET_KEY=YOURSECRETKEYGOESHERE # comment\nSECRET_HASH=\"something-with-a-#-hash\"\n```\n\nComments begin where a `#` exists, so if your value contains a `#` please wrap it in quotes. This is a breaking change from `>= v15.0.0` and on.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Parsing\u003C\u002Fsummary>\u003Cbr>\n\nThe engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.\n\n```javascript\nconst dotenv = require('dotenv')\nconst buf = Buffer.from('BASIC=basic')\nconst config = dotenv.parse(buf) \u002F\u002F will return an object\nconsole.log(typeof config, config) \u002F\u002F object { BASIC : 'basic' }\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Preload\u003C\u002Fsummary>\u003Cbr>\n\n> Note: Consider using [`dotenvx`](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) instead of preloading. I am now doing (and recommending) so.\n>\n> It serves the same purpose (you do not need to require and load dotenv), adds better debugging, and works with ANY language, framework, or platform. – [motdotla](https:\u002F\u002Fnot.la)\n\nYou can use the `--require` (`-r`) [command line option](https:\u002F\u002Fnodejs.org\u002Fapi\u002Fcli.html#-r---require-module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.\n\n```bash\n$ node -r dotenv\u002Fconfig your_script.js\n```\n\nThe configuration options below are supported as command line arguments in the format `dotenv_config_\u003Coption>=value`\n\n```bash\n$ node -r dotenv\u002Fconfig your_script.js dotenv_config_path=\u002Fcustom\u002Fpath\u002Fto\u002F.env dotenv_config_debug=true\n```\n\nAdditionally, you can use environment variables to set configuration options. Command line arguments will precede these.\n\n```bash\n$ DOTENV_CONFIG_\u003COPTION>=value node -r dotenv\u002Fconfig your_script.js\n```\n\n```bash\n$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv\u002Fconfig your_script.js dotenv_config_path=\u002Fcustom\u002Fpath\u002Fto\u002F.env\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Variable Expansion\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) for variable expansion.\n\nReference and expand variables already on your machine for use in your .env file.\n\n```ini\n# .env\nUSERNAME=\"username\"\nDATABASE_URL=\"postgres:\u002F\u002F${USERNAME}@localhost\u002Fmy_database\"\n```\n```js\n\u002F\u002F index.js\nconsole.log('DATABASE_URL', process.env.DATABASE_URL)\n```\n```sh\n$ dotenvx run --debug -- node index.js\n⟐ injected env (2) from .env · dotenvx@1.59.1\nDATABASE_URL postgres:\u002F\u002Fusername@localhost\u002Fmy_database\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Command Substitution\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) for command substitution.\n\nAdd the output of a command to one of your variables in your .env file.\n\n```ini\n# .env\nDATABASE_URL=\"postgres:\u002F\u002F$(whoami)@localhost\u002Fmy_database\"\n```\n```js\n\u002F\u002F index.js\nconsole.log('DATABASE_URL', process.env.DATABASE_URL)\n```\n```sh\n$ dotenvx run --debug -- node index.js\n⟐ injected env (1) from .env · dotenvx@1.59.1\nDATABASE_URL postgres:\u002F\u002Fyourusername@localhost\u002Fmy_database\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Encryption\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) for encryption.\n\nAdd encryption to your `.env` files with a single command.\n\n```\n$ dotenvx set HELLO Production -f .env.production\n$ echo \"console.log('Hello ' + process.env.HELLO)\" > index.js\n\n$ DOTENV_PRIVATE_KEY_PRODUCTION=\"\u003C.env.production private key>\" dotenvx run -- node index.js\n⟐ injected env (2) from .env.production · dotenvx@1.59.1\nHello Production\n```\n\n[learn more](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx?tab=readme-ov-file#encryption)\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Multiple Environments\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) to manage multiple environments.\n\nRun any environment locally. Create a `.env.ENVIRONMENT` file and use `-f` to load it. It's straightforward, yet flexible.\n\n```bash\n$ echo \"HELLO=production\" > .env.production\n$ echo \"console.log('Hello ' + process.env.HELLO)\" > index.js\n\n$ dotenvx run -f=.env.production -- node index.js\nHello production\n> ^^\n```\n\nor with multiple .env files\n\n```bash\n$ echo \"HELLO=local\" > .env.local\n$ echo \"HELLO=World\" > .env\n$ echo \"console.log('Hello ' + process.env.HELLO)\" > index.js\n\n$ dotenvx run -f=.env.local -f=.env -- node index.js\nHello local\n```\n\n[more environment examples](https:\u002F\u002Fdotenvx.com\u002Fdocs\u002Fquickstart\u002Fenvironments?utm_source=github&utm_medium=readme&utm_campaign=motdotla-dotenv&utm_content=docs-environments)\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Production\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) for production deploys.\n\nCreate a `.env.production` file.\n\n```sh\n$ echo \"HELLO=production\" > .env.production\n```\n\nEncrypt it.\n\n```sh\n$ dotenvx encrypt -f .env.production\n```\n\nSet `DOTENV_PRIVATE_KEY_PRODUCTION` (found in `.env.keys`) on your server.\n\n```\n$ heroku config:set DOTENV_PRIVATE_KEY_PRODUCTION=value\n```\n\nCommit your `.env.production` file to code and deploy.\n\n```\n$ git add .env.production\n$ git commit -m \"encrypted .env.production\"\n$ git push heroku main\n```\n\nDotenvx will decrypt and inject the secrets at runtime using `dotenvx run -- node index.js`.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Syncing\u003C\u002Fsummary>\u003Cbr>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) to sync your .env files.\n\nEncrypt them with `dotenvx encrypt -f .env` and safely include them in source control. Your secrets are securely synced with your git.\n\nThis still subscribes to the twelve-factor app rules by generating a decryption key separate from code.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>More Examples\u003C\u002Fsummary>\u003Cbr>\n\nSee [examples](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples) of using dotenv with various frameworks, languages, and configurations.\n\n* [nodejs](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-nodejs)\n* [nodejs (debug on)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-nodejs-debug)\n* [nodejs (override on)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-nodejs-override)\n* [nodejs (processEnv override)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-custom-target)\n* [esm](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-esm)\n* [esm (preload)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-esm-preload)\n* [typescript](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-typescript)\n* [typescript parse](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-typescript-parse)\n* [typescript config](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-typescript-config)\n* [webpack](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-webpack)\n* [webpack (plugin)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-webpack2)\n* [react](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-react)\n* [react (typescript)](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-react-typescript)\n* [express](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-express)\n* [nestjs](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-nestjs)\n* [fastify](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-fastify)\n\n\u003C\u002Fdetails>\n\n&nbsp;\n\n## FAQ\n\n\u003Cdetails>\u003Csummary>Should I commit my `.env` file?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nNo.\n\nUnless you encrypt it with [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx). Then we recommend you do.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>What about variable expansion?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx).\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Should I have multiple `.env` files?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nWe recommend creating one `.env` file per environment. Use `.env` for local\u002Fdevelopment, `.env.production` for production and so on. This still follows the twelve factor principles as each is attributed individually to its own environment. Avoid custom set ups that work in inheritance somehow (`.env.production` inherits values from `.env` for example). It is better to duplicate values if necessary across each `.env.environment` file.\n\n> In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.\n>\n> – [The Twelve-Factor App](http:\u002F\u002F12factor.net\u002Fconfig)\n\nAdditionally, we recommend using [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) to encrypt and manage these.\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>How do I use dotenv with `import`?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nSimply..\n\n```javascript\n\u002F\u002F index.mjs (ESM)\nimport 'dotenv\u002Fconfig' \u002F\u002F see https:\u002F\u002Fgithub.com\u002Fmotdotla\u002Fdotenv#how-do-i-use-dotenv-with-import\nimport express from 'express'\n```\n\nA little background..\n\n> When you run a module containing an `import` declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.\n>\n> – [ES6 In Depth: Modules](https:\u002F\u002Fhacks.mozilla.org\u002F2015\u002F08\u002Fes6-in-depth-modules\u002F)\n\nWhat does this mean in plain language? It means you would think the following would work but it won't.\n\n`errorReporter.mjs`:\n```js\nclass Client {\n  constructor (apiKey) {\n    console.log('apiKey', apiKey)\n\n    this.apiKey = apiKey\n  }\n}\n\nexport default new Client(process.env.API_KEY)\n```\n`index.mjs`:\n```js\n\u002F\u002F Note: this is INCORRECT and will not work\nimport * as dotenv from 'dotenv'\ndotenv.config()\n\nimport errorReporter from '.\u002FerrorReporter.mjs' \u002F\u002F process.env.API_KEY will be blank!\n```\n\n`process.env.API_KEY` will be blank.\n\nInstead, `index.mjs` should be written as..\n\n```js\nimport 'dotenv\u002Fconfig'\n\nimport errorReporter from '.\u002FerrorReporter.mjs'\n```\n\nDoes that make sense? It's a bit unintuitive, but it is how importing of ES6 modules work. Here is a [working example of this pitfall](https:\u002F\u002Fgithub.com\u002Fdotenv-org\u002Fexamples\u002Ftree\u002Fmaster\u002Fusage\u002Fdotenv-es6-import-pitfall).\n\nThere are two alternatives to this approach:\n\n1. Preload with dotenvx: `dotenvx run -- node index.js` (_Note: you do not need to `import` dotenv with this approach_)\n2. Create a separate file that will execute `config` first as outlined in [this comment on #133](https:\u002F\u002Fgithub.com\u002Fmotdotla\u002Fdotenv\u002Fissues\u002F133#issuecomment-255298822)\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>Can I customize\u002Fwrite plugins for dotenv?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nYes! `dotenv.config()` returns an object representing the parsed `.env` file. This gives you everything you need to continue setting values on `process.env`. For example:\n\n```js\nconst dotenv = require('dotenv')\nconst variableExpansion = require('dotenv-expand')\nconst myEnv = dotenv.config()\nvariableExpansion(myEnv)\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>What rules does the parsing engine follow?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nThe parsing engine currently supports the following rules:\n\n- `BASIC=basic` becomes `{BASIC: 'basic'}`\n- empty lines are skipped\n- lines beginning with `#` are treated as comments\n- `#` marks the beginning of a comment (unless when the value is wrapped in quotes)\n- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)\n- inner quotes are maintained (think JSON) (`JSON={\"foo\": \"bar\"}` becomes `{JSON:\"{\\\"foo\\\": \\\"bar\\\"}\"`)\n- whitespace is removed from both ends of unquoted values (see more on [`trim`](https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FJavaScript\u002FReference\u002FGlobal_Objects\u002FString\u002FTrim)) (`FOO=  some value  ` becomes `{FOO: 'some value'}`)\n- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: \"quoted\"}`)\n- single and double quoted values maintain whitespace from both ends (`FOO=\"  some value  \"` becomes `{FOO: '  some value  '}`)\n- double quoted values expand new lines (`MULTILINE=\"new\\nline\"` becomes\n\n```\n{MULTILINE: 'new\nline'}\n```\n\n- backticks are supported (`` BACKTICK_KEY=`This has 'single' and \"double\" quotes inside of it.` ``)\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>What about syncing and securing .env files?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nUse [dotenvx](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx) to unlock syncing encrypted .env files over git.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>How do I specify config options with ES6 import?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nWhen using `import 'dotenv\u002Fconfig'`, you can't pass options directly. Here are a few ways to handle it.\n\n**Option 1: Import and call `config()` yourself (Recommended)**\n\n```javascript\n\u002F\u002F index.mjs\nimport dotenv from 'dotenv'\n\ndotenv.config({\n  path: '\u002Fcustom\u002Fpath\u002Fto\u002F.env',\n  debug: true\n})\n\n\u002F\u002F Now import everything else\nimport express from 'express'\n```\n\nBecause ES6 imports are hoisted, put the `dotenv` import and `config()` call at the very top, before any other imports that rely on `process.env`.\n\n**Option 2: Use environment variables**\n\n```bash\nDOTENV_CONFIG_DEBUG=true DOTENV_CONFIG_PATH=\u002Fcustom\u002Fpath\u002Fto\u002F.env node index.mjs\n```\n\nThen in your code you can keep the shorthand:\n\n```javascript\nimport 'dotenv\u002Fconfig'\n```\n\n**Option 3: A tiny wrapper file**\n\nCreate `load-env.mjs`:\n\n```javascript\nimport dotenv from 'dotenv'\ndotenv.config({ path: '\u002Fcustom\u002Fpath\u002Fto\u002F.env', debug: true })\n```\n\nThen in your main file:\n\n```javascript\nimport '.\u002Fload-env.mjs'\nimport express from 'express'\n```\n\nNot the most elegant, but it works reliably when hoisting gets in the way.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>What if I accidentally commit my `.env` file to code?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nRemove it, [remove git history](https:\u002F\u002Fdocs.github.com\u002Fen\u002Fauthentication\u002Fkeeping-your-account-and-data-secure\u002Fremoving-sensitive-data-from-a-repository) and then install the [git pre-commit hook](https:\u002F\u002Fgithub.com\u002Fdotenvx\u002Fdotenvx#pre-commit) to prevent this from ever happening again. \n\n```\nnpm i -g @dotenvx\u002Fdotenvx\ndotenvx precommit --install\n```\n\n\u003C\u002Fdetails>\n\n\u003Cdetails>\u003Csummary>What happens to environment variables that were already set?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nBy default, we will never modify any environment variables that have already been set. In particular, if there is a variable in your `.env` file which collides with one that already exists in your environment, then that variable will be skipped.\n\nIf instead, you want to override `process.env` use the `override` option.\n\n```javascript\nrequire('dotenv').config({ override: true })\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>How can I prevent committing my `.env` file to a Docker build?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nUse the [docker prebuild hook](https:\u002F\u002Fdotenvx.com\u002Fdocs\u002Ffeatures\u002Fprebuild?utm_source=github&utm_medium=readme&utm_campaign=motdotla-dotenv&utm_content=docs-prebuild).\n\n```bash\n# Dockerfile\n...\nRUN curl -fsS https:\u002F\u002Fdotenvx.sh\u002F | sh\n...\nRUN dotenvx prebuild\nCMD [\"dotenvx\", \"run\", \"--\", \"node\", \"index.js\"]\n```\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>How come my environment variables are not showing up for React?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nYour React code is run in Webpack, where the `fs` module or even the `process` global itself are not accessible out-of-the-box. `process.env` can only be injected through Webpack configuration.\n\nIf you are using [`react-scripts`](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Freact-scripts), which is distributed through [`create-react-app`](https:\u002F\u002Fcreate-react-app.dev\u002F), it has dotenv built in but with a quirk. Preface your environment variables with `REACT_APP_`. See [this stack overflow](https:\u002F\u002Fstackoverflow.com\u002Fquestions\u002F42182577\u002Fis-it-possible-to-use-dotenv-in-a-react-project) for more details.\n\nIf you are using other frameworks (e.g. Next.js, Gatsby...), you need to consult their documentation for how to inject environment variables into the client.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Why is the `.env` file not loading my environment variables successfully?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nMost likely your `.env` file is not in the correct place. [See this stack overflow](https:\u002F\u002Fstackoverflow.com\u002Fquestions\u002F42335016\u002Fdotenv-file-is-not-loading-environment-variables).\n\nTurn on debug mode and try again..\n\n```js\nrequire('dotenv').config({ debug: true })\n```\n\nYou will receive a helpful error outputted to your console.\n\n\u003C\u002Fdetails>\n\u003Cdetails>\u003Csummary>Why am I getting the error `Module not found: Error: Can't resolve 'crypto|os|path'`?\u003C\u002Fsummary>\u003Cbr\u002F>\n\nYou are using dotenv on the front-end and have not included a polyfill. Webpack \u003C 5 used to include these for you. Do the following:\n\n```bash\nnpm install node-polyfill-webpack-plugin\n```\n\nConfigure your `webpack.config.js` to something like the following.\n\n```js\nrequire('dotenv').config()\n\nconst path = require('path');\nconst webpack = require('webpack')\n\nconst NodePolyfillPlugin = require('node-polyfill-webpack-plugin')\n\nmodule.exports = {\n  mode: 'development',\n  entry: '.\u002Fsrc\u002Findex.ts',\n  output: {\n    filename: 'bundle.js',\n    path: path.resolve(__dirname, 'dist'),\n  },\n  plugins: [\n    new NodePolyfillPlugin(),\n    new webpack.DefinePlugin({\n      'process.env': {\n        HELLO: JSON.stringify(process.env.HELLO)\n      }\n    }),\n  ]\n};\n```\n\nAlternatively, just use [dotenv-webpack](https:\u002F\u002Fgithub.com\u002Fmrsteele\u002Fdotenv-webpack) which does this and more behind the scenes for you.\n\n\u003C\u002Fdetails>\n\n&nbsp;\n\n## Docs\n\nDotenv exposes four functions:\n\n* `config`\n* `parse`\n* `populate`\n\n### Config\n\n`config` will read your `.env` file, parse the contents, assign it to\n[`process.env`](https:\u002F\u002Fnodejs.org\u002Fdocs\u002Flatest\u002Fapi\u002Fprocess.html#process_process_env),\nand return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.\n\n```js\nconst result = dotenv.config()\n\nif (result.error) {\n  throw result.error\n}\n\nconsole.log(result.parsed)\n```\n\nYou can additionally, pass options to `config`.\n\n#### Options\n\n##### path\n\nDefault: `path.resolve(process.cwd(), '.env')`\n\nSpecify a custom path if your file containing environment variables is located elsewhere.\n\n```js\nrequire('dotenv').config({ path: '\u002Fcustom\u002Fpath\u002Fto\u002F.env' })\n```\n\nBy default, `config` will look for a file called .env in the current working directory.\n\nPass in multiple files as an array, and they will be parsed in order and combined with `process.env` (or `option.processEnv`, if set). The first value set for a variable will win, unless the `options.override` flag is set, in which case the last value set will win.  If a value already exists in `process.env` and the `options.override` flag is NOT set, no changes will be made to that value. \n\n```js  \nrequire('dotenv').config({ path: ['.env.local', '.env'] })\n```\n\n##### quiet\n\nDefault: `false`\n\nSuppress runtime logging message.\n\n```js\n\u002F\u002F index.js\nrequire('dotenv').config({ quiet: false }) \u002F\u002F change to true to suppress\nconsole.log(`Hello ${process.env.HELLO}`)\n```\n\n```ini\n# .env\nHELLO=World\n```\n\n```sh\n$ node index.js\nHello World\n```\n\n##### encoding\n\nDefault: `utf8`\n\nSpecify the encoding of your file containing environment variables.\n\n```js\nrequire('dotenv').config({ encoding: 'latin1' })\n```\n\n##### debug\n\nDefault: `false`\n\nTurn on logging to help debug why certain keys or values are not being set as you expect.\n\n```js\nrequire('dotenv').config({ debug: process.env.DEBUG })\n```\n\n##### override\n\nDefault: `false`\n\nOverride any environment variables that have already been set on your machine with values from your .env file(s). If multiple files have been provided in `option.path` the override will also be used as each file is combined with the next. Without `override` being set, the first value wins. With `override` set the last value wins. \n\n```js\nrequire('dotenv').config({ override: true })\n```\n\n##### processEnv\n\nDefault: `process.env`\n\nSpecify an object to write your environment variables to. Defaults to `process.env` environment variables.\n\n```js\nconst myObject = {}\nrequire('dotenv').config({ processEnv: myObject })\n\nconsole.log(myObject) \u002F\u002F values from .env\nconsole.log(process.env) \u002F\u002F this was not changed or written to\n```\n\n### Parse\n\nThe engine which parses the contents of your file containing environment\nvariables is available to use. It accepts a String or Buffer and will return\nan Object with the parsed keys and values.\n\n```js\nconst dotenv = require('dotenv')\nconst buf = Buffer.from('BASIC=basic')\nconst config = dotenv.parse(buf) \u002F\u002F will return an object\nconsole.log(typeof config, config) \u002F\u002F object { BASIC : 'basic' }\n```\n\n#### Options\n\n##### debug\n\nDefault: `false`\n\nTurn on logging to help debug why certain keys or values are not being set as you expect.\n\n```js\nconst dotenv = require('dotenv')\nconst buf = Buffer.from('hello world')\nconst opt = { debug: true }\nconst config = dotenv.parse(buf, opt)\n\u002F\u002F expect a debug message because the buffer is not in KEY=VAL form\n```\n\n### Populate\n\nThe engine which populates the contents of your .env file to `process.env` is available for use. It accepts a target, a source, and options. This is useful for power users who want to supply their own objects.\n\nFor example, customizing the source:\n\n```js\nconst dotenv = require('dotenv')\nconst parsed = { HELLO: 'world' }\n\ndotenv.populate(process.env, parsed)\n\nconsole.log(process.env.HELLO) \u002F\u002F world\n```\n\nFor example, customizing the source AND target:\n\n```js\nconst dotenv = require('dotenv')\nconst parsed = { HELLO: 'universe' }\nconst target = { HELLO: 'world' } \u002F\u002F empty object\n\ndotenv.populate(target, parsed, { override: true, debug: true })\n\nconsole.log(target) \u002F\u002F { HELLO: 'universe' }\n```\n\n#### options\n\n##### Debug\n\nDefault: `false`\n\nTurn on logging to help debug why certain keys or values are not being populated as you expect.\n\n##### override\n\nDefault: `false`\n\nOverride any environment variables that have already been set.\n\n&nbsp;\n\n## CHANGELOG\n\nSee [CHANGELOG.md](CHANGELOG.md)\n\n&nbsp;\n\n## Who's using dotenv?\n\n[These npm modules depend on it.](https:\u002F\u002Fwww.npmjs.com\u002Fbrowse\u002Fdepended\u002Fdotenv)\n\nProjects that expand it often use the [keyword \"dotenv\" on npm](https:\u002F\u002Fwww.npmjs.com\u002Fsearch?q=keywords:dotenv).\n","dotenv 是一个用于 Node.js 项目的环境变量加载工具，它能够从 `.env` 文件中读取配置并将其添加到 `process.env` 对象中。该模块不依赖任何外部库，遵循 The Twelve-Factor App 方法论，将应用配置与代码分离以提高安全性及灵活性。支持多种安装方式如 npm、yarn 或 pnpm，并且可以处理多行值和注释，适用于需要通过环境变量管理敏感信息或配置参数的各种开发场景，比如API密钥、数据库连接字符串等的存储与使用。","2026-06-11 02:52:42","top_language"]