Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

Commit b918c13

Browse files
committed
Add explicit calls to read config values from .env files when generating Next config for server
I thought Next's `loadConfig` function implicitly read .env files, but I was wrong.
1 parent 59ae604 commit b918c13

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v16.x

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"config:generate": "node ./server/generate-config.js",
88
"dev": "next dev",
9-
"build": "npm run config:generate & npm run build:next",
9+
"build": "npm run config:generate && npm run build:next",
1010
"build:next": "next build",
1111
"lint": "next lint",
1212
"start": "node server.js"
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"@microsoft/applicationinsights-web": "^2.8.5",
2020
"applicationinsights": "^2.3.3",
21+
"dotenv": "^16.0.3",
2122
"next": "^12.2.0",
2223
"react": "^18.2.0",
2324
"react-dom": "^18.2.0",

server/generate-config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
require('dotenv').config({ path: './.env', override: true })
2+
require('dotenv').config({ path: './.env.production', override: true })
3+
require('dotenv').config({ path: './.env.local', override: true })
4+
15
const path = require('path')
26
const fs = require('fs')
37
const loadConfig = require('next/dist/server/config').default
@@ -7,7 +11,6 @@ loadConfig('phase-production-build', path.join(__dirname), nextConfig).then(
711
(config) => {
812
config.distDir = './.next'
913
config.configOrigin = 'next.config.js'
10-
/* eslint-enable no-param-reassign */
1114

1215
fs.writeFileSync('next.config.json', JSON.stringify(config))
1316
}

0 commit comments

Comments
 (0)