Search code examples
javascriptreactjsnpmasync-awaites6-promise

Exporting async functions with React, (getting error when running npm build)


I am re-using async functions within my React project, and I have a file called apiRequest.js which looks like this:

const axios = require('axios');
const serverURL = "http://localhost:8080"

getInfo = async function ({email}) {
  try {
      return await axios.post(serverURL, { email: email })
    } catch (error) {
      console.error(error)
    }
}

module.exports = {
    getInfo
};

My component attempts to load the function like this, which works in dev mode:

import { getInfo } from "./../../util/apiRequest.js";

When I run npm run build, I get this error:

Attempted import error: 'getInfo' is not exported from './../../util/apiRequest.js'.

What am I doing wrong when exporting this function?

I have tried removing the const & removing the fat arrow => but still get the same results.


Solution

  • use the ES6 modules import and export

    import axios from 'axios'
    
    export const getInfo = async() => {
    
    }