Skip to content

Commit

Permalink
replace dotenv with node's --env-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfsousa committed Jun 29, 2024
1 parent 2d38f6b commit 64390fe
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN npm ci --omit=dev
COPY . .

FROM base-dev AS development
CMD ["node", "-r", "./src/lib/tracing.cjs", "--watch", "bin/www.js"]
CMD ["node", "--env-file=.env", "--require=./src/lib/tracing.cjs", "--watch", "bin/www.js"]

FROM gcr.io/distroless/nodejs20-debian12:20.15 AS production
WORKDIR /app
Expand Down
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Swagger: https://express-rest-boilerplate.onrender.com/docs
- [Requirements](#requirements)
- [Features](#features)
- [Getting Started](#getting-started)
- [With a Package Manager](#with-a-package-manager)
- [With a Node Version Manager](#with-a-node-version-manager)
- [With Github](#with-github)
- [With Docker Compose](#with-docker-compose)
- [Folder Structure](#folder-structure)
Expand All @@ -26,14 +26,14 @@ Swagger: https://express-rest-boilerplate.onrender.com/docs

## Requirements

- [Node 18+](https://nodejs.org/en/download/) or [Docker](https://www.docker.com/)
- [Node.js v20+](https://nodejs.org/en/download/) or [Docker](https://www.docker.com/)

## Features

- No transpilers, just vanilla javascript
- Node 18 and ES2022 latest features like [top-level await](https://h3manth.com/ES2022/#top-level-await)
- Node.js v20 and ES2024 latest features
- [ECMAScript modules](https://nodejs.org/api/esm.html#modules-ecmascript-modules) and import aliases
- Code completion and optional type checking with [Typescript](https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html), [JSDoc](https://code.visualstudio.com/docs/languages/javascript#_jsdoc-support) and [VSCode's IntelliSense](https://code.visualstudio.com/docs/nodejs/working-with-javascript#_intellisense)
- Code completion and optional type checking with [JSDoc](https://code.visualstudio.com/docs/languages/javascript#_jsdoc-support)
- Structured logs with [pino](https://github.com/pinojs/pino)
- Metrics with [Prometheus](https://github.com/siimon/prom-client) client
- [OpenTelemetry](https://opentelemetry.io/) auto instrumentation for traces, metrics and logs.
Expand All @@ -44,23 +44,23 @@ Swagger: https://express-rest-boilerplate.onrender.com/docs
- MongoDB ORM with [Mongoose](https://mongoosejs.com/)
- [Docker](https://www.docker.com/) with [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/) and [distroless](https://github.com/GoogleContainerTools/distroless) base image for production builds
- [Docker compose](https://docs.docker.com/compose/) file for easy setup, including observability tools (Grafana, Loki, Tempo and Prometheus)
- Load environment variables from .env files with [dotenv](https://github.com/rolodato/dotenv-safe)
- Load environment variables from .env files with Node's [--env-file](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) flag
- Unit and integration tests with [Vitest](https://vitest.dev/) and [Supertest](https://github.com/visionmedia/supertest)
- Code coverage with [Codecov](https://about.codecov.io/) github action
- Git pre-commit hooks with [husky](https://github.com/typicode/husky) and [lint-staged](https://github.com/okonet/lint-staged)
- Authentication and Authorization with [passport](http://passportjs.org)
- Automatic error handling for express asynchronous routes with [express-async-errors](https://github.com/davidbanham/express-async-errors)
- CI/CD with [Github Actions](https://github.com/features/actions)
- Auto reload with [node's watch mode](https://nodejs.org/dist/latest-v18.x/docs/api/cli.html#--watch)
- Auto reload with [node's watch mode](https://nodejs.org/dist/latest-v20.x/docs/api/cli.html#--watch)
- Server and tests debugging with [VSCode's debugger](https://code.visualstudio.com/docs/editor/debugging)
- [Dependabot](https://docs.github.com/pt/code-security/dependabot/dependabot-security-updates/configuring-dependabot-security-updates) configuration file for automatic dependency updates
- CORS configuration

## Getting Started

### With a Package Manager
### With a Node Version Manager

1. Install Node.JS v18. You can use a node version manager like [fnm](https://github.com/Schniz/fnm).
1. Install Node.JS v20. You can use a node version manager like [fnm](https://github.com/Schniz/fnm).

```bash
fnm install
Expand All @@ -71,33 +71,39 @@ fnm use

```bash
# with npm
npm init @danielfsousa express-rest-boilerplate my-api
npm init @danielfsousa express-rest my-api

# with yarn
yarn create @danielfsousa express-rest-boilerplate my-api
yarn create @danielfsousa express-rest my-api

# with pnpm
pnpm create @danielfsousa express-rest-boilerplate my-api
pnpm create @danielfsousa express-rest my-api

# with bun
bun create @danielfsousa express-rest my-api
```

3. Change .env database credentials

```bash
cp .env.example .env
nano .env
vim .env
```

4. Run server in development mode. The server will restart everytime a file is changed.

```bash
# with npm
npm run start:dev
npm run dev

# with yarn
yarn start:dev
yarn dev

# with pnpm
pnpm start:dev
pnpm dev

# with bun
bun dev
```

### With Github
Expand Down Expand Up @@ -171,7 +177,7 @@ To learn how to search for logs and traces in Grafana, take a look at the [docum
npm run lint # lints code and check formatting
npm run lint:fix # lints code, check formatting and tries to fix found problems
npm run start # starts server
npm run start:dev # starts server in watch mode, waiting for file changes
npm run dev # starts server in watch mode, waiting for file changes
npm run test # runn all tests in watch mode, waiting for file changes
npm run test:cov # runn all tests and report coverage
npm run validate # runs lint and test scripts for files on git's staging area
Expand Down
2 changes: 1 addition & 1 deletion bin/www.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node -r ./src/lib/tracing.cjs
#!/usr/bin/env -S node --env-file=.env --require=./src/lib/tracing.cjs
import main from '../src/main.js'

await main()
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
},
"main": "src/main.js",
"scripts": {
"dev": "node --env-file=.env --require=./src/lib/tracing.cjs --watch bin/www.js",
"lint": "eslint . ; prettier . --check",
"lint:fix": "eslint . --fix ; prettier --write .",
"prepare": "husky install || true",
"start": "bin/www.js",
"start:dev": "node -r ./src/lib/tracing.cjs --watch bin/www.js",
"test": "vitest",
"test:cov": "vitest run --coverage",
"validate": "lint-staged"
Expand All @@ -51,7 +51,6 @@
"body-parser": "^1.19.0",
"cors": "^2.8.3",
"date-fns": "^2.29.3",
"dotenv": "^10.0.0",
"email-templates": "^8.0.7",
"envalid": "^7.2.1",
"express": "^4.15.2",
Expand Down Expand Up @@ -95,7 +94,8 @@
"testcontainers": "^9.0.0",
"vitest": "^0.23.4"
},
"packageManager": "[email protected]+sha512.0e9d42e92bd2318408ed81eaff2da5f78baf23ee7d12a6eed44a6e2901d0f29d7ab715d1b918ade601f72e769a824d9a5c322383f22bbbda5dd396e79de2a077",
"engines": {
"node": ">=18"
"node": ">=20"
}
}
5 changes: 0 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import fs from 'fs'
import path from 'path'
import dotenv from 'dotenv'
import { cleanEnv, str, num, bool } from 'envalid'
import { LogLevel, LogFormat } from '#enums/log'

const appPath = path.dirname(import.meta.url).replace('file:', '')
const pkgPath = path.join(appPath, '../package.json')
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))

dotenv.config({
path: path.join(appPath, '../.env'),
})

const env = cleanEnv(process.env, {
NODE_ENV: str({
choices: ['development', 'test', 'production', 'staging'],
Expand Down

0 comments on commit 64390fe

Please sign in to comment.