What is the best way to put often used javascript functions in another javascript file?


#1

When I put a function like this in my javascript file and call it from that same file, it works fine:

function testFunction() {
  return "output from my test function";
}

But when I put that same function into a separate javascript file I get “testFunction is not defined”, even though I can see that meteor is parsing and loading the file, because if I change the file and save it, the server reloads.

I think this post from SO is relevant, but I was just wondering if there is a better way… seems this post is telling me I have to change the way I call my function (Meteor.myFunctions.functionName) and also change the way I define my function, when I move it to an external file…

Any suggestions are appreciated… I’m new to meteor and javascript!


#2

local / file specific:

function testFunction() {
return “output from my test function”;
}

var testFunction = function () {
return “output from my test function”;
}

global / not file specific:

TestFunction = function () {
return “output from my test function”;
}

You want the global way. And make sure you put the file with this function in a folder appropriate for where you want to use it, /client for just on the client, /server for just on the server, any other folder name, except /packages and /private, to be able to use them on both.


#3

great, thanks for the reply, that works, however, say you have a set of functions that someone else wrote and you just want to bring them into your project… in other languages, you can just paste those functions into your files and it works fine, but it seems with javascript I would have to convert every:

function functionName() {}

to

fuctionName = function() {}

that just seems cumbersome… am I missing something?

my use case is this:
https://github.com/kvz/phpjs/blob/master/functions/var/var_export.js
I like this function and want to bring it, and possibly others that he wrote, into my project to help with debugging… but for each function I would have to change how it’s declared…?


#4

One way for the client is to put the functions into the client/compatibility folder:

This folder is for compatibility JavaScript libraries that rely on variables declared with var at the top level being exported as globals. Files in this directory are executed without being wrapped in a new variable scope. These files are executed before other client-side JavaScript files.

#5

Thanks @robfallows that did the trick on the client, but is there an equivalent of this concept on the server? I can see needing javascript functions on the server as well and not wanting to have to rewrite the files…


#6

On the server you can use Exports and Require to export globally. Much cleaner.


#7

Could you point to documentation on what you’re referring to? Thanks!


#8

https://nodejs.org/api/modules.html

Edit: see better answer below


#9

The only clean solution ATM with meteor is to put everything in packages and to use export / api.use.


#10

Thank you!! (adding characters to 20 minimum)