我想了解为什么在浏览器中检查变量值时它显示为“未定义”
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 变量进行硬编码,这会起作用......
请问有人可以帮忙吗?
只是想知道您使用什么解决方案来代替 Browserify(正如您在后续回复中提到的)?谢谢!
作为反馈,我没有找到解决方案,因此我决定使用另一种不使用 browserify/js 的方法...