diff --git a/README.md b/README.md index 67b64fe4..e68faa55 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,9 @@ just add the following `server.js`: 'use strict' // Read the .env file. -require('dotenv').config() +try { + process.loadEnvFile() +} catch {} // Require the framework const Fastify = require('fastify') diff --git a/args.js b/args.js index 3fa05366..854ca506 100644 --- a/args.js +++ b/args.js @@ -1,8 +1,8 @@ 'use strict' const argv = require('yargs-parser') -const dotenv = require('dotenv') const { requireModule } = require('./util') +const { loadEnvQuitely } = require('./env-loader') const DEFAULT_IGNORE = 'node_modules build dist .git bower_components logs .swp .nyc_output' @@ -22,7 +22,7 @@ const DEFAULT_ARGUMENTS = { } module.exports = function parseArgs (args) { - dotenv.config() + loadEnvQuitely() const commandLineArguments = argv(args, { configuration: { 'populate--': true diff --git a/env-loader.js b/env-loader.js new file mode 100644 index 00000000..4a31be79 --- /dev/null +++ b/env-loader.js @@ -0,0 +1,7 @@ +module.exports = { + loadEnvQuitely: () => { + try { + process.loadEnvFile() + } catch { } + } +} diff --git a/generate-swagger.js b/generate-swagger.js index 54c2c180..9ef87eeb 100644 --- a/generate-swagger.js +++ b/generate-swagger.js @@ -12,6 +12,7 @@ const { showHelpForCommand } = require('./util') const fp = require('fastify-plugin') +const { loadEnvQuitely } = require('./env-loader') let Fastify = null @@ -55,7 +56,7 @@ async function generateSwagger (args) { } async function runFastify (opts) { - require('dotenv').config() + loadEnvQuitely() let file = null diff --git a/lib/watch/index.js b/lib/watch/index.js index 7b9589b6..d766c26f 100644 --- a/lib/watch/index.js +++ b/lib/watch/index.js @@ -8,6 +8,7 @@ const { GRACEFUL_SHUT } = require('./constants.js') const EventEmitter = require('node:events') const chokidar = require('chokidar') +const { loadEnvQuitely } = require('../../env-loader.js') const forkPath = path.join(__dirname, './fork.js') const watch = function (args, ignoreWatch, verboseWatch) { @@ -39,7 +40,8 @@ const watch = function (args, ignoreWatch, verboseWatch) { const run = (event) => { const childEvent = { childEvent: event } - const env = Object.assign({}, require('dotenv').config().parsed, process.env, childEvent) + loadEnvQuitely() + const env = Object.assign({}, process.env, childEvent) const _child = cp.fork(forkPath, args, { env, cwd: process.cwd(), diff --git a/package.json b/package.json index 8b46a614..9780595b 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "chokidar": "^4.0.0", "close-with-grace": "^2.1.0", "commist": "^3.0.0", - "dotenv": "^16.0.0", "fastify": "^5.0.0", "fastify-plugin": "^5.0.0", "generify": "^4.0.0", diff --git a/print-plugins.js b/print-plugins.js index c673130a..166a89c3 100644 --- a/print-plugins.js +++ b/print-plugins.js @@ -3,6 +3,7 @@ 'use strict' const parseArgs = require('./args') +const { loadEnvQuitely } = require('./env-loader') const log = require('./log') const { exit, @@ -38,7 +39,7 @@ function printPlugins (args) { } async function runFastify (opts) { - require('dotenv').config() + loadEnvQuitely() let file = null diff --git a/print-routes.js b/print-routes.js index 0e259414..b49a86de 100644 --- a/print-routes.js +++ b/print-routes.js @@ -3,6 +3,7 @@ 'use strict' const parseArgs = require('./args') +const { loadEnvQuitely } = require('./env-loader') const log = require('./log') const { exit, @@ -38,7 +39,7 @@ function printRoutes (args) { } async function runFastify (opts) { - require('dotenv').config() + loadEnvQuitely() let file = null diff --git a/start.js b/start.js index 23af6ddb..da1fbc76 100755 --- a/start.js +++ b/start.js @@ -2,8 +2,10 @@ 'use strict' -require('dotenv').config() +const { loadEnvQuitely } = require('./env-loader') +loadEnvQuitely() const isDocker = require('is-docker') + const closeWithGrace = require('close-with-grace') const deepmerge = require('@fastify/deepmerge')({ cloneProtoObject (obj) { return obj } diff --git a/templates/eject-esm/server.js b/templates/eject-esm/server.js index 3001fa62..7196ab2b 100644 --- a/templates/eject-esm/server.js +++ b/templates/eject-esm/server.js @@ -1,6 +1,3 @@ -// Read the .env file. -import * as dotenv from 'dotenv' - // Require the framework import Fastify from 'fastify' @@ -11,7 +8,9 @@ import closeWithGrace from 'close-with-grace' import appService from './app.js' // Dotenv config -dotenv.config() +try { + process.loadEnvFile() +} catch {} // Instantiate Fastify with some config const app = Fastify({ diff --git a/templates/eject-ts/server.ts b/templates/eject-ts/server.ts index a2ece791..0fd28fa4 100644 --- a/templates/eject-ts/server.ts +++ b/templates/eject-ts/server.ts @@ -1,12 +1,11 @@ -// Read the .env file. -import * as dotenv from 'dotenv' - // Require the framework import Fastify from 'fastify' // Require library to exit fastify process, gracefully (if possible) import closeWithGrace from 'close-with-grace' -dotenv.config() +try { + process.loadEnvFile() +} catch {} // Instantiate Fastify with some config const app = Fastify({ diff --git a/templates/eject/server.js b/templates/eject/server.js index eb96a0e0..23e139d1 100644 --- a/templates/eject/server.js +++ b/templates/eject/server.js @@ -1,7 +1,9 @@ 'use strict' // Read the .env file. -require('dotenv').config() +try { + process.loadEnvFile() +} catch {} // Require the framework const Fastify = require('fastify') diff --git a/test/start.test.js b/test/start.test.js index 8e732550..ca5f4986 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -518,20 +518,18 @@ test('should start the server at the given prefix (using env var)', async t => { }) test('should start the server at the given prefix (using env var read from dotenv)', async t => { - t.plan(3) + t.plan(4) - const start = proxyquire('../start', { - dotenv: { - config () { - t.pass('config called') - process.env.FASTIFY_PORT = 8080 - } - } + const stub = sinon.stub(process, 'loadEnvFile').callsFake(() => { + t.pass('config called') + process.env.FASTIFY_PORT = 8080 }) + const argv = ['./examples/plugin.js'] const fastify = await start.start(argv) t.equal(fastify.server.address().port, 8080) delete process.env.FASTIFY_PORT + stub.restore() await fastify.close() t.pass('server closed')