Skip to content

Commit b26a113

Browse files
committed
Allow empty .env
1 parent 4ee1a7a commit b26a113

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

.nowignore

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

src/lib/env.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
let env = (typeof window !== 'undefined' && window.env) || {};
22

33
if (typeof window === 'undefined') {
4-
const dotenvConfig = require('dotenv').config();
5-
if (dotenvConfig.error) {
6-
throw dotenvConfig.error;
7-
}
4+
const fs = require('fs');
5+
try {
6+
const stats = fs.statSync('.env');
7+
if (stats && stats.isFile()) {
8+
const dotenvConfig = require('dotenv').config();
9+
if (dotenvConfig.error) {
10+
throw dotenvConfig.error;
11+
}
812

9-
Object.assign(env, dotenvConfig.parsed, process.env);
13+
Object.assign(env, dotenvConfig.parsed, process.env);
14+
}
15+
} catch (e) {
16+
console.log('No .env file existing or accessible:', e);
17+
}
1018
}
1119

1220
module.exports = env;

src/server/server.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
const dotenvConfig = require('dotenv').config();
21
const pick = require('lodash/pick');
3-
if (dotenvConfig.error) {
4-
throw dotenvConfig.error;
5-
}
6-
console.log('Parsed .env config:', dotenvConfig.parsed);
2+
const dotenvConfig = require('../lib/env');
3+
console.log('Parsed .env config:', dotenvConfig);
74
console.log('Node version:', process.version);
85

96
const apm = require('../lib/apm/ServerSide');
@@ -70,7 +67,7 @@ app.prepare().then(() => {
7067

7168
// Defines a filtered set of environment variables to the client.
7269
server.get('/clientEnv.js', (req, res) => {
73-
const envAndDotEnv = { ...dotenvConfig.parsed, ...process.env };
70+
const envAndDotEnv = { ...dotenvConfig, ...process.env };
7471
const filteredEnvObject = JSON.stringify(
7572
pick(
7673
envAndDotEnv,

0 commit comments

Comments
 (0)