Axios errors in app

I am creating an app for Freshdesk (FDK v 8.6.7, nodejs v14.*) and am trying to install and test Axios for use in server.js.

I am using the require command as expected within server.js…

const axios = require('axios');

On testing locally using fdk run I am getting this error when I run my axios.post() command. Specifically, I am firing it on the OnAppInstall, OnAppUpdate and OnAppUninstall events.

Cannot find module '(path/path/path)/server/node_modules/axios'

Note that it is looking for node_modules inside the server directory. But when I install it, it of course goes under the root directory, as expected. I tried using cd server to install it down there, but the axios directory always appears under the root’s node_modules.

I added an empty package.json file under /server and that helped in the install, but now I get the error

Exception occured while validation: Error while parsing file containing serverless functions. Please use the exports section as recommended in the Serverless Apps section of the SDK documentation.

…which is irritating, as this NOT a serverless app.

Any guidance is welcome…

I see two issues here.

First, the axios package dependency: To add an npm package as a dependency for your app, you will need to add it to the "dependencies" key in manifest.json:

{
    "platform-version": "2.2",
    "product": {
       // ...
    },
    "engines": {
      // ...
    },
    "dependencies": {
        "axios": "1.3.4"
    },
    "whitelisted-domains": [ ]
}

Second, about this validation error:

You will see this error if you are not using an exports block in your server/server.js file to export all the serverless functions and event handlers. Named exports don’t work.

For example, in server/server.js:

exports = {

  onTicketCreateHandler: async function (payload) {
    // ...
  },

  onTicketUpdateHandler: async function (payload) {
    // ...
  },

}

I’m still confused by this statement, though. If you are listening to product events in server.js, then, according to fdk (and us), your app uses serverless. :thinking:

@Tom-Kelleher, I’m able to reproduce the same issue with Axios.

To import the Axios library with common JS, use the following script.

const axios = require('axios/dist/node/axios.cjs');

It’s also mentioned in the Installing section of the Axios documentation in NPM.
Our Freshworks CLI (fdk) uses the require() statement for importing the dependencies, and it doesn’t work with ES modules exported in the case of Axios.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.