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
exportsobject with a function that returns an object incollector.js, but incli_config.jsyou 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 aroundasync / await, and a few others, but I think those problems are probably just a copy-paste error.Using ECMA modules
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 theexport / importsyntax.collector.mjs
cli_config.mjs
Using CommonJS
If you have to use CommonJS Modules, then the fix would look something like this:
collector.js
cli_config.js