react evironment variables .env return undefined
up vote
0
down vote
favorite
I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'
class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}
export default App;
reactjs undefined
add a comment |
up vote
0
down vote
favorite
I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'
class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}
export default App;
reactjs undefined
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'
class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}
export default App;
reactjs undefined
I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'
class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}
export default App;
reactjs undefined
reactjs undefined
asked Nov 10 at 8:31
David whyte
1
1
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35
add a comment |
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You will probably need to call dotenv.config()
as suggested by the document
If you are using create-react-app, you don't need dotenv
package. You will need to add REACT_APP_
prefix to the variable name in .env
file. See the document here
add a comment |
up vote
0
down vote
2 things to note here
- the variable should be prefixed with
REACT_APP_
- You need to restart the server to reflect the changes.
add a comment |
up vote
0
down vote
Hey thanks guy what i did and worked was create a config.js file
const dev={
API_URL:"http://localhost:300"
}
const prod={
API_URL:"llll"
}
const config=process.env.NODE_ENV=='development'?dev:prod
export default config
Then i import wherever maybe in a component and get my data.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You will probably need to call dotenv.config()
as suggested by the document
If you are using create-react-app, you don't need dotenv
package. You will need to add REACT_APP_
prefix to the variable name in .env
file. See the document here
add a comment |
up vote
0
down vote
You will probably need to call dotenv.config()
as suggested by the document
If you are using create-react-app, you don't need dotenv
package. You will need to add REACT_APP_
prefix to the variable name in .env
file. See the document here
add a comment |
up vote
0
down vote
up vote
0
down vote
You will probably need to call dotenv.config()
as suggested by the document
If you are using create-react-app, you don't need dotenv
package. You will need to add REACT_APP_
prefix to the variable name in .env
file. See the document here
You will probably need to call dotenv.config()
as suggested by the document
If you are using create-react-app, you don't need dotenv
package. You will need to add REACT_APP_
prefix to the variable name in .env
file. See the document here
edited Nov 10 at 9:01
answered Nov 10 at 8:48
tingxuanz
365
365
add a comment |
add a comment |
up vote
0
down vote
2 things to note here
- the variable should be prefixed with
REACT_APP_
- You need to restart the server to reflect the changes.
add a comment |
up vote
0
down vote
2 things to note here
- the variable should be prefixed with
REACT_APP_
- You need to restart the server to reflect the changes.
add a comment |
up vote
0
down vote
up vote
0
down vote
2 things to note here
- the variable should be prefixed with
REACT_APP_
- You need to restart the server to reflect the changes.
2 things to note here
- the variable should be prefixed with
REACT_APP_
- You need to restart the server to reflect the changes.
answered Nov 10 at 9:11
kiranvj
12k23249
12k23249
add a comment |
add a comment |
up vote
0
down vote
Hey thanks guy what i did and worked was create a config.js file
const dev={
API_URL:"http://localhost:300"
}
const prod={
API_URL:"llll"
}
const config=process.env.NODE_ENV=='development'?dev:prod
export default config
Then i import wherever maybe in a component and get my data.
add a comment |
up vote
0
down vote
Hey thanks guy what i did and worked was create a config.js file
const dev={
API_URL:"http://localhost:300"
}
const prod={
API_URL:"llll"
}
const config=process.env.NODE_ENV=='development'?dev:prod
export default config
Then i import wherever maybe in a component and get my data.
add a comment |
up vote
0
down vote
up vote
0
down vote
Hey thanks guy what i did and worked was create a config.js file
const dev={
API_URL:"http://localhost:300"
}
const prod={
API_URL:"llll"
}
const config=process.env.NODE_ENV=='development'?dev:prod
export default config
Then i import wherever maybe in a component and get my data.
Hey thanks guy what i did and worked was create a config.js file
const dev={
API_URL:"http://localhost:300"
}
const prod={
API_URL:"llll"
}
const config=process.env.NODE_ENV=='development'?dev:prod
export default config
Then i import wherever maybe in a component and get my data.
answered Nov 12 at 20:36
David whyte
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237293%2freact-evironment-variables-env-return-undefined%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35