apisauce npm is giving error (Uncaught TypeError: axios.create is not a function) in my mac in my reactjs project

546 views Asked by At

I am getting this error in console of browser. However, it had started after when I had done factory reset of my MacBook. I have no idea why I am getting this error in this npm. Because I had used this npm for calling APIs in many project. I had even tried to create new ReactJs project and installed apisauce in that project too. When I called API in that project as well. I was getting same error there too. Please help me fix the issue.

This is my error which I am getting in browser console:

Uncaught TypeError: axios.create is not a function
    at create (apisauce.js:1:1)
    at ./src/api/privateClient.js (privateClient.js:16:1)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)
    at ./src/api/common.js (App.js:36:1)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)
    at ./src/App.js (logo.svg:30:1)

That is how I am calling apisauce npm for API:

import settings from "./settings";
import { create } from "apisauce";

const token = "testcookietoken";

const getToken = () => {
  try {
    return token;
  } catch (e) {
    console.log("Error getting access token: " + e);
  }
};

const privateAPIClient = create({
  baseURL: settings.API_BASE_URL,
});

privateAPIClient.addAsyncRequestTransform(async (request) => {
  const authToken = await getToken();
  console.log(
    " ~ file: privateClient.js:22 ~ privateAPIClient.addAsyncRequestTransform ~ authToken:",
    authToken
  );
  if (!authToken) {
    return;
  }
  request.headers["Authorization"] = "Bearer " + authToken;
});

export default privateAPIClient;

I have tried apisauce npm as above. I was expecting it to call API like it was before. However, it is giving above error in browser console.

1

There are 1 answers

1
Marvin Gooßens On

I had the same error. Changing the version in the package.json to "apisauce": "3.0.0", fixed it for me. With 3.0.1 it did not work.