Issue with external module

Hi averyone. Need help. I got the issue in Node-red

require() of ES modules is not supported.
require() of /home/ubuntu/scalar-crm-integration/externalModules/node_modules/node-fetch/src/index.js from /usr/lib/node_modules/node-red/node_modules/@node-red/registry/lib/externalModules.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 /home/ubuntu/scalar-crm-integration/externalModules/node_modules/node-fetch/package.json.```

Hi @Tonna100

please share some details on what you are trying to do, where you see that message and what it is related to.

My node-red application hosted on AWS Linux server and managed by pm2. I try to export external modules to function node like moment or node-fetch and get thise issue. But I have not any troubles on my local machine

"Error: Function node failed to load external modules"

What version of node.js are you using on the server?

Node - v14.17.0

Where are you seeing that message exactly?

How are you trying to use the externalModules option?

2021-09-01_154117

Code example:

const getResourse = async (url, headers, retries = 3) => {
     const response = await fetch(url, headers);
    if (response.status !== 200) {
         if (retries > 0) {
             return getResourse(url, headers, retries - 1);
         } else {
             return {'statusCode':response.status, 'message':response.statusText};
         }
    }
   return await response.json();
}

On the local machine thise code works

What version of Node-RED have you got on the server?

Node-red v.1.3.5

To use ESM modules with Node-RED you'll need Node-RED 2.x.

Ok. But I have node-red the same version on my local machine and it works fine

Check to see if you have the same version of node-fetch installed in both places. Perhaps you have an older version locally that can be required, rather than imported.

The fact remains, Node-RED wasn't updated to use import for external modules until 2.x.

1 Like

Thank you for your help. I will try to update Node-red application to 2.x version

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