How to add a custom JavaScript file to Shopify in Liquid?
In this tutorial, I’m going to walk you through a cleaner, more organized approach to customizing your Shopify theme.
While Shopify themes generally allow the insertion of custom JavaScript via built-in sections in the theme editor, I find this method a bit messy.
It’s not the most streamlined way to integrate custom styles into your theme.
Plus, when you hand over a Shopify store to a client who isn’t tech-savvy, embedding code directly through the theme customizer can lead to accidental mishaps or bugs.
That’s why I prefer and recommend a different strategy.
We’ll be linking an external JavaScript file to your Shopify theme, consolidating all your custom scripts neatly in one place.
Let’s dive in and see how this can be done for a more efficient and safer customization process.
For the purpose of this tutorial Iām going to use the default Shopify theme which is āDawnā.
Create the JavaScript file
Let’s start by creating a JavaScript file on your local machine.
This file will hold all those custom code you’ve been thinking about for your Shopify store.
Grab your favorite text editor for this task.
I’m a big fan of VS Code, personally.
Make sure to save your file with a ā.jsā extension to signify it’s a JavaScript file.
A good name for this file is ātheme-custom.jsā.
Why this name?
Well, it’s pretty straightforward and it helps distinguish your custom work from the standard ātheme.jsā file that most themes use.
Think of this naming convention as my little trick.
It’s super helpful for anyone who might take over your theme’s development down the line, especially if they don’t have access to the theme’s original repository.
Also, in Shopify themes with sections, you’ll often find JavaScript files linked to individual sections.
This setup means the scripts only load when their specific section does.
A prime example of this is the Dawn theme, which I’ll use in this tutorial.
But hey, if setting up a local file sounds like a hassle, don’t sweat it.
Shopify’s got your back with its cloud code editor, where you can create and manage files directly in the cloud.
You do not need to minimize the JavaScript file, Shopify will do that automatically when the file is requested by the browser.
Upload the file
After youāve created the file, you need to upload the JavaScript file to your Shopify store.
Go to the Shopify admin, login inside and go to the āOnline Storeā menu item (marked with 1 in the image below), the āThemesā menu item (marked with 2 in the image below) will be auto selected:

Go to the ā…ā(3 dots) button (marked with 3 in the image above) and select āEdit codeā (marked with 4 in the image above).
Now the Shopify code editor will open. Look for the “Assets” folder in the left sidebar like in the image below.
In order to see the files inside the āAssetsā folder click on it and click again on the āAdd a new assetā link (marked with 2 in the image below):

This action will open a pop-up that will allow you to upload the JavaScript file created above to Shopify servers.
You have 2 options here: āUpload a fileā (marked with 1 in the image below) and āCreate a blank fileā (marked with 2 in the image below).
Upload a file
If you have the file created you can upload it by clicking the āAdd fileā button (marked with 3 in the image below):

Create the JavaScript file
If you didnāt create the file you have the option to do it here on the Shopify code editor by clicking the āCreate a blank fileā (marked with 1 in the image below):

In order to create the JavaScript file select the extension which in this case should be āJavaScriptā (marked with 2 in the image above) and name the file ātheme-custom.jsā (marked with 3 in the image above).
When done click on the āDoneā button (marked with 4 in the image above) and youāre done.

Now you can add any custom JavaScript code as you please.
You can do it here inside the Shopify editor or pull the file inside a local environment via Shopify CLI in order to edit the file with a code editor like VS Code.
Do not close the Shopify code editor since we are not done yet.
Link the file to Shopify
You have the JavaScript file created but for it to work and for Shopify to know that a custom JavaScript file needs to be served you need to tell that to Shopify.
This is why now, you need to link the JavaScript file to Shopify.
To do that we need to find the theme index file which is the “theme.liquid”.
Inside the Shopify code editor go to the āLayoutā folder (marked with 1 in the image below) and click on the ātheme.liquidā file to open it.
Add the following code (marked with 3 in the image below):
{{ 'theme-custom.js' | asset_url | script_tag }}
I recommend adding the code below the rest of the JavaScript code links inside the body tag to improve page speed.
In the example below Iāve put my custom JavaScript file exactly above the closing </body> tag.
When all is done click the āSaveā button to save your changes.

Test the JavaScript file
Test the file to make sure that Shopify loads it in the front-end.
Go to your store front-end and open the developer tools by pressing ā + Option + I on MAC or F12 or CTRL + ā§ + I on Windows or Linux. These shortcuts are for Chrome browser.
After youāve open the dev tools, reload the page by pressing ā + ā§ + R ( on MacOS ) or CTRL + F5 or CTRL + ā§ + R (on Windows or Linux ) to do a hard reload.
Now go the āNetworkā tab (marked with 1 in the image below), select the JavaScript profile (marked with 2 in the image below) and type in the āFilterā box (marked with 3 in the image below) the name of your JavaScript file which in this case is ātheme-custom.jsā.

You should see the file (marked with 4 in the image above) but that doesnāt mean the file loaded correctly.
This is why you need to check the āStatusā. If the status is 200 (marked with 5 in the image above) then your file is loaded and whatever styles you add will work correctly.
Conclusion
In conclusion, integrating custom JavaScript into your Shopify store doesn’t have to be a daunting task.
By creating a separate ā.jsā file, such as ātheme-custom.jsā, and uploading it directly to your store, you maintain a clean, organized codebase.
This approach not only keeps your theme customization neat but also makes it easier for any future developers who might work on your site.
Remember, the key is to stay organized and follow best practices to ensure a smooth, bug-free experience for both you and your clients.