Use axios in Functions. Throws errors

I am trying to use axios in Functions. The package installs successfully but when I want to use it, I get following errors. I installed the latest axios version. But which version is NodeJS within Functions? Or what am I missing?

Here my basic function:

exports = function() {
  const axios = require("axios");
axios.get('https://api.github.com/users/PowellTravis/repos?per_page=100&page=1')
  .then(function (response) {
    onSuccess(response)
  })
  .catch(function (error) {
    console.log(error);
  });
}

and here the error:

failed to execute source for 'node_modules/axios/index.js': FunctionError: failed to execute source for 'node_modules/axios/lib/axios.js': FunctionError: failed to execute source for 'node_modules/axios/lib/core/Axios.js': FunctionError: failed to execute source for 'node_modules/axios/lib/core/dispatchRequest.js': FunctionError: failed to execute source for 'node_modules/axios/lib/adapters/adapters.js': FunctionError: failed to execute source for 'node_modules/axios/lib/adapters/http.js': TypeError: Cannot access member 'Z_SYNC_FLUSH' of undefined
    at node_modules/axios/lib/adapters/http.js:36:10(187)

    at require (native)
    at node_modules/axios/lib/adapters/adapters.js:16:44(41)

    at require (native)
    at node_modules/axios/lib/core/dispatchRequest.js:20:48(77)

    at require (native)
    at node_modules/axios/lib/core/Axios.js:18:55(63)

    at require (native)
    at node_modules/axios/lib/axios.js:17:45(51)

    at require (native)
    at node_modules/axios/index.js:22:45(73)

Hi @borabora,

App Services Functions don’t run in a Node.js process, but in a specific environment that emulates, as close as possible, Node.js v10, so it’s very likely that the latest axios package wouldn’t be compatible.

Have you tried older versions? Some users have reported v1.2.0 as working, YMMV.

1 Like

what is the correct way to make requests? if i try to use “fetch” the response is “fetch is not define” and with axios 1.2.0 the response if i try to make a request with "await axios.get(“url”) is

error:
{“message”:"‘get’ is not a function", “name”: “TypeError”}

Hi @ruben_martin_acebedo,

The following works:

exports = async function(arg) {
  const axios = require('axios').default;

  const response = await axios.get('https://jsonplaceholder.typicode.com/posts/1', {
    responseType: 'json',
    headers: {
      "Accept": "application/json",
      "Accept-Encoding": "deflate"
    }
  });
  
  return response.data;
};