Back

How to add Axios library with appwirte Cloud Function

  • 0
  • Functions
  • REST API
Zionnite
17 Jan, 2024, 20:25

Hello guys, i just installed a javascript libary to my cloud function project and now i'm getting this error

TL;DR
Developer is trying to add the Axios library to their appwirte Cloud Function. They were initially trying to use `require()` but realized it can only be used in CommonJS and not in ES Modules. The solution is to use `import axios from 'axios';` instead and add `"type": "module"` back to package.json. They are also experiencing an error and the suggested solution is to remove `"type": "module"` from package.json and try again. A link to the discord conversation and the mentioned files are provided for reference.
kamal.panara
17 Jan, 2024, 20:26

can you show me your package.json file

Zionnite
17 Jan, 2024, 20:28

here is the package.json

kamal.panara
17 Jan, 2024, 20:28

remove this "type": "module" from your package.json and try again

Zionnite
17 Jan, 2024, 20:28

okay

kamal.panara
17 Jan, 2024, 20:31

also can you show the main.js file

Zionnite
17 Jan, 2024, 20:32

now, i'm getting this

Zionnite
17 Jan, 2024, 20:34

here is the main.js

kamal.panara
17 Jan, 2024, 20:36

use import axios from 'axios'; for importing axios, and add "type": "module" back to package.json

ideclon
17 Jan, 2024, 20:37

require() is only available in CommonJS, but import can only be used in ES Modules

Zionnite
17 Jan, 2024, 20:37

so how do i use the import with axios

kamal.panara
17 Jan, 2024, 20:41

If your issue has been solved, you can mark this post as closed by adding "[SOLVED]" to the beginning of the title

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more