wallabyjs / quokka

Repository for Quokka.js questions and issues
https://quokkajs.com
1.17k stars 31 forks source link

Error: TypeError: axios.request is not a function  #926

Closed lifeofdave closed 9 months ago

lifeofdave commented 9 months ago

Issue description or question

When importing axios using require I get a Error: TypeError: axios.request is not a function error. The code runs fine when using node.js. Any way round this? For context I'm developing to deploy to Azure functions - hence use of require.

Sample code


const axios = require('axios');
const fs = require('fs');

const localSettings = JSON.parse(fs.readFileSync('../../local.settings.json', 'utf8'));
Object.keys(localSettings.Values).forEach(key => {
    process.env[key] = localSettings.Values[key];
});

const makeHsApiRequest = async (endpoint, data, method = 'get') => {
    let config = {
        method: method,
        maxBodyLength: Infinity,
        url: `https://api.hubapi.com${endpoint}`,
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${process.env['HUBSPOT_PRIVATE_APP_TOKEN']}`
        },
        data: JSON.stringify(data)
    };

    try {
        const response = await axios.request(config)
        return response.data
    }
    catch (error) {
        console.log(error)
    }
}

Quokka.js Console Output

Error making request in hubspot makeHsApiRequest: Error: TypeError: axios.request is not a function 
  ​​​​​at ​​​​​​​​makeHsApiRequest​​​ ​src/utils/hubspot.js:38:9​
  ​​​​​at ​​​​​​​​batchUpdateContacts​​​ ​src/utils/hubspot.js:73:5​
  ​​​​​at ​​​​​​​​testThis​​​ ​src/utils/hubspot.js:170:5​
  ​​​​​at ​​​​​​src/utils/hubspot.js:236:1

Code editor version

Visual Studio Code v1.85.1

OS name and version

OSX

Quokka version

v1.0.609

ArtemGovorov commented 9 months ago

By default for CommonJs projects, Quokka uses esm module for JavaScript files, and it may function slightly differently from standard node loading mechanism (depending on the used node version). There are a couple of ways to make this work as expected:

lifeofdave commented 9 months ago

amazing, thanks for quick reply