I'm trying to use the retrieve_endpoints function in cli_config.js. I added the functions to the module exports and added the require in the cli_config file. Why can't I seem to call the retrieve_endpoints function?
collector.js
async function retrieve_endpoints(enviromnent) {
let configured_endpoints = [];
return configured_endpoints;
}
module.exports = ({ server }) => {
return {
retrieve_endpoints
};
}
cli_config.js
const collector = require('../collector/collector');
function webapi_source({ menus }) {
endpoints = await collector.retrieve_endpoints(env);
}
The main problem with your example is that you replace the exports
object with a function that returns an object in collector.js
, but in cli_config.js
you don't call that function, you expect a bare object. Based on your example I think you don't need that function in the first place. Sulman is also right that there is a syntax error around async / await
, and a few others, but I think those problems are probably just a copy-paste error.
It's 2024, I suggest you rather to write Standard ECMAScript Modules. All you have to do is to rename your files to .mjs
, this will allow you to use the export / import
syntax.
collector.mjs
export async function retrieve_endpoints(enviromnent) {
let configured_endpoints = [];
return configured_endpoints;
}
cli_config.mjs
import { retrieve_endpoints } from '../collector/collector.mjs';
async function webapi_source({ menus }) {
endpoints = await retrieve_endpoints(env);
}
If you have to use CommonJS Modules, then the fix would look something like this:
collector.js
async function retrieve_endpoints(enviromnent) {
let configured_endpoints = [];
return configured_endpoints;
}
module.exports.retrieve_endpoints = retrieve_endpoints;
cli_config.js
const collector = require('../collector/collector');
async function webapi_source({ menus }) {
endpoints = await collector.retrieve_endpoints(env);
}