Skip to content

Commit f472d6d

Browse files
authored
BREAKING: remove dotenv (#851)
1 parent b6bd8e5 commit f472d6d

File tree

13 files changed

+39
-26
lines changed

13 files changed

+39
-26
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ just add the following `server.js`:
203203
'use strict'
204204

205205
// Read the .env file.
206-
require('dotenv').config()
206+
try {
207+
process.loadEnvFile()
208+
} catch {}
207209

208210
// Require the framework
209211
const Fastify = require('fastify')

args.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
const argv = require('yargs-parser')
4-
const dotenv = require('dotenv')
54
const { requireModule } = require('./util')
5+
const { loadEnvQuitely } = require('./env-loader')
66

77
const DEFAULT_IGNORE = 'node_modules build dist .git bower_components logs .swp .nyc_output'
88

@@ -22,7 +22,7 @@ const DEFAULT_ARGUMENTS = {
2222
}
2323

2424
module.exports = function parseArgs (args) {
25-
dotenv.config()
25+
loadEnvQuitely()
2626
const commandLineArguments = argv(args, {
2727
configuration: {
2828
'populate--': true

env-loader.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
loadEnvQuitely: () => {
3+
try {
4+
process.loadEnvFile()
5+
} catch { }
6+
}
7+
}

generate-swagger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
showHelpForCommand
1313
} = require('./util')
1414
const fp = require('fastify-plugin')
15+
const { loadEnvQuitely } = require('./env-loader')
1516

1617
let Fastify = null
1718

@@ -55,7 +56,7 @@ async function generateSwagger (args) {
5556
}
5657

5758
async function runFastify (opts) {
58-
require('dotenv').config()
59+
loadEnvQuitely()
5960

6061
let file = null
6162

lib/watch/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { GRACEFUL_SHUT } = require('./constants.js')
88

99
const EventEmitter = require('node:events')
1010
const chokidar = require('chokidar')
11+
const { loadEnvQuitely } = require('../../env-loader.js')
1112
const forkPath = path.join(__dirname, './fork.js')
1213

1314
const watch = function (args, ignoreWatch, verboseWatch) {
@@ -39,7 +40,8 @@ const watch = function (args, ignoreWatch, verboseWatch) {
3940

4041
const run = (event) => {
4142
const childEvent = { childEvent: event }
42-
const env = Object.assign({}, require('dotenv').config().parsed, process.env, childEvent)
43+
loadEnvQuitely()
44+
const env = Object.assign({}, process.env, childEvent)
4345
const _child = cp.fork(forkPath, args, {
4446
env,
4547
cwd: process.cwd(),

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"chokidar": "^4.0.0",
5252
"close-with-grace": "^2.1.0",
5353
"commist": "^3.0.0",
54-
"dotenv": "^16.0.0",
5554
"fastify": "^5.0.0",
5655
"fastify-plugin": "^5.0.0",
5756
"generify": "^4.0.0",

print-plugins.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict'
44

55
const parseArgs = require('./args')
6+
const { loadEnvQuitely } = require('./env-loader')
67
const log = require('./log')
78
const {
89
exit,
@@ -38,7 +39,7 @@ function printPlugins (args) {
3839
}
3940

4041
async function runFastify (opts) {
41-
require('dotenv').config()
42+
loadEnvQuitely()
4243

4344
let file = null
4445

print-routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict'
44

55
const parseArgs = require('./args')
6+
const { loadEnvQuitely } = require('./env-loader')
67
const log = require('./log')
78
const {
89
exit,
@@ -38,7 +39,7 @@ function printRoutes (args) {
3839
}
3940

4041
async function runFastify (opts) {
41-
require('dotenv').config()
42+
loadEnvQuitely()
4243

4344
let file = null
4445

start.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
'use strict'
44

5-
require('dotenv').config()
5+
const { loadEnvQuitely } = require('./env-loader')
6+
loadEnvQuitely()
67
const isDocker = require('is-docker')
8+
79
const closeWithGrace = require('close-with-grace')
810
const deepmerge = require('@fastify/deepmerge')({
911
cloneProtoObject (obj) { return obj }

templates/eject-esm/server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Read the .env file.
2-
import * as dotenv from 'dotenv'
3-
41
// Require the framework
52
import Fastify from 'fastify'
63

@@ -11,7 +8,9 @@ import closeWithGrace from 'close-with-grace'
118
import appService from './app.js'
129

1310
// Dotenv config
14-
dotenv.config()
11+
try {
12+
process.loadEnvFile()
13+
} catch {}
1514

1615
// Instantiate Fastify with some config
1716
const app = Fastify({

0 commit comments

Comments
 (0)