Help needed with importing modules. How to specify that an exported function is a function?

Hello,

I am migrating a module which I used to use with node-red-contrib-function-npm to the new(-ish) method of specifying the modules in the "Setup" tab of the function node.

If I import the example of node-copy-paste This returns an object with properties which are functions and these work fine.

However, if I import the module I was using, gps-to-gpx , this module exports a function.

I specify it like this
image

When I attempt to call it as specified in the module description:

const gpx = createGpx(data.waypoints, {
  activityName: data.activityType,
  startTime: data.startTime,
});

It fails with the Typedef error that createGpx is not a function.

What am I missing? I'd be grateful for any help!

Put the following in your function - node.warn({ "what-is-this": createGpx })

What do you see in the debug panel?


It is likely you should import gps-to-gpx as gpsToGpx then call gpsToGpx.createGpx

Thanks for replying Steve!

image

I already tried the object idea, but I tried it again per your example:
"TypeError: gpsToGpx.createGpx is not a function"

I'm not strong with coding, but the source code of the module contains this line:
export default function createGpx(waypoints, options = {})

It's weird that the "what-is-this" reports a function, but then it isn't!

As ever, very grateful for the help so far!

Edit:

Apparently "Export Default" is part of ES6 which the release notes for V2 say is supported.

Edit 2:
I noticed another difference between this module and others....

E.g. copy-paste definition states that it is imported like this:

var ncp = require("copy-paste");
 
ncp.copy('some text', function () {
  // complete...
})

Whereas gps-to-gpx is defined like this:
const createGpx = require('gps-to-gpx').default;

Note the .default at the end of the require.

Is there a way of getting Node Red to import in that way?

Yes, so gpsToGpx.default should do the trick...

const data = {
  waypoints: [{ latitude: 51, longitude: -1 }, { latitude: 51, longitude: 0 }, { latitude: 51, longitude:1}],
    activityType: "run",
    startTime: new Date()
}
msg.payload = gpsToGpx.default(data.waypoints, {
  activityName: data.activityType,
  startTime: data.startTime,
})
return msg;

image

1 Like

Hooray!

I was so close, too! Thanks very much Steve, you are an absolute hero! (I do regret reaching working age in the 70s instead of the 90s!)

Cheers!

Brian

1 Like

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