Issues with node-fetch

Hi all,

I’m trying to use node-fetch in a serverless app. I want to use this since our Freshdesk account has a very long response time ( > 15 seconds some times) and the request method times out after 6 seconds.

However when using node-fetch (and same for axios) I get an error when running the App. The error is:

thomas.haitsma@escalate-ticket-to-new % fdk run     
Installing dependencies...
+ base-64@0.1.0
+ uuid@8.3.2
+ node-fetch@3.3.0
updated 3 packages and audited 10 packages in 0.48s

3 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Starting local testing server at http://*:10001/    
Append 'dev=true' to your Freshdesk account URL to start testing    
Quit the server with Control-C.

To simulate product, app setup, and external events, visit - http://localhost:10001/web/test
Populating test data with events found in the app
To test the installation page, visit - http://localhost:10001/custom_configs
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/thomas.haitsma/Documents/Workspace/fdapps/apps/escalate-ticket-to-new/server/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /Users/thomas.haitsma/Documents/Workspace/fdapps/apps/escalate-ticket-to-new/server/node_modules/node-fetch/src/index.js from /usr/local/lib/node_modules/fdk/lib/event_handler/framework.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/thomas.haitsma/Documents/Workspace/fdapps/apps/escalate-ticket-to-new/server/node_modules/node-fetch/package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at loadDependency (/usr/local/lib/node_modules/fdk/lib/event_handler/framework.js:37:10)
    at require (/usr/local/lib/node_modules/fdk/lib/event_handler/framework.js:119:16)
    at server.js:4825:39
    at server.js:5844:7
    at Script.runInContext (vm.js:144:12)
    at Request.sandboxExecutor (/usr/local/lib/node_modules/fdk/lib/event_handler/framework.js:226:25)
    at handler (/usr/local/lib/node_modules/fdk/lib/event_handler/framework.js:362:31)
    at Object.invoke (/usr/local/lib/node_modules/fdk/lib/api/smi.js:66:5)
    at dpRouterExec (/usr/local/lib/node_modules/fdk/lib/routes/data-pipe.js:39:32)
    at asyncify (/usr/local/lib/node_modules/fdk/lib/utils/helper-util.js:133:28)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/fdk/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/local/lib/node_modules/fdk/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/usr/local/lib/node_modules/fdk/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/fdk/node_modules/express/lib/router/layer.js:95:5)
    at /usr/local/lib/node_modules/fdk/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/usr/local/lib/node_modules/fdk/node_modules/express/lib/router/index.js:346:12)

This error is thrown as soon as the first function in server.js is called.

I’m declaring the module like this:
const fetch = require('node-fetch')

And in manifest.json it looks like this:

    "base-64": "0.1.0",
    "uuid": "8.3.2",
    "node-fetch": "3.3.0"

I’ve reached out to our technical account manager and he advised me to raise the issue here.

Some extra info:

  • node version: 14.19.3
  • fdk version: 8.6.7

We use yarn to run FDK

any help here would be very much appreciated! :slight_smile:

1 Like

Hi Thomas,

Have you tried to to downgrade the node-fetch version to for example 2.6.6, as the higher versions only use ESM, which might cause some errors

Hi @Thomas_Haitsma,

It could be caused by Node.js when using a dependency marked as a module. You can find out if it’s an ES module.

Importing the ES Modules in JavaScript needs a different syntax, as mentioned in error thrown. Could you try the import for using this dependency on the server.js file?

Hi @Raviraj
can you tell where to install node-fetch in my app directory.

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