React - can't access environment variables in the server side












0














I have created an app using Create-React-App on the client side and using express in the server side. I have created an .env file for my environment variables where the DB_USER and DB_PASSWORD will be stored.



In the server side, I would like to use a mongoose connection and the .env variables will be the credentials when connecting to the mongodb server.



I'm getting undefined variables in the terminal console when I do process.env.DB_USER. Instead I'm getting my OS environment variables, and NodeJS variables. However, I do see the variables when I do console.log(process.env.DB_USER) in chrome console/client side.



I tried to install dotenv, dotenv-webpack and configure my webpack.config but nothing seem to be working. I have also added REACT_APP_* as prefix to my variables, but still undefined values.



Also when I use dotenv and set this require('dotenv').config() in my index.js file, it complains about an fs dependency??



I just can't get the environment variables to be read in the server folder, ideally it will be nice to have these variables read in the client and server folder. Has anyone encountered this issue before? I'm just learning how to use Create-React-App and webpack. Your help will be appreciated!!!



server.js



//server.js
const express = require('express');
const path = require('path');
const router = require('./routes/routes.js')
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const user = process.env.REACT_APP_DBUSER;
const password = process.env.REACT_APP_DBPASSWORD;

//tells express frontend will reside in client folder
app.use(express.static(path.join(__dirname, '../build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

console.log('node', process.env);
console.log(user);//this is undefined

//connect to mongoDB
mongoose.connect(`mongodb://${user}:${password}@ds141812.mlab.com:41812/note_app_db`, { useNewUrlParser: true });

let conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection:'));
conn.once('open', () => {
console.log('connected to database');
});

//pass in routes from router const
app.use('/',router)

module.exports=app;


webpack.config



const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const config = {
entry: __dirname + '/client/js/index.jsx',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /.jsx?/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env', '@babel/react']
}
},
{
test: /.(png|jpg|svg|gif|mp4|mov)$/,
use: [{
loader: 'file-loader',
options: {
name: '/assets/[name]-[hash:8].[ext]'
}
}]
},
{
test: /.scss$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
publicPath: '/',
contentBase: __dirname + '/build',
port: 5000,
historyApiFallback: {
index: 'index.html'
}
},
plugins: [
new CopyWebpackPlugin([
{ from: './client/index.html', to: './index.html' }
]),
new Dotenv({
path: './.env',
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify("development"),
REACT_APP_DBUSER: JSON.stringify(process.env.REACT_APP_DBUSER),
REACT_APP_DBPASSWORD: JSON.stringify(process.env.REACT_APP_DBPASSWORD)
}
})
]
}


module.exports = config


index.jsx



import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
require('dotenv').config({path: '../../.env'});//is this how it supposed to look like???

ReactDOM.render(<App />, document.getElementById('root'));


package.json



{
"name": "note_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18.0",
"babel-loader": "^8.0.4",
"body-parser": "^1.18.3",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1",
"dotenv": "^6.1.0",
"dotenv-webpack": "^1.5.7",
"env-cmd": "^8.0.2",
"express": "^4.16.4",
"file-loader": "^2.0.0",
"mongoose": "^5.3.11",
"node-sass": "^4.10.0",
"nodemon": "^1.18.6",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-modal": "^3.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack-cli": "^3.1.2"
},
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"webpack": "webpack --mode development",
"dev": "npm run webpack && nodemon bin/www"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}


.env file



REACT_APP_DBUSER="username"
REACT_APP_DBPASSWORD="password"


file structure
enter image description here










share|improve this question
























  • I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
    – Salah-1
    Nov 12 '18 at 4:45
















0














I have created an app using Create-React-App on the client side and using express in the server side. I have created an .env file for my environment variables where the DB_USER and DB_PASSWORD will be stored.



In the server side, I would like to use a mongoose connection and the .env variables will be the credentials when connecting to the mongodb server.



I'm getting undefined variables in the terminal console when I do process.env.DB_USER. Instead I'm getting my OS environment variables, and NodeJS variables. However, I do see the variables when I do console.log(process.env.DB_USER) in chrome console/client side.



I tried to install dotenv, dotenv-webpack and configure my webpack.config but nothing seem to be working. I have also added REACT_APP_* as prefix to my variables, but still undefined values.



Also when I use dotenv and set this require('dotenv').config() in my index.js file, it complains about an fs dependency??



I just can't get the environment variables to be read in the server folder, ideally it will be nice to have these variables read in the client and server folder. Has anyone encountered this issue before? I'm just learning how to use Create-React-App and webpack. Your help will be appreciated!!!



server.js



//server.js
const express = require('express');
const path = require('path');
const router = require('./routes/routes.js')
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const user = process.env.REACT_APP_DBUSER;
const password = process.env.REACT_APP_DBPASSWORD;

//tells express frontend will reside in client folder
app.use(express.static(path.join(__dirname, '../build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

console.log('node', process.env);
console.log(user);//this is undefined

//connect to mongoDB
mongoose.connect(`mongodb://${user}:${password}@ds141812.mlab.com:41812/note_app_db`, { useNewUrlParser: true });

let conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection:'));
conn.once('open', () => {
console.log('connected to database');
});

//pass in routes from router const
app.use('/',router)

module.exports=app;


webpack.config



const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const config = {
entry: __dirname + '/client/js/index.jsx',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /.jsx?/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env', '@babel/react']
}
},
{
test: /.(png|jpg|svg|gif|mp4|mov)$/,
use: [{
loader: 'file-loader',
options: {
name: '/assets/[name]-[hash:8].[ext]'
}
}]
},
{
test: /.scss$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
publicPath: '/',
contentBase: __dirname + '/build',
port: 5000,
historyApiFallback: {
index: 'index.html'
}
},
plugins: [
new CopyWebpackPlugin([
{ from: './client/index.html', to: './index.html' }
]),
new Dotenv({
path: './.env',
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify("development"),
REACT_APP_DBUSER: JSON.stringify(process.env.REACT_APP_DBUSER),
REACT_APP_DBPASSWORD: JSON.stringify(process.env.REACT_APP_DBPASSWORD)
}
})
]
}


module.exports = config


index.jsx



import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
require('dotenv').config({path: '../../.env'});//is this how it supposed to look like???

ReactDOM.render(<App />, document.getElementById('root'));


package.json



{
"name": "note_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18.0",
"babel-loader": "^8.0.4",
"body-parser": "^1.18.3",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1",
"dotenv": "^6.1.0",
"dotenv-webpack": "^1.5.7",
"env-cmd": "^8.0.2",
"express": "^4.16.4",
"file-loader": "^2.0.0",
"mongoose": "^5.3.11",
"node-sass": "^4.10.0",
"nodemon": "^1.18.6",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-modal": "^3.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack-cli": "^3.1.2"
},
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"webpack": "webpack --mode development",
"dev": "npm run webpack && nodemon bin/www"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}


.env file



REACT_APP_DBUSER="username"
REACT_APP_DBPASSWORD="password"


file structure
enter image description here










share|improve this question
























  • I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
    – Salah-1
    Nov 12 '18 at 4:45














0












0








0







I have created an app using Create-React-App on the client side and using express in the server side. I have created an .env file for my environment variables where the DB_USER and DB_PASSWORD will be stored.



In the server side, I would like to use a mongoose connection and the .env variables will be the credentials when connecting to the mongodb server.



I'm getting undefined variables in the terminal console when I do process.env.DB_USER. Instead I'm getting my OS environment variables, and NodeJS variables. However, I do see the variables when I do console.log(process.env.DB_USER) in chrome console/client side.



I tried to install dotenv, dotenv-webpack and configure my webpack.config but nothing seem to be working. I have also added REACT_APP_* as prefix to my variables, but still undefined values.



Also when I use dotenv and set this require('dotenv').config() in my index.js file, it complains about an fs dependency??



I just can't get the environment variables to be read in the server folder, ideally it will be nice to have these variables read in the client and server folder. Has anyone encountered this issue before? I'm just learning how to use Create-React-App and webpack. Your help will be appreciated!!!



server.js



//server.js
const express = require('express');
const path = require('path');
const router = require('./routes/routes.js')
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const user = process.env.REACT_APP_DBUSER;
const password = process.env.REACT_APP_DBPASSWORD;

//tells express frontend will reside in client folder
app.use(express.static(path.join(__dirname, '../build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

console.log('node', process.env);
console.log(user);//this is undefined

//connect to mongoDB
mongoose.connect(`mongodb://${user}:${password}@ds141812.mlab.com:41812/note_app_db`, { useNewUrlParser: true });

let conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection:'));
conn.once('open', () => {
console.log('connected to database');
});

//pass in routes from router const
app.use('/',router)

module.exports=app;


webpack.config



const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const config = {
entry: __dirname + '/client/js/index.jsx',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /.jsx?/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env', '@babel/react']
}
},
{
test: /.(png|jpg|svg|gif|mp4|mov)$/,
use: [{
loader: 'file-loader',
options: {
name: '/assets/[name]-[hash:8].[ext]'
}
}]
},
{
test: /.scss$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
publicPath: '/',
contentBase: __dirname + '/build',
port: 5000,
historyApiFallback: {
index: 'index.html'
}
},
plugins: [
new CopyWebpackPlugin([
{ from: './client/index.html', to: './index.html' }
]),
new Dotenv({
path: './.env',
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify("development"),
REACT_APP_DBUSER: JSON.stringify(process.env.REACT_APP_DBUSER),
REACT_APP_DBPASSWORD: JSON.stringify(process.env.REACT_APP_DBPASSWORD)
}
})
]
}


module.exports = config


index.jsx



import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
require('dotenv').config({path: '../../.env'});//is this how it supposed to look like???

ReactDOM.render(<App />, document.getElementById('root'));


package.json



{
"name": "note_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18.0",
"babel-loader": "^8.0.4",
"body-parser": "^1.18.3",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1",
"dotenv": "^6.1.0",
"dotenv-webpack": "^1.5.7",
"env-cmd": "^8.0.2",
"express": "^4.16.4",
"file-loader": "^2.0.0",
"mongoose": "^5.3.11",
"node-sass": "^4.10.0",
"nodemon": "^1.18.6",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-modal": "^3.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack-cli": "^3.1.2"
},
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"webpack": "webpack --mode development",
"dev": "npm run webpack && nodemon bin/www"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}


.env file



REACT_APP_DBUSER="username"
REACT_APP_DBPASSWORD="password"


file structure
enter image description here










share|improve this question















I have created an app using Create-React-App on the client side and using express in the server side. I have created an .env file for my environment variables where the DB_USER and DB_PASSWORD will be stored.



In the server side, I would like to use a mongoose connection and the .env variables will be the credentials when connecting to the mongodb server.



I'm getting undefined variables in the terminal console when I do process.env.DB_USER. Instead I'm getting my OS environment variables, and NodeJS variables. However, I do see the variables when I do console.log(process.env.DB_USER) in chrome console/client side.



I tried to install dotenv, dotenv-webpack and configure my webpack.config but nothing seem to be working. I have also added REACT_APP_* as prefix to my variables, but still undefined values.



Also when I use dotenv and set this require('dotenv').config() in my index.js file, it complains about an fs dependency??



I just can't get the environment variables to be read in the server folder, ideally it will be nice to have these variables read in the client and server folder. Has anyone encountered this issue before? I'm just learning how to use Create-React-App and webpack. Your help will be appreciated!!!



server.js



//server.js
const express = require('express');
const path = require('path');
const router = require('./routes/routes.js')
const app = express();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const user = process.env.REACT_APP_DBUSER;
const password = process.env.REACT_APP_DBPASSWORD;

//tells express frontend will reside in client folder
app.use(express.static(path.join(__dirname, '../build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

console.log('node', process.env);
console.log(user);//this is undefined

//connect to mongoDB
mongoose.connect(`mongodb://${user}:${password}@ds141812.mlab.com:41812/note_app_db`, { useNewUrlParser: true });

let conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection:'));
conn.once('open', () => {
console.log('connected to database');
});

//pass in routes from router const
app.use('/',router)

module.exports=app;


webpack.config



const webpack = require('webpack');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');

const config = {
entry: __dirname + '/client/js/index.jsx',
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
resolve: {
extensions: ['.js', '.jsx', '.css']
},
module: {
rules: [
{
test: /.jsx?/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['@babel/preset-env', '@babel/react']
}
},
{
test: /.(png|jpg|svg|gif|mp4|mov)$/,
use: [{
loader: 'file-loader',
options: {
name: '/assets/[name]-[hash:8].[ext]'
}
}]
},
{
test: /.scss$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
devServer: {
publicPath: '/',
contentBase: __dirname + '/build',
port: 5000,
historyApiFallback: {
index: 'index.html'
}
},
plugins: [
new CopyWebpackPlugin([
{ from: './client/index.html', to: './index.html' }
]),
new Dotenv({
path: './.env',
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify("development"),
REACT_APP_DBUSER: JSON.stringify(process.env.REACT_APP_DBUSER),
REACT_APP_DBPASSWORD: JSON.stringify(process.env.REACT_APP_DBPASSWORD)
}
})
]
}


module.exports = config


index.jsx



import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
require('dotenv').config({path: '../../.env'});//is this how it supposed to look like???

ReactDOM.render(<App />, document.getElementById('root'));


package.json



{
"name": "note_app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18.0",
"babel-loader": "^8.0.4",
"body-parser": "^1.18.3",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^1.0.1",
"dotenv": "^6.1.0",
"dotenv-webpack": "^1.5.7",
"env-cmd": "^8.0.2",
"express": "^4.16.4",
"file-loader": "^2.0.0",
"mongoose": "^5.3.11",
"node-sass": "^4.10.0",
"nodemon": "^1.18.6",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-modal": "^3.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack-cli": "^3.1.2"
},
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"webpack": "webpack --mode development",
"dev": "npm run webpack && nodemon bin/www"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}


.env file



REACT_APP_DBUSER="username"
REACT_APP_DBPASSWORD="password"


file structure
enter image description here







node.js reactjs webpack environment-variables create-react-app






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 3:38

























asked Nov 12 '18 at 2:51









medev21

3141622




3141622












  • I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
    – Salah-1
    Nov 12 '18 at 4:45


















  • I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
    – Salah-1
    Nov 12 '18 at 4:45
















I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
– Salah-1
Nov 12 '18 at 4:45




I’m client console can u see the values of DB credentials? If so there is something wrong and not good security wise
– Salah-1
Nov 12 '18 at 4:45












2 Answers
2






active

oldest

votes


















0














Replacing my old incorrect answer.



Rather than using that dotenv-webpack package, could you use this package:



https://www.npmjs.com/package/dotenv



In theory, you can remove that new DotEnv from your plugins and then when you call require('dotenv').config(), your webpack.config.js should have access to those env variables, then your webpack define plugin will pick them up and inject them into your code






share|improve this answer























  • I have already added the REACT_APP_ to my variables and still the same issue.
    – medev21
    Nov 12 '18 at 3:01










  • Ah my mistake, my eyes skipped over that part...
    – 3stacks
    Nov 12 '18 at 3:19










  • Could you check out the edited answer above?
    – 3stacks
    Nov 12 '18 at 3:23










  • I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
    – medev21
    Nov 12 '18 at 3:37



















0














Found the solution, so dotenv is fine and I had uninstall dotenv-webpack. In order for the server.js file to read the environment variables, I just needed to add require('dotenv').config(); in this file. It should be good to go!






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255396%2freact-cant-access-environment-variables-in-the-server-side%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Replacing my old incorrect answer.



    Rather than using that dotenv-webpack package, could you use this package:



    https://www.npmjs.com/package/dotenv



    In theory, you can remove that new DotEnv from your plugins and then when you call require('dotenv').config(), your webpack.config.js should have access to those env variables, then your webpack define plugin will pick them up and inject them into your code






    share|improve this answer























    • I have already added the REACT_APP_ to my variables and still the same issue.
      – medev21
      Nov 12 '18 at 3:01










    • Ah my mistake, my eyes skipped over that part...
      – 3stacks
      Nov 12 '18 at 3:19










    • Could you check out the edited answer above?
      – 3stacks
      Nov 12 '18 at 3:23










    • I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
      – medev21
      Nov 12 '18 at 3:37
















    0














    Replacing my old incorrect answer.



    Rather than using that dotenv-webpack package, could you use this package:



    https://www.npmjs.com/package/dotenv



    In theory, you can remove that new DotEnv from your plugins and then when you call require('dotenv').config(), your webpack.config.js should have access to those env variables, then your webpack define plugin will pick them up and inject them into your code






    share|improve this answer























    • I have already added the REACT_APP_ to my variables and still the same issue.
      – medev21
      Nov 12 '18 at 3:01










    • Ah my mistake, my eyes skipped over that part...
      – 3stacks
      Nov 12 '18 at 3:19










    • Could you check out the edited answer above?
      – 3stacks
      Nov 12 '18 at 3:23










    • I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
      – medev21
      Nov 12 '18 at 3:37














    0












    0








    0






    Replacing my old incorrect answer.



    Rather than using that dotenv-webpack package, could you use this package:



    https://www.npmjs.com/package/dotenv



    In theory, you can remove that new DotEnv from your plugins and then when you call require('dotenv').config(), your webpack.config.js should have access to those env variables, then your webpack define plugin will pick them up and inject them into your code






    share|improve this answer














    Replacing my old incorrect answer.



    Rather than using that dotenv-webpack package, could you use this package:



    https://www.npmjs.com/package/dotenv



    In theory, you can remove that new DotEnv from your plugins and then when you call require('dotenv').config(), your webpack.config.js should have access to those env variables, then your webpack define plugin will pick them up and inject them into your code







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 3:23

























    answered Nov 12 '18 at 2:57









    3stacks

    841611




    841611












    • I have already added the REACT_APP_ to my variables and still the same issue.
      – medev21
      Nov 12 '18 at 3:01










    • Ah my mistake, my eyes skipped over that part...
      – 3stacks
      Nov 12 '18 at 3:19










    • Could you check out the edited answer above?
      – 3stacks
      Nov 12 '18 at 3:23










    • I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
      – medev21
      Nov 12 '18 at 3:37


















    • I have already added the REACT_APP_ to my variables and still the same issue.
      – medev21
      Nov 12 '18 at 3:01










    • Ah my mistake, my eyes skipped over that part...
      – 3stacks
      Nov 12 '18 at 3:19










    • Could you check out the edited answer above?
      – 3stacks
      Nov 12 '18 at 3:23










    • I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
      – medev21
      Nov 12 '18 at 3:37
















    I have already added the REACT_APP_ to my variables and still the same issue.
    – medev21
    Nov 12 '18 at 3:01




    I have already added the REACT_APP_ to my variables and still the same issue.
    – medev21
    Nov 12 '18 at 3:01












    Ah my mistake, my eyes skipped over that part...
    – 3stacks
    Nov 12 '18 at 3:19




    Ah my mistake, my eyes skipped over that part...
    – 3stacks
    Nov 12 '18 at 3:19












    Could you check out the edited answer above?
    – 3stacks
    Nov 12 '18 at 3:23




    Could you check out the edited answer above?
    – 3stacks
    Nov 12 '18 at 3:23












    I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
    – medev21
    Nov 12 '18 at 3:37




    I removed new DotEnv and still issue. First I had an issue with the fs dependency so I had to add node: {fs: 'empty'} in webpack.config.js, it ran but still undefined values. Then I added the path in the dotenv, so require('dotenv').config({path: '../../.env'}); but still undefined values.
    – medev21
    Nov 12 '18 at 3:37













    0














    Found the solution, so dotenv is fine and I had uninstall dotenv-webpack. In order for the server.js file to read the environment variables, I just needed to add require('dotenv').config(); in this file. It should be good to go!






    share|improve this answer


























      0














      Found the solution, so dotenv is fine and I had uninstall dotenv-webpack. In order for the server.js file to read the environment variables, I just needed to add require('dotenv').config(); in this file. It should be good to go!






      share|improve this answer
























        0












        0








        0






        Found the solution, so dotenv is fine and I had uninstall dotenv-webpack. In order for the server.js file to read the environment variables, I just needed to add require('dotenv').config(); in this file. It should be good to go!






        share|improve this answer












        Found the solution, so dotenv is fine and I had uninstall dotenv-webpack. In order for the server.js file to read the environment variables, I just needed to add require('dotenv').config(); in this file. It should be good to go!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 15:08









        medev21

        3141622




        3141622






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255396%2freact-cant-access-environment-variables-in-the-server-side%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation