TensorFlow.js
Keras
loadLayersModel
absolute URLs
machine learning

Only absolute URLs are supported when loading Keras model in Tensorflow.js with loadLayersModel

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error Only absolute URLs are supported usually means tf.loadLayersModel() is running in an environment that cannot resolve the path you passed as a normal browser-relative URL. In practice, this is most often a Node.js issue, not a browser issue. The fix depends on where TensorFlow.js is running: browser code should load from a served HTTP path, while Node.js code should use a proper file:// URL or a real absolute URL.

Understand What loadLayersModel Expects

loadLayersModel() loads a model JSON file plus the referenced weight files. The input string must be something the underlying fetch or I/O layer can resolve correctly.

Typical browser usage:

javascript
const model = await tf.loadLayersModel('/models/model.json');

This works in the browser if /models/model.json is actually being served by your web server.

In Node.js, that same string can fail because there is no browser page URL to resolve it against.

Browser Case: Relative URLs Are Fine if They Are Served

If your code runs in a browser and the model files are part of your web app, a relative or site-root path is normal.

html
1<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
2<script>
3async function run() {
4  const model = await tf.loadLayersModel('/models/model.json');
5  console.log('Loaded', model);
6}
7run();
8</script>

The key condition is that /models/model.json must be reachable through HTTP from the page that loaded the script.

If the file exists only on disk and is not served, the browser cannot fetch it.

Node.js Case: Use file:// or a Real Absolute URL

In Node.js, a bare relative path often triggers the absolute-URL error. For local files, use a file:// URL.

javascript
1const tf = require('@tensorflow/tfjs-node');
2
3async function run() {
4  const model = await tf.loadLayersModel('file:///Users/me/project/model/model.json');
5  console.log('Loaded', model);
6}
7
8run().catch(console.error);

If the model is hosted remotely, use a full HTTP or HTTPS URL instead:

javascript
const model = await tf.loadLayersModel('https://example.com/model/model.json');

The point is that Node.js does not magically resolve 'model/model.json' the same way a browser page would.

Convert Keras Models Correctly First

A Keras .h5 file cannot be loaded directly by TensorFlow.js loadLayersModel() in the browser or general tfjs loader APIs. You need the TensorFlow.js converted model artifacts, usually a model.json plus binary weight shards.

Typical conversion workflow:

bash
1tensorflowjs_converter \
2  --input_format=keras \
3  model.h5 \
4  web_model/

After conversion, web_model/ contains the files that loadLayersModel() expects.

Keep JSON and Weight Files Together

The model.json file includes paths to the weight shards. If you move only the JSON file without the weight files, loading will fail even if the initial URL is correct.

That is why deployment should preserve the model artifact directory structure rather than copying just one file.

Use the Right Loader for the Right Environment

A useful rule is:

  • browser: serve the converted model files and load with an HTTP-relative or absolute URL
  • Node.js local disk: use a file:// absolute path
  • Node.js remote host: use a full https://... URL

Once you align the loader path with the runtime, the error usually disappears.

Common Pitfalls

A common mistake is using a browser-style relative path while actually running under Node.js. That is the most common source of the absolute-URL error.

Another issue is trying to load a raw Keras .h5 file directly with loadLayersModel() instead of converting it to TensorFlow.js format first.

Developers also sometimes serve model.json but forget the weight shard files referenced inside it. Then the first fetch succeeds and later weight fetches fail.

Finally, local files must be accessible to the runtime. A correct-looking URL still fails if the file is not actually present where the process expects it.

Summary

  • The absolute-URL error usually means the model path is being interpreted in the wrong runtime context.
  • Browser code can use relative served paths such as /models/model.json.
  • Node.js local loading should use a file:// absolute path.
  • 'loadLayersModel() expects TensorFlow.js model files, not a raw Keras .h5 file.'
  • Keep model.json and its weight files together when deploying the model.

Course illustration
Course illustration

All Rights Reserved.