Load Tensorflow js model from local file system in javascript
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Loading a TensorFlow.js model from a "local file system" means different things in the browser and in Node.js. In Node.js, TensorFlow.js can read a real filesystem path through the file:// scheme, while in the browser you usually either serve the model over HTTP or let the user pick the model files through a file input.
Load from the local filesystem in Node.js
If your code runs under Node.js, use @tensorflow/tfjs-node and point tf.loadLayersModel or tf.loadGraphModel at the model JSON file with a file:// URL.
This works because the Node.js binding has filesystem access. The .json file must be next to the referenced weight shard files, usually named something like group1-shard1of1.bin. TensorFlow.js reads the JSON first, then follows the weight paths listed inside it.
If you are loading a converted SavedModel or GraphModel, the API changes but the storage rule is the same:
In the browser, you cannot read arbitrary paths directly
A browser app cannot normally open file:///Users/.../model.json the way Node.js can. Browsers restrict direct filesystem access for security reasons.
If the user has the model files on disk and needs to load them locally in a web page, the usual solution is a file picker plus tf.io.browserFiles:
That pattern is local from the user's perspective, but it is not the same thing as a direct filesystem path. The user explicitly grants the page access to the chosen files.
Serving the model from a local development server
For browser development, the most common setup is to run a local HTTP server and load the model by URL:
This is often easier than wiring up file inputs, especially when the application itself already has a dev server. It also matches how the app will usually load the model in production.
The key detail is that TensorFlow.js will request the model.json file first and then fetch the referenced weight shard files. If the model JSON points at group1-shard1of1.bin, that file must also be available at the expected relative path.
Choose the right loading method
Use file:// only for Node.js scripts, offline tooling, or server-side inference code.
Use tf.io.browserFiles when a browser user should upload local model files manually.
Use http:// or https:// when the browser app can access a server, including a local dev server running on your machine.
Once you separate those cases, the problem becomes much less confusing.
Common Pitfalls
The most common mistake is trying to use file:// inside browser JavaScript and expecting it to behave like Node.js. It usually fails because the browser sandbox does not grant arbitrary disk access.
Another frequent issue is loading only model.json and forgetting the .bin weight files. TensorFlow.js needs both.
Path mismatches are also common. The weight file paths stored in model.json are relative, so moving only one file without the others breaks loading.
Finally, be careful about which API you call. tf.loadLayersModel is for Layers models, while converted Graph models should be loaded with tf.loadGraphModel.
Summary
- In Node.js, load local models with
file://.../model.json. - In the browser, use
tf.io.browserFilesfor user-selected local files. - A local HTTP server is often the easiest browser-friendly development setup.
- Keep
model.jsonand the weight shard files together in the expected paths. - Match the loader to the model type:
loadLayersModelfor Layers models andloadGraphModelfor Graph models.
Related reading
- Loading a trained Keras model and continue training
- Loading folders of images in tensorflow
- Loading Images in a Directory As Tensorflow Data set
- Loading keras tensorflow model from .h5 file
- load weights require h5py
- Loaded runtime CuDNN library 8.0.5 but source was compiled with 8.1.0
- Load uservoice API on Click
- loading js files and other dependent js files asynchronously
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.