node.jsgoogle-apigoogle-compute-enginegoogle-api-nodejs-clientgoogle-client

'request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate. NodeJS


Node Version - v12.16.1 NPM Version- 6.13.4

I am using below code in Nodejs to get VM's list from google cloud using google cloud compute library. Following this link - https://github.com/googleapis/nodejs-compute#before-you-begin


// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
const Compute = require('@google-cloud/compute');
rejectUnauthorized: false;//add when working with https sites
requestCert: false;//add when working with https sites
agent: false;//add when working with https sites
// Creates a client
const compute = new Compute();

async function getVmsExample() {
  // In this example we only want one VM per page
  const options = {
    maxResults: 1,
  };
  const vms = await compute.getVMs(options);
  return vms;

}

// Run the examples
exports.main = async () => {
  const vms = await getVmsExample().catch(console.error);
  if (vms) console.log('VMs:', vms);
  return vms;
};

if (module === require.main) {
  exports.main(console.log);
}

I have already fulfilled all the prerequisites but whwnever I run the code I get the below error-

FetchError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate
    at ClientRequest.<anonymous> (C:\Users\username\Desktop\Full-Stack\NodeJS\node-examples\node_modules\node-fetch\lib\index.js:1455:11)
    at ClientRequest.emit (events.js:311:20)
    at TLSSocket.socketErrorListener (_http_client.js:426:9)
    at TLSSocket.emit (events.js:311:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  message: 'request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate',
  type: 'system',
  errno: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
  code: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
  config: {
    method: 'POST',
    url: 'https://www.googleapis.com/oauth2/v4/token',
    data: {
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      assertion: 'eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJub2RlanNhY2NvdW50QGNvZ2VudC1jYXNlLTI0MjAxNC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInNjb3BlIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9jb21wdXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vb2F1dGgyL3Y0L3Rva2VuIiwiZXhwIjoxNTg3OTU4NDcwLCJpYXQiOjE1ODc5NTQ4NzB9.QSn0bSjHtph4aHGZcXIkWhbbUxampHSOE1BsDkI8dZOah12ICHFOZV0zwrngCPbTMr4MIfTAE7s8fLESjCUEq7lPSvB0uTqU5Lr3fI4FUUEqOGp56821Lh68Z8stWmKb-9HV85h7Ub0aSkJdnezYMcK_-FPu__a3ZLeP3lEnjJu9292DtctGT73XvHaeDTMFiHSI10BlJ2LIPds5lC6XM5I4f6W-4UH0VhUgLo1uCGxJJj0jnkQZbjp11l8KSwsMuIMFvug8G6Y5OKP1E4Ef1EKoEBFGC-vjIjaCPiqkFv4U1yh8xc7ShXh2MBQ8eyUZY1OvDNO4IXexQ-RoWBt0pQ'
    },
    headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
    responseType: 'json',
    params: [Object: null prototype] {},
    paramsSerializer: [Function: paramsSerializer],
    body: '{"grant_type":"urn:ietf:params:oauth:grant-type:jwt-bearer","assertion":"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJub2RlanNhY2NvdW50QGNvZ2VudC1jYXNlLTI0MjAxNC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInNjb3BlIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9jb21wdXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vb2F1dGgyL3Y0L3Rva2VuIiwiZXhwIjoxNTg3OTU4NDcwLCJpYXQiOjE1ODc5NTQ4NzB9.QSn0bSjHtph4aHGZcXIkWhbbUxampHSOE1BsDkI8dZOah12ICHFOZV0zwrngCPbTMr4MIfTAE7s8fLESjCUEq7lPSvB0uTqU5Lr3fI4FUUEqOGp56821Lh68Z8stWmKb-9HV85h7Ub0aSkJdnezYMcK_-FPu__a3ZLeP3lEnjJu9292DtctGT73XvHaeDTMFiHSI10BlJ2LIPds5lC6XM5I4f6W-4UH0VhUgLo1uCGxJJj0jnkQZbjp11l8KSwsMuIMFvug8G6Y5OKP1E4Ef1EKoEBFGC-vjIjaCPiqkFv4U1yh8xc7ShXh2MBQ8eyUZY1OvDNO4IXexQ-RoWBt0pQ"}',    validateStatus: [Function: validateStatus]
  }
}

I tried npm config set strict-ssl false also.

Have anyone any idea what is wrong?

Thanks for help!


Solution

  • When you initiate the client you can either set strictSSL to false ( you did ) or pass in the new valid certificates.

    Set strictSSL to false ( which you already did ) and then update cert files (you should be able to export them here - https://baltimore-cybertrust-root.chain-demos.digicert.com/)

    This link http://registry.npmjs.org/npm could be block by IT admin in your organization. ( Please Verify )

    In addition,You can refer to this stack overflow case as reference for the fix with secure manner and some various alternatives which might assist you for getting direction for the solution.