When I'm adding script files to a lightning component i'm getting an error as 'script tags only allowed in templates: Source'. Is there another way to add script links to the lightning application?

  • 1
    This could be helped by some code. What did you attempt?
    – pchittum
    Commented Jan 20, 2015 at 23:16

3 Answers 3


You cannot include scripts with a script tag from a component. You can only include them with a script tag from the app.

From the Lightning Components Developer’s Guide

To reference a JavaScript library that you’ve uploaded as a static resource, use a <script> tag in your .app resource.

There are alternatives to the script tag mentioned in the Question How to use jQuery (or any JS lib) in initial post rendering of Lightning Components that you could explore.


Use ltng:require.

ltng:require enables you to load external CSS and JavaScript libraries after you upload them as static resources.


   styles="{!$Resource.SLDSv1 + '/assets/styles/lightning-design-system-ltng.css'}"

   scripts="{!$Resource.jsLibraries + '/jsLibOne.js'}"

   afterScriptsLoaded="{!c.scriptsLoaded}" />

Source:- https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_ltng_require.htm


Here we need to create link or script tags dynamically in the component's controller doInit function.

doInit : function(component, event, helper) {

    if (typeof require !== "undefined") {
        } else {
            //Create head tag dynamically
            var head = document.getElementsByTagName('head')[0];

            //Create link or script tag dynamically
            var link = document.createElement('link');

            //Add appropriate attributes
            link.href = "/resource/bootstrap"; 
            link.rel = "stylesheet";


},//Delimiter for future code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .