javascript - Webpack 和环境变量评估?
问题描述
我正在使用 webpack 捆绑我的前端应用程序并从 heroku 加载环境变量。
环境变量在函数内部使用时可以解析,但在外部评估并分配给 const 时不能解析:
import...
/*This line will fail with : 'VM286683:1 Uncaught SyntaxError: Unexpected token u in JSON at position
0 at JSON.parse (<anonymous>)'*/
const geoApiSecretKey = JSON.parse(process.env.GEO_API_SECRET!);
function getGpsPosition() {
//That one will parse the JSON without any issue
const local_geoApiSecretKey = JSON.parse(process.env.GEO_API_SECRET!);
...
}
我的 webpack 配置使用DefinePlugin加载环境变量:
...
plugins: [
new CheckerPlugin(),
new HtmlWebpackPlugin({template: 'index.html.ejs',}),
new webpack.DefinePlugin({
'process.env': {
'GEO_API_SECRET': JSON.stringify(process.env.GEO_API_SECRET)
}
})
],
我猜它与 webpack 或 heroku 无关。我只是错过了一些我在这里找不到的东西。谢谢您的帮助。
解决方案
变量在另一个文件中被覆盖,从而改变了它的性质
推荐阅读
- javascript - 如果我更改复制的数组,则更改原始数组
- sql - 尝试使用 SQL 查询向表中添加多行数据时出错
- ssis - 有什么方法可以创建一个 excel 文件并将其保存或通过电子邮件发送?
- javascript - 在 ID 包含特殊字符的 HTML 元素上使用 JQuery
- php - WooCommerce - 自动从导航栏中隐藏空类别和子类别
- c - C:有没有办法避免给定行中的代码重复?
- python-3.x - 使用硒粘贴到texbox
- java - 无法将 Spring Boot 应用升级到 Flyway 7.0.0
- java - 如何评估 java 运算符的评估
- java - 未加权二维数组中的最短路径。如何显示 BFS 期间采取的步骤/方向