我正在尝试使用Netlify部署我的react应用,该应用对我在前端使用的API使用秘密令牌。我用process.env
在webpack
中定义了令牌。
问题:
在部署Netlify之后,我的应用程序返回“ NO_TOKEN_WARNING”错误。
在本地捆绑时它可以工作,这表明我可能在Netlify中错误地设置了环境var?
我的设置:
creat-react-app
-我从头开始创建了自己的webpack文件。我意识到您可以将react-scripts
用于create-react-app
,但对我而言并非如此。webpack.DefinePlugin
设置令牌的环境变量,该变量从.gitignore文件获取实际的令牌值。 注意:我正在使用webpack-merge
将我的webpack配置文件分成3个:webpack-common,dev和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' };