From c1645c96aa8dcd2dbe5f9b086a8164166f2806f2 Mon Sep 17 00:00:00 2001 From: Max Beatty Date: Sun, 13 Jan 2019 10:38:40 -0800 Subject: [PATCH 1/4] add typescript example (#362) --- README.md | 2 +- examples/typescript/.env.example | 3 +++ examples/typescript/.gitignore | 1 + examples/typescript/README.md | 16 ++++++++++++++ examples/typescript/package-lock.json | 32 +++++++++++++++++++++++++++ examples/typescript/package.json | 13 +++++++++++ examples/typescript/src/index.ts | 8 +++++++ examples/typescript/src/lib/env.ts | 5 +++++ examples/typescript/src/lib/errors.ts | 17 ++++++++++++++ examples/typescript/tsconfig.json | 21 ++++++++++++++++++ 10 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 examples/typescript/.env.example create mode 100644 examples/typescript/.gitignore create mode 100644 examples/typescript/README.md create mode 100644 examples/typescript/package-lock.json create mode 100644 examples/typescript/package.json create mode 100644 examples/typescript/src/index.ts create mode 100644 examples/typescript/src/lib/env.ts create mode 100644 examples/typescript/src/lib/errors.ts create mode 100644 examples/typescript/tsconfig.json diff --git a/README.md b/README.md index cb4148b3..1148c3a2 100644 --- a/README.md +++ b/README.md @@ -255,7 +255,7 @@ errorReporter.client.report(new Error('faq example')) `client` will not be configured correctly because it was constructed before `dotenv.config()` was executed. There are (at least) 3 ways to make this work. 1. Preload dotenv: `node --require dotenv/config index.js` (_Note: you do not need to `import` dotenv with this approach_) -2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line with this approach_) +2. Import `dotenv/config` instead of `dotenv` (_Note: you do not need to call `dotenv.config()` and must pass options via the command line or environment variables with this approach_) 3. 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) ## Contributing Guide diff --git a/examples/typescript/.env.example b/examples/typescript/.env.example new file mode 100644 index 00000000..6d0d4d8c --- /dev/null +++ b/examples/typescript/.env.example @@ -0,0 +1,3 @@ +NODE_ENV=example +SAMPLE_KEY=defined +ERR_API_KEY=qwerty12345 diff --git a/examples/typescript/.gitignore b/examples/typescript/.gitignore new file mode 100644 index 00000000..849ddff3 --- /dev/null +++ b/examples/typescript/.gitignore @@ -0,0 +1 @@ +dist/ diff --git a/examples/typescript/README.md b/examples/typescript/README.md new file mode 100644 index 00000000..c766ef93 --- /dev/null +++ b/examples/typescript/README.md @@ -0,0 +1,16 @@ +# TypeScript Example + +> Use `dotenv` in a simple TypeScript project. + +`src/lib/errors.ts` requires an environment variable at time of export so `src/lib/env.ts` contains `dotenv` configuration and is imported first in `src/index.ts`. + +1. `npm install` +2. `npm run build` +3. `npm start` + +Expected output: + +``` +Current NODE_ENV is example +Sample key is defined +``` diff --git a/examples/typescript/package-lock.json b/examples/typescript/package-lock.json new file mode 100644 index 00000000..73d4fc01 --- /dev/null +++ b/examples/typescript/package-lock.json @@ -0,0 +1,32 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "@types/dotenv": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/@types/dotenv/-/dotenv-6.1.0.tgz", + "integrity": "sha512-gmbNb7V1LbJQA4MmH0hVFgqY1cyKsa6RvKC1Xrq0WBnZ0JuuvXKciXx/s8dN0LVXCJd8xO6wIaSFSyUIoGph9g==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/node": { + "version": "10.12.18", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.18.tgz", + "integrity": "sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==", + "dev": true + }, + "dotenv": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.2.0.tgz", + "integrity": "sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w==" + }, + "typescript": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.2.tgz", + "integrity": "sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==", + "dev": true + } + } +} diff --git a/examples/typescript/package.json b/examples/typescript/package.json new file mode 100644 index 00000000..60335bf4 --- /dev/null +++ b/examples/typescript/package.json @@ -0,0 +1,13 @@ +{ + "scripts": { + "build": "tsc", + "start": "node dist/index.js" + }, + "dependencies": { + "dotenv": "latest" + }, + "devDependencies": { + "@types/dotenv": "^6.1.0", + "typescript": "^3.2.2" + } +} diff --git a/examples/typescript/src/index.ts b/examples/typescript/src/index.ts new file mode 100644 index 00000000..ddd60eb1 --- /dev/null +++ b/examples/typescript/src/index.ts @@ -0,0 +1,8 @@ +import "./lib/env" +import errorReporter from "./lib/errors" + +console.log(`Current NODE_ENV is ${process.env.NODE_ENV}`) + +console.log(`Sample key is ${process.env.SAMPLE_KEY}`) + +errorReporter.report(new Error("example")) diff --git a/examples/typescript/src/lib/env.ts b/examples/typescript/src/lib/env.ts new file mode 100644 index 00000000..8cdf10b3 --- /dev/null +++ b/examples/typescript/src/lib/env.ts @@ -0,0 +1,5 @@ +import { resolve } from "path" + +import { config } from "dotenv" + +config({ path: resolve(__dirname, "../../.env.example") }) diff --git a/examples/typescript/src/lib/errors.ts b/examples/typescript/src/lib/errors.ts new file mode 100644 index 00000000..ed07429d --- /dev/null +++ b/examples/typescript/src/lib/errors.ts @@ -0,0 +1,17 @@ +class ErrorReporter { + private apiKey: string + + constructor(apiKey: string) { + if (apiKey === undefined || apiKey === "") { + throw new Error("apiKey required") + } + + this.apiKey = apiKey + } + + report(err: Error) { + // could use apiKey here to send error somewhere + } +} + +export default new ErrorReporter(process.env.ERR_API_KEY) diff --git a/examples/typescript/tsconfig.json b/examples/typescript/tsconfig.json new file mode 100644 index 00000000..d0aa77ed --- /dev/null +++ b/examples/typescript/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "target": "es6", + "noImplicitAny": true, + "moduleResolution": "node", + "sourceMap": true, + "outDir": "dist", + "baseUrl": ".", + "paths": { + "*": [ + "node_modules/*", + "src/types/*" + ] + } + }, + "include": [ + "src/**/*" + ] +} From 406d0d1e380751e179703fb2056fb0f0511ee5d6 Mon Sep 17 00:00:00 2001 From: Max Beatty Date: Tue, 15 Jan 2019 10:41:06 -0800 Subject: [PATCH 2/4] clarify encoding usage (#366) --- README.md | 4 ++-- tests/test-config.js | 2 +- tests/test-env-options.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1148c3a2..7a9584ed 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ $ DOTENV_CONFIG_