creating global variables in strapi

  global, javascript, koa, strapi

This is because for example if I am creating a Helper / Service function, we either need to require(WITH PATH) but this path might contain lots of ../../ due to the depth of the file.

Taking this as example, if I can create a global variable for example

const path = require('path');
const resolve = path.resolve();

global.helper_path = `${resolve}/helpers`;
global.abc_service_path = `${resolve}/service/abc`;

Then I can use something like

const Helper = require(global.helper_path);
const abc = require(global.abc_service_path);

no matter how nested the folder structures are.

P.S. I know we can create global services by doing strapi generate:service <name> but this gets created under the api folder which I would like to keep my api folder only related with models / controllers / routes. Anyway it can be created outside of api folder?

Thank you in advance for any suggestions and advices.

Source: Ask Javascript Questions