Skip to main content

JavascriptTagInWrongFile

Identifies {% javascript %} tags in files where JavaScript tags aren't valid. Theme Check reports a JavaScript tag unless it's in section, block, or snippet files.

JavaScript tags are scoped to reusable theme building blocks, so keep them out of templates, layouts, and other theme files.


The following examples contain code snippets that either fail or pass this check.

In the following example, templates/index.liquid contains a {% javascript %} tag:

{% javascript %}
customElements.define('template-index', TemplateIndex);
{% endjavascript %}

In the following example, snippets/card.liquid contains a {% javascript %} tag in a snippet file:

{% javascript %}
customElements.define('product-card', ProductCard);
{% endjavascript %}

The following example contains the default configuration for this check:

JavascriptTagInWrongFile:
enabled: true
severity: error
ParameterDescription
enabledWhether this check is enabled.
severityThe severity of the check.

Anchor to Disabling this checkDisabling this check

Disabling this check isn't recommended. If you can't avoid violating the rule, then you should disable the check using the comment syntax. This ensures that you intentionally disable the check for each instance.

{% # theme-check-disable JavascriptTagInWrongFile %}
{% javascript %}
customElements.define('template-index', TemplateIndex);
{% endjavascript %}
{% # theme-check-enable JavascriptTagInWrongFile %}

Was this page helpful?