浏览器|控制台中的 dotenv 变量值“未定义”

问题描述 投票:0回答:2

我想了解为什么在浏览器中检查变量值时它显示为“未定义”

require('dotenv').config()

const apiKey = process.env.GOOGLE_MAPS_API_KEY;
console.log('API key: ' + apiKey);
<script src="js/api_key_bundle.js" type="module"></script>

当我在本地运行 vscode 时,它正确返回 api 密钥,因此,这不是 dotenv 问题。

运行命令后:

browserify apiKey.js -o api_key_bundle.js

然后我进入控制台,值为:'undefined'

我需要的只是使用index.html文件中的api密钥:

var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?key=' + apiKey + '&callback=initMap';
script.defer = true;
script.async = true;

如果我用键值对 apiKey 变量进行硬编码,这会起作用......

请问有人可以帮忙吗?

javascript node.js browserify
2个回答
0
投票

只是想知道您使用什么解决方案来代替 Browserify(正如您在后续回复中提到的)?谢谢!


-1
投票

作为反馈,我没有找到解决方案,因此我决定使用另一种不使用 browserify/js 的方法...

© www.soinside.com 2019 - 2024. All rights reserved.