问题描述
我有一个节点应用程序,我正在尝试使用Google语言api.我想将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为同一目录中的json文件(同级package.json和app.js).
I have a node application and I'm trying to use the google language api. I want to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the json file in the same directory (sibling to package.json and app.js).
我已经在我的app.js文件中尝试了process.env.GOOGLE_APPLICATION_CREDENTIALS = "./key.json";
(使用express),但是它不起作用.我还尝试将"GOOGLE_APPLICATION_CREDENTIALS":"./key.json"
放入package.json中,但效果也不佳.当我在终端export GOOGLE_APPLICATION_CREDENTIALS="./key"
中运行时,它确实起作用.
I had tried process.env.GOOGLE_APPLICATION_CREDENTIALS = "./key.json";
in my app.js file (using express), but it isn't working. I have also tried putting "GOOGLE_APPLICATION_CREDENTIALS":"./key.json"
in my package.json and that didn't work as well. It DOES work when I run in the terminal export GOOGLE_APPLICATION_CREDENTIALS="./key"
.
这是错误消息:
ERROR: Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
感谢任何提示,谢谢!
推荐答案
您设置的GOOGLE_APPLICATION_DEFAULT环境变量可以通过google客户端库访问-它将无法使用相对路径,您需要设置绝对路径.
The GOOGLE_APPLICATION_DEFAULT environment variable you are setting is accessed by the google client libraries - it wont work with a relative path, you'll need to set the absolute path.
这篇关于在节点(尤其是GOOGLE_APPLICATION_CREDENTIALS)中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!