Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions args.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const argv = require('yargs-parser')
const dotenv = require('dotenv')
const { requireModule } = require('./util')
const { safeParse } = require('./env-parser')

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

Expand All @@ -22,7 +22,7 @@ const DEFAULT_ARGUMENTS = {
}

module.exports = function parseArgs (args) {
dotenv.config()
safeParse()
const commandLineArguments = argv(args, {
configuration: {
'populate--': true
Expand Down
7 changes: 7 additions & 0 deletions env-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
safeParse: () => {
try {
process.loadEnvFile()
} catch { }
}
}
3 changes: 2 additions & 1 deletion generate-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
showHelpForCommand
} = require('./util')
const fp = require('fastify-plugin')
const { safeParse } = require('./env-parser')

let Fastify = null

Expand Down Expand Up @@ -55,7 +56,7 @@ async function generateSwagger (args) {
}

async function runFastify (opts) {
require('dotenv').config()
safeParse()

let file = null

Expand Down
4 changes: 3 additions & 1 deletion lib/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { GRACEFUL_SHUT } = require('./constants.js')

const EventEmitter = require('node:events')
const chokidar = require('chokidar')
const { safeParse } = require('../../env-parser.js')
const forkPath = path.join(__dirname, './fork.js')

const watch = function (args, ignoreWatch, verboseWatch) {
Expand Down Expand Up @@ -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)
safeParse()
const env = Object.assign({}, process.env, childEvent)
const _child = cp.fork(forkPath, args, {
env,
cwd: process.cwd(),
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion print-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict'

const parseArgs = require('./args')
const { safeParse } = require('./env-parser')
const log = require('./log')
const {
exit,
Expand Down Expand Up @@ -38,7 +39,7 @@ function printPlugins (args) {
}

async function runFastify (opts) {
require('dotenv').config()
safeParse()

let file = null

Expand Down
3 changes: 2 additions & 1 deletion print-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict'

const parseArgs = require('./args')
const { safeParse } = require('./env-parser')
const log = require('./log')
const {
exit,
Expand Down Expand Up @@ -38,7 +39,7 @@ function printRoutes (args) {
}

async function runFastify (opts) {
require('dotenv').config()
safeParse()

let file = null

Expand Down
4 changes: 3 additions & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

'use strict'

require('dotenv').config()
const { safeParse } = require('./env-parser')
safeParse()
const isDocker = require('is-docker')

const closeWithGrace = require('close-with-grace')
const deepmerge = require('@fastify/deepmerge')({
cloneProtoObject (obj) { return obj }
Expand Down
7 changes: 3 additions & 4 deletions templates/eject-esm/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Read the .env file.
import * as dotenv from 'dotenv'

// Require the framework
import Fastify from 'fastify'

Expand All @@ -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({
Expand Down
7 changes: 3 additions & 4 deletions templates/eject-ts/server.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
4 changes: 3 additions & 1 deletion templates/eject/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

// Read the .env file.
require('dotenv').config()
try {
process.loadEnvFile()
} catch {}

// Require the framework
const Fastify = require('fastify')
Expand Down
11 changes: 4 additions & 7 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,11 @@ 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)

const start = proxyquire('../start', {
dotenv: {
config () {
t.pass('config called')
process.env.FASTIFY_PORT = 8080
}
}
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)
Expand Down
Loading