[SalesForce] Sharing JavaScript between multiple Lightning Components

In a Lightning Component we have the notion of a JavaScript controller and a helper. We can use the helper to call code from the JavaScript controller that is shared between functions, so we can reduce redundancy in the code. This all works great.

However, I was wondering if it is possible, and if so how, to share code between multiple Lightning Components. It doesn't behave like Apex where you can just reference other class.

I was looking at inheritance and abstract components, but I was thinking of code that is shared between components that aren't necessarily related to each other, such as code to handle object and field-level errors.

Best Answer

Currently the recommended mechanism for this is to use a Static Resource and include it in any components that need to share the same JavaScript via ltng:require (loads the library once and only once similar to requirejs).