I'm trying to deploy my react app using Netlify, which uses a secret token for an API I'm using on the front-end. I defined the token in process.env
with webpack
to do this.
Problem:
After the Netlify deployment, my app returns a "NO_TOKEN_WARNING" error.
It works when bundling locally, which tells me I might be setting up the environment var incorrectly in Netlify?
My setup:
- For prod, I manually entered the API token into Netlify's "environment setup" page (Build & Deploy > Environment > Environment Variables") - I tried enclosing the token with & without quotes.
- I am not using
creat-react-app
- I created my own webpack file from scratch. I realize you could usereact-scripts
forcreate-react-app
, but that isn't the case for me. - For dev, I'm using
webpack.DefinePlugin
to set the environment variable for the token, which gets the actual token value from a .gitignore'd file.
Note: I'm using webpack-merge
separate my webpack config files into 3: webpack-common, dev, and prod.
webpack.dev.js:
...
const config = require('./config.js'); // importing the file with the token
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
contentBase: './client/dist',
},
plugins: [
new webpack.DefinePlugin({
"process.env.SECRET_TOKEN": JSON.stringify(config.SECRET_TOKEN), // token is here
})
],
})
config.js:
module.exports = { SECRET_TOKEN: 'super secret' };