版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <div align="center">
  2. <p>
  3. <sup>
  4. <a href="https://github.com/sponsors/motdotla">Dotenv is supported by the community.</a>
  5. </sup>
  6. </p>
  7. <sup>Special thanks to:</sup>
  8. <br>
  9. <br>
  10. <a href="https://www.warp.dev/?utm_source=github&utm_medium=referral&utm_campaign=dotenv_p_20220831">
  11. <div>
  12. <img src="https://res.cloudinary.com/dotenv-org/image/upload/v1661980709/warp_hi8oqj.png" width="230" alt="Warp">
  13. </div>
  14. <b>Warp is a blazingly fast, Rust-based terminal reimagined to work like a modern app.</b>
  15. <div>
  16. <sup>Get more done in the CLI with real text editing, block-based output, and AI command search.</sup>
  17. </div>
  18. </a>
  19. <br>
  20. <a href="https://retool.com/?utm_source=sponsor&utm_campaign=dotenv">
  21. <div>
  22. <img src="https://res.cloudinary.com/dotenv-org/image/upload/c_scale,w_300/v1664466968/logo-full-black_vidfqf.png" width="270" alt="Retool">
  23. </div>
  24. <b>Retool helps developers build custom internal software, like CRUD apps and admin panels, really fast.</b>
  25. <div>
  26. <sup>Build UIs visually with flexible components, connect to any data source, and write business logic in JavaScript.</sup>
  27. </div>
  28. </a>
  29. <hr>
  30. <br>
  31. <br>
  32. <br>
  33. <br>
  34. </div>
  35. [![dotenv-vault](https://badge.dotenv.org/works-with.svg?r=1)](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1)
  36. # dotenv
  37. <img src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.svg" alt="dotenv" align="right" width="200" />
  38. Dotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](http://12factor.net/config) methodology.
  39. [![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv)
  40. [![Build status](https://ci.appveyor.com/api/projects/status/github/motdotla/dotenv?svg=true)](https://ci.appveyor.com/project/motdotla/dotenv/branch/master)
  41. [![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv)
  42. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
  43. [![Coverage Status](https://img.shields.io/coveralls/motdotla/dotenv/master.svg?style=flat-square)](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration)
  44. [![LICENSE](https://img.shields.io/github/license/motdotla/dotenv.svg)](LICENSE)
  45. [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
  46. [![Featured on Openbase](https://badges.openbase.com/js/featured/dotenv.svg?token=eE0hWPkhC2JGSD4G9hwg5C54EBxjJAyvurGfQsYoKiQ=)](https://openbase.com/js/dotenv?utm_source=embedded&utm_medium=badge&utm_campaign=featured-badge&utm_term=js/dotenv)
  47. [![Limited Edition Tee Original](https://img.shields.io/badge/Limited%20Edition%20Tee%20%F0%9F%91%95-Original-yellow?labelColor=black&style=plastic)](https://dotenv.gumroad.com/l/original)
  48. [![Limited Edition Tee Redacted](https://img.shields.io/badge/Limited%20Edition%20Tee%20%F0%9F%91%95-Redacted-gray?labelColor=black&style=plastic)](https://dotenv.gumroad.com/l/redacted)
  49. ## Install
  50. ```bash
  51. # install locally (recommended)
  52. npm install dotenv --save
  53. ```
  54. Or installing with yarn? `yarn add dotenv`
  55. ## Usage
  56. Create a `.env` file in the root of your project:
  57. ```dosini
  58. S3_BUCKET="YOURS3BUCKET"
  59. SECRET_KEY="YOURSECRETKEYGOESHERE"
  60. ```
  61. As early as possible in your application, import and configure dotenv:
  62. ```javascript
  63. require('dotenv').config()
  64. console.log(process.env) // remove this after you've confirmed it is working
  65. ```
  66. .. or using ES6?
  67. ```javascript
  68. import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
  69. dotenv.config()
  70. import express from 'express'
  71. ```
  72. That's it. `process.env` now has the keys and values you defined in your `.env` file:
  73. ```javascript
  74. require('dotenv').config()
  75. ...
  76. s3.getBucketCors({Bucket: process.env.S3_BUCKET}, function(err, data) {})
  77. ```
  78. ### Multiline values
  79. If you need multiline variables, for example private keys, those are now supported (`>= v15.0.0`) with line breaks:
  80. ```dosini
  81. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
  82. ...
  83. Kh9NV...
  84. ...
  85. -----END RSA PRIVATE KEY-----"
  86. ```
  87. Alternatively, you can double quote strings and use the `\n` character:
  88. ```dosini
  89. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
  90. ```
  91. ### Comments
  92. Comments may be added to your file on their own line or inline:
  93. ```dosini
  94. # This is a comment
  95. SECRET_KEY=YOURSECRETKEYGOESHERE # comment
  96. SECRET_HASH="something-with-a-#-hash"
  97. ```
  98. Comments 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.
  99. ### Parsing
  100. The 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.
  101. ```javascript
  102. const dotenv = require('dotenv')
  103. const buf = Buffer.from('BASIC=basic')
  104. const config = dotenv.parse(buf) // will return an object
  105. console.log(typeof config, config) // object { BASIC : 'basic' }
  106. ```
  107. ### Preload
  108. You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#-r---require-module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.
  109. ```bash
  110. $ node -r dotenv/config your_script.js
  111. ```
  112. The configuration options below are supported as command line arguments in the format `dotenv_config_<option>=value`
  113. ```bash
  114. $ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env dotenv_config_debug=true
  115. ```
  116. Additionally, you can use environment variables to set configuration options. Command line arguments will precede these.
  117. ```bash
  118. $ DOTENV_CONFIG_<OPTION>=value node -r dotenv/config your_script.js
  119. ```
  120. ```bash
  121. $ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/.env
  122. ```
  123. ### Variable Expansion
  124. You need to add the value of another variable in one of your variables? Use [dotenv-expand](https://github.com/motdotla/dotenv-expand).
  125. ### Syncing
  126. You need to keep `.env` files in sync between machines, environments, or team members? Use [dotenv-vault](https://github.com/dotenv-org/dotenv-vault).
  127. ## Examples
  128. See [examples](https://github.com/dotenv-org/examples) of using dotenv with various frameworks, languages, and configurations.
  129. * [nodejs](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs)
  130. * [nodejs (debug on)](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs-debug)
  131. * [nodejs (override on)](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs-override)
  132. * [esm](https://github.com/dotenv-org/examples/tree/master/dotenv-esm)
  133. * [esm (preload)](https://github.com/dotenv-org/examples/tree/master/dotenv-esm-preload)
  134. * [typescript](https://github.com/dotenv-org/examples/tree/master/dotenv-typescript)
  135. * [typescript parse](https://github.com/dotenv-org/examples/tree/master/dotenv-typescript-parse)
  136. * [typescript config](https://github.com/dotenv-org/examples/tree/master/dotenv-typescript-config)
  137. * [webpack](https://github.com/dotenv-org/examples/tree/master/dotenv-webpack)
  138. * [webpack (plugin)](https://github.com/dotenv-org/examples/tree/master/dotenv-webpack2)
  139. * [react](https://github.com/dotenv-org/examples/tree/master/dotenv-react)
  140. * [react (typescript)](https://github.com/dotenv-org/examples/tree/master/dotenv-react-typescript)
  141. * [express](https://github.com/dotenv-org/examples/tree/master/dotenv-express)
  142. * [nestjs](https://github.com/dotenv-org/examples/tree/master/dotenv-nestjs)
  143. ## Documentation
  144. Dotenv exposes two functions:
  145. * `config`
  146. * `parse`
  147. ### Config
  148. `config` will read your `.env` file, parse the contents, assign it to
  149. [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
  150. and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.
  151. ```js
  152. const result = dotenv.config()
  153. if (result.error) {
  154. throw result.error
  155. }
  156. console.log(result.parsed)
  157. ```
  158. You can additionally, pass options to `config`.
  159. #### Options
  160. ##### Path
  161. Default: `path.resolve(process.cwd(), '.env')`
  162. Specify a custom path if your file containing environment variables is located elsewhere.
  163. ```js
  164. require('dotenv').config({ path: '/custom/path/to/.env' })
  165. ```
  166. ##### Encoding
  167. Default: `utf8`
  168. Specify the encoding of your file containing environment variables.
  169. ```js
  170. require('dotenv').config({ encoding: 'latin1' })
  171. ```
  172. ##### Debug
  173. Default: `false`
  174. Turn on logging to help debug why certain keys or values are not being set as you expect.
  175. ```js
  176. require('dotenv').config({ debug: process.env.DEBUG })
  177. ```
  178. ##### Override
  179. Default: `false`
  180. Override any environment variables that have already been set on your machine with values from your .env file.
  181. ```js
  182. require('dotenv').config({ override: true })
  183. ```
  184. ### Parse
  185. The engine which parses the contents of your file containing environment
  186. variables is available to use. It accepts a String or Buffer and will return
  187. an Object with the parsed keys and values.
  188. ```js
  189. const dotenv = require('dotenv')
  190. const buf = Buffer.from('BASIC=basic')
  191. const config = dotenv.parse(buf) // will return an object
  192. console.log(typeof config, config) // object { BASIC : 'basic' }
  193. ```
  194. #### Options
  195. ##### Debug
  196. Default: `false`
  197. Turn on logging to help debug why certain keys or values are not being set as you expect.
  198. ```js
  199. const dotenv = require('dotenv')
  200. const buf = Buffer.from('hello world')
  201. const opt = { debug: true }
  202. const config = dotenv.parse(buf, opt)
  203. // expect a debug message because the buffer is not in KEY=VAL form
  204. ```
  205. ## FAQ
  206. ### Why is the `.env` file not loading my environment variables successfully?
  207. Most likely your `.env` file is not in the correct place. [See this stack overflow](https://stackoverflow.com/questions/42335016/dotenv-file-is-not-loading-environment-variables).
  208. Turn on debug mode and try again..
  209. ```js
  210. require('dotenv').config({ debug: true })
  211. ```
  212. You will receive a helpful error outputted to your console.
  213. ### Should I commit my `.env` file?
  214. No. We **strongly** recommend against committing your `.env` file to version
  215. control. It should only include environment-specific values such as database
  216. passwords or API keys. Your production database should have a different
  217. password than your development database.
  218. ### Should I have multiple `.env` files?
  219. No. We **strongly** recommend against having a "main" `.env` file and an "environment" `.env` file like `.env.test`. Your config should vary between deploys, and you should not be sharing values between environments.
  220. > 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.
  221. >
  222. > – [The Twelve-Factor App](http://12factor.net/config)
  223. ### What rules does the parsing engine follow?
  224. The parsing engine currently supports the following rules:
  225. - `BASIC=basic` becomes `{BASIC: 'basic'}`
  226. - empty lines are skipped
  227. - lines beginning with `#` are treated as comments
  228. - `#` marks the beginning of a comment (unless when the value is wrapped in quotes)
  229. - empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
  230. - inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
  231. - whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`)
  232. - single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
  233. - single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`)
  234. - double quoted values expand new lines (`MULTILINE="new\nline"` becomes
  235. ```
  236. {MULTILINE: 'new
  237. line'}
  238. ```
  239. - backticks are supported (`` BACKTICK_KEY=`This has 'single' and "double" quotes inside of it.` ``)
  240. ### What happens to environment variables that were already set?
  241. By 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.
  242. If instead, you want to override `process.env` use the `override` option.
  243. ```javascript
  244. require('dotenv').config({ override: true })
  245. ```
  246. ### How come my environment variables are not showing up for React?
  247. Your 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.
  248. If you are using [`react-scripts`](https://www.npmjs.com/package/react-scripts), which is distributed through [`create-react-app`](https://create-react-app.dev/), it has dotenv built in but with a quirk. Preface your environment variables with `REACT_APP_`. See [this stack overflow](https://stackoverflow.com/questions/42182577/is-it-possible-to-use-dotenv-in-a-react-project) for more details.
  249. If 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.
  250. ### Can I customize/write plugins for dotenv?
  251. Yes! `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:
  252. ```js
  253. const dotenv = require('dotenv')
  254. const variableExpansion = require('dotenv-expand')
  255. const myEnv = dotenv.config()
  256. variableExpansion(myEnv)
  257. ```
  258. ### How do I use dotenv with `import`?
  259. Simply..
  260. ```javascript
  261. // index.mjs (ESM)
  262. import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
  263. dotenv.config()
  264. import express from 'express'
  265. ```
  266. A little background..
  267. > 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.
  268. >
  269. > – [ES6 In Depth: Modules](https://hacks.mozilla.org/2015/08/es6-in-depth-modules/)
  270. What does this mean in plain language? It means you would think the following would work but it won't.
  271. ```js
  272. // errorReporter.mjs
  273. import { Client } from 'best-error-reporting-service'
  274. export default new Client(process.env.API_KEY)
  275. // index.mjs
  276. import dotenv from 'dotenv'
  277. dotenv.config()
  278. import errorReporter from './errorReporter.mjs'
  279. errorReporter.report(new Error('documented example'))
  280. ```
  281. `process.env.API_KEY` will be blank.
  282. Instead the above code should be written as..
  283. ```js
  284. // errorReporter.mjs
  285. import { Client } from 'best-error-reporting-service'
  286. export default new Client(process.env.API_KEY)
  287. // index.mjs
  288. import * as dotenv from 'dotenv'
  289. dotenv.config()
  290. import errorReporter from './errorReporter.mjs'
  291. errorReporter.report(new Error('documented example'))
  292. ```
  293. Does 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://github.com/dotenv-org/examples/tree/master/dotenv-es6-import-pitfall).
  294. There are two alternatives to this approach:
  295. 1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_)
  296. 2. Create a separate file that will execute `config` first as outlined in [this comment on #133](https://github.com/motdotla/dotenv/issues/133#issuecomment-255298822)
  297. ### What about variable expansion?
  298. Try [dotenv-expand](https://github.com/motdotla/dotenv-expand)
  299. ### What about syncing and securing .env files?
  300. Use [dotenv-vault](https://github.com/dotenv-org/dotenv-vault)
  301. ## Contributing Guide
  302. See [CONTRIBUTING.md](CONTRIBUTING.md)
  303. ## CHANGELOG
  304. See [CHANGELOG.md](CHANGELOG.md)
  305. ## Who's using dotenv?
  306. [These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv)
  307. Projects that expand it often use the [keyword "dotenv" on npm](https://www.npmjs.com/search?q=keywords:dotenv).