wallabyjs / quokka

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

Unable to use Axios even when installed in the Node project #872

Closed jackmurra-rgare closed 1 year ago

jackmurra-rgare commented 1 year ago

Issue description or question

Is this issue related to Quokka not outputting the expected results of your code?: No

Sample code

const axios = require('axios')

const result = await axios.get('www.example.com')

console.log(result) //?

Quokka.js Console Output

axios.js (node: v16.19.1)

axios.get is not a function
    at quokka.js:3:0

Code editor version

IntelliJ IDE 2023.1.1 Build #IU-231.8770.65

OS name and version

OSX Ventura 13.1 on Intel i7 - work computer also happens in WebStorm on my m2 mac (latest OS) home computer

Screenshot 2023-05-02 at 10 08 08 PM Screenshot 2023-05-02 at 10 08 24 PM Screenshot 2023-05-02 at 10 08 43 PM
ArtemGovorov commented 1 year ago

By default from CommonJs repos Quokka is using standard-things/esm module, that adds support for top-level await, etc.

When standard-things/esm is used, it sometimes works a bit differently from the standard node module resolution. In this case to make it work you may use this:

  const axios = require('axios').default

  const result = await axios.get('https://wallabyjs.com')

  console.log(result) //?

If you would like Quokka to work the exact same way in terms of node module resolution, you may add the stdEsm: false setting to your Quokka config:

{
  ...
  "stdEsm": false,
}

(for example by creating .quokka file in the project with the {"stdEsm": false} content). Please note that in this case however, the top level await will not work for you anymore, the exact same way it won't work if you run the code via node CLI without Quokka.

jackmurra-rgare commented 1 year ago

Thanks for the help Artem!