Search code examples
javascriptejshead

How to put script file in the head of view using EJS


I included layout to my view and also i need to make JS script to load for this view directly in the head. How to do that ?

// view file
<%- layout('/layout/boilerplate.ejs') %>
<script> 
 // want to load script in the HEAD of my boilerplate file.
</script>

I tried to add head tag and expected it to combined with head in boiler plate but didn`t. Do not want to add to boilerplate because it be downloaded for every route then.


Solution

  • There are a few steps that needs to be followed.

    1. Put your JS files in the /public folder of your project.
    2. Configure the Node server to serve your Static files.
    3. Use a script tag to reference your JS in your ejs file

    Hope this will be helpful. Cheers!!