How to import HTML template file into JavaScript module

Marian C.
5 min readApr 28, 2021

import template from './temlplate.html'; does not work in a browser.

It is much easier to develop web applications when HTML templates and JavaScript stay separate. The template of the illustration is borrowed from Human Vectors by Vecteezy.

Although some frameworks oblige developers to mix everything, generally web development is easier when the layout, styles and application logic are separated. You layout a page and its component in .html files, style them in .css files and add the logic implemented in many tiny modules each dedicated to a discrete task. If clients ask to change some color, you change it in CSS without touch the rest of the code. If you have to change some title or add a container for a new component, you just adjust an .html file.

RequireJS, a lovely library implementing Asynchronous Module Definition (AMD) API, for years have enabled developers to divide applications into many components that are executable directly in the browser. Development and debugging is much faster when changes to the code are seen in the browser immediately, without any annoying delay for transpilation, compilation or bundling. Bundling might be necessary for production but during development it is a waste of time in the name of a framework. Now that the native JavaScript modules (also called ES modules or ES6 modules) started to catch up with RequireJS, there is no absolute need to bundle even for production, particularly in enterprise web applications.

--

--

Marian C.
Marian C.

Written by Marian C.

Java, JavaScript and SQL developer. Interested in data collection and visualization.

No responses yet