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;
};

I tried to run this and got this error just to test axios

{
    "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\n\tat node_modules/axios/lib/adapters/http.js:36:10(187)\n\n\tat require (native)\n\tat node_modules/axios/lib/adapters/adapters.js:16:44(41)\n\n\tat require (native)\n\tat node_modules/axios/lib/core/dispatchRequest.js:20:48(77)\n\n\tat require (native)\n\tat node_modules/axios/lib/core/Axios.js:18:55(63)\n\n\tat require (native)\n\tat node_modules/axios/lib/axios.js:17:45(51)\n\n\tat require (native)\n\tat node_modules/axios/index.js:22:45(73)\n",
    "error_code": "FunctionExecutionError",
    "link": "https://realm.mongodb.com/groups/63c732d0ff601149806c78ea/apps/63c927c66cfbe1353c093075/logs?co_id=63d5cb15af6f2c4ff6011455"
}

Hi @Tim_Carrender,

Which version of axios have you uploaded as dependency? As specified above, the sample code I provided works for 1.2.0, other versions may not work within the current Function environment.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.