NodeJS Google Vision is unable to detect a Project Id in the current environment - node.js

Under Ubuntu environment, NodeJS Google Vision complains:
Error: Unable to detect a Project Id in the current environment.
Even though I already put json credential through
$ export GOOGLE_APPLICATION_CREDENTIALS=/var/credential_google.json"
Please help.

As a quick hack you can try this :
$ GOOGLE_APPLICATION_CREDENTIALS="/var/credential_google.json" node app.js

It's not recommended to use a .json config file locally. I've seen these leak on production servers causing whole platforms to be deleted + the introduce environmental switching and security issues.
Setup Google Cloud CLI.
Now the server will 'look' at the local environment and use that.
If you get the error "Unable to detect a Project Id in the current environment.", it means the auth library cannot find the project default id.
You need to have a base project in Google Cloud set, regardless of environmental variables and project you're running.
Run
gcloud config set project [some-project-id]
Now if you run (node example)
"dev": "NODE_ENV=dev GCP_PROJECT=some-project-id nodemon index.ts",
It will load the project environment. This also allows you to deploy easier with:
"deploy:dev": "y | gcloud app deploy --project some-dev-project app.yaml",
"deploy:prod": "y | gcloud app deploy --project some-prod-project app.yaml"
App engine has security setup automatically with standard environments. With flex you can use one of the manage images Google Provides.

If you are usually a windows user and trying out Ubuntu (like me), the problem is likely with the assumptions that the export command exports variable to all terminal sessions and that you need to open a new terminal to get it to use (as expected in a windows terminal for an environment variable).
The export command doesn't export the variable to another terminal session. So if you export it in a terminal, you use it on the same terminal.
If you would like to export it permanently, then you can try the solution listed here

You can put the path to the JSON credentials directly when instantiating the client, by passing it as an argument.
For example:
const client = new speech.SpeechClient( {keyFilename: "credential_google.json"});
Also, for me setting it in the terminal didn't work.

Related

Using dotenv in cloud run

I have created a .env file in my local system while developing a project. If I upload my project along with .env file, will it work fine or do i have to assign env variables separately?
According to the documentation it won't work like that.
You can set them in Console or provide with --set-env-vars flags during deployment from command line or set id Dockerfile with ENV parameter.

Dotnet Core - Get the application's launch path

Question - Is there a better/right way to get the application's launch path?
Setup -
I have a console application that runs in a Linux Debian docker image. I am building the application using the --runtime linux-x64 command line switch and have all the runtime identifiers set appropriately. I was expecting the application to behave the same whether launching it by calling dotnet MyApplication.dll or ./MyApplication but they are not.
Culprit Code -
I have deployed files in a folder below the application directory that I reference so I do the following to get what I consider my launch path. I have read various articles saying this is the correct way to get what I want, and it works depending on how I launch it.
using var processModule = Process.GetCurrentProcess().MainModule;
var basePath = Path.GetDirectoryName(processModule?.FileName);
When launching this using the comand dotnet MyApplication.dll the above codes path is /usr/share/dotnet
When launching this using the command ./MyApplication.dll the path is then /app
I understand why using dotnet would be different as it is the process that is running my code, but again it was unexpected.
Any help here to what I should use given the current environment would be appreciated. Ultimately I need the path where the console application started from as gathered by the application when it starts up.
Thanks for your help.
This code should work:
public static IConfiguration LoadConfiguration()
{
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
.....
}

Build variables not accessible in my react native app

I need to inject env variables into my code.
I'm using azure pipelines to build my android app in react native.
I have set env variables in the build configuration and I have created a file called appcenter-post-clone.sh. The contents of this file are as follows:
ENV ADMIN_HOST= $ADMIN_HOST
And in my build configuration I have defined
ADMIN_HOST = https://example.com.
But I'm getting this error, [command]/bin/bash /Users/runner/runners/2.160.1/work/1/s/appcenter-post-clone.sh
ENV: https://example.com: No such file or directory. What I fail to understand here is, why is azure treating the value of my env variables as a file? How do I make this work?
The blunder I made here is, I should have used
ENV ADMIN_HOST=$ADMIN_HOST
Without the space. That solved it for me.

How can I make node application see system variables on Google Cloud?

I have variable set in my .bash_rc file:
whoami#cloudshell:~/source/NodePrototype (x-alcove-9999999)$ echo $APP_ENVIRONMENT
LIVE
Yet node.js application out of:
const app_environment_config=require('./APP_ENVIRONMENT/' + process.env.APP_ENVIRONMENT)
produce
2019-02-21 14:18:16 default[20190221t141628] Error: Cannot find module './APP_ENVIRONMENT/undefined'
Eventhough when I enter node shell:
whoami#cloudshell:~/source/NodePrototype (x-alcove-9999999)$ node
> process.env.APP_ENVIRONMENT
'LIVE'
The same part works locally.
It depends on how your Node app is being launched, because looks like is not running in an environment where that variable exists, to make sure print all your current env vars to make this sure: console.log(process.env).
Also, a good practice, when you need something like that, is to use .env files with this module: https://www.npmjs.com/package/dotenv is a good practice to pass configuration to your Node apps.

Kibana 4 configuration for production

I'm trying to deploy Kibana 4 to Azure Websites. I can't use bin/kibanta.bat file since Azure Websites uses start script in package.json to bootstrap application. I tried to update package.json start script to run bin\kibana.js file and environment variables in it. After that Azure starts running Kibana server but I'm getting this error: Uncaught ReferenceError: ZeroClipboard is not defined (http://kibana-site.azurewebsites.net/index.js?_b=5827:89458). Does anyone tried to make Kibana 4 run not using bin\kibana* files? Maybe I have to specify additional environment variables?
The solution is explained here: https://github.com/elastic/kibana/issues/2617
It's a bug that was fixed later.

Resources