Jump to content

How to call functions from another .js file ?


Recommended Posts

hey everyone ,

so i've been learning about javascripts and .js files and i've got a problem , i googled my problem but answers didn't help me in rage MP scripts,

here's the problem : im declaring some functions in "functions.js" file , so i can use them in another .js file. but i get this error : function is not defined .

for example : i declared function 'myfunc' in functions.js in this way :

function myfunc(a,b){

        return a*b;


but in the other file when i say for example z=myfunc(5,8) and i expect z to be 40 , but instead i get this error in console : "myfunc is not defined."

i even required "functions.js" before the other .js file : my index.js file is like this :



but the functions are still undefined ..

Can anyone help me out ??!

Link to comment
Share on other sites

got the answer finally :|

so you to do this for example :

inside "functions.js" -->

function myfunc(a,b){

        return a*b;


module.exports = {
    myfunc : myfunc,

inside the other .js file -->

tempJS = require('"path to functions.js"/functions.js');

FUNC = tempJS.myfunc;


now FUNC is the same as myfunc ..

but still , if you guys have a better idea of how to do it , share it plZ ..

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...


module.exports = {
    myfunc: function (a,b){
        return a*b;
    otherfunc: function (a,b){
        return a/b;

let Functions = require('"path to functions.js"/functions.js'); Object.assign(global, Functions);
// myfunc() and otherfunc() are simply added to the global object and can be used everywhere

// you can also use:
global.Functions = require('"path to functions.js"/functions.js');
// Functions.myfunc() and Functions.otherfunc() are added to the global object and can be used everywhere


Edited by rt-2
  • Like 1
Link to comment
Share on other sites

  • 1 month later...

@rt-2 feeling really dump of asking that much but i don't get my error. I have a folder called functions. Within are folders for player, vehicles, etc to have a real clean structure. In serverside package index.js i load the function file which is definded (for an example) like this file:

index.js in functions folder:

let a = require('./player/playerLogin.js');
let b = require('./player/playerLoadFromDB.js');
let c = require('./player/playerGiveWeapon.js');

Object.assign(global, a);
Object.assign(global, b);
Object.assign(global, c);

file playerGiveWeapon.js

var DB = require('../../modules/db');
var Players = require('../../modules/players');

module.exports = {
    pxkPlayerGiveWeapon: function (player, weapon, ammo){
        return "start loading from db";
    test: function (player){
        console.log("start loading from db");

None of the following work (put in index.js of server package and above the index.js of functions is loaded)

let test = test();

it says that "test" is not defined. What am i doing wrong?

Edited by cmdflow
Link to comment
Share on other sites

  • 5 weeks later...
On 3/1/2018 at 7:18 AM, cmdflow said:

Okay it seems you can't do multible 

global.Functions = require(fileA)
global.Functions = require(fileB)
global.Functions = require(fileC)


should be 


global.Functions = require(fileA);
let b = require(fileB); Object.assign(global.Functions, b);
let c = require(fileC); Object.assign(global.Functions, c);

What you do will overwrite the whole object everytime.


Edited by rt-2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...