0

I have tons of files where I have to add this function.

const Media  = require("../../functions/Media");
Media(....);

Is there any way to:

Directly add Media() function without requiring the file

2 Answers2

0

Try declaring it as global in index file:

const {Media} = require('./functions/Media');

global.MediaGlobal = function Media(input) {
   Media(input)
}
Harshit Rastogi
  • 1,996
  • 1
  • 10
  • 19
0

It really depends on the framework you are using. You need to find the starting index file and append all global functions there. Samples are given below.

// Override Globals in Backend Nodejs

if (typeof global !== "undefined") {
  global.someGlobalFunction = function () {};
}
// Override Globals in Frontend Nodejs/reactjs
// Override Globals in Frontend javascript written on nodejs

if (typeof window !== "undefined") {
  window.someGlobalFunction = function () {};
}

Node.js + Express (Backend)

Look for file define server, app.listen, Defined on top before import other modules

Node.js + React (Frontend)

Look for file define react.render, app.jsx, Defined on top before import other modules

Learn the concept of polyfill:

Best way to polyfill ES6 features in React app that uses create-react-app

xdeepakv
  • 7,835
  • 2
  • 22
  • 32