tfjs

A hardware-accelerated JavaScript library for training and deploying machine learning models in the browser and Node.js.

Library
npm
v4.22.0
19,136stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
62/100Good
Development Activity20
Maintenance44
Community84
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture80
Code Quality78
Innovation82
Learning Curve75

TensorFlow.js (@tensorflow/tfjs) is the union package of the TensorFlow.js ecosystem, bundling the low-level tensor/ops core, the high-level Keras-like Layers API, the SavedModel/TFLite Converter, the Data loading API, and the CPU and WebGL backends into a single install. It lets developers define, train, and run neural networks directly in a web page or a Node.js process, with no server round-trip required for inference.

The package targets two very different audiences with the same API surface: front-end engineers who want to run a pre-trained model client-side (webcam pose estimation, in-browser image classification, on-device recommendation), and ML practitioners porting existing TensorFlow SavedModels or Keras models into a JS runtime. Its automatic backend selection picks WebGL when a GPU context is available and falls back to a pure-JS CPU backend otherwise, while sibling packages (not bundled here) add WASM and WebGPU backends and React Native/Node native bindings for the same core API.

What You Get

  • A unified tf namespace combining tensor ops (tfjs-core), a Keras-like Layers/Sequential/Functional model API (tfjs-layers), and a GraphModel/loadGraphModel importer for converted TensorFlow SavedModels (tfjs-converter)
  • Automatic backend registration and selection between the pure-JS CPU backend and the GPU-accelerated WebGL backend, exposed via tf.findBackend/tf.setBackend
  • A tf.data namespace (from tfjs-data) for building streaming, batched, shuffled input pipelines from CSV files, arrays, or generators, mirroring Python’s tf.data
  • Script-tag distribution via a prebuilt, minified UMD bundle (dist/tf.min.js) alongside the ES module build, so it can be dropped into a plain HTML page with no bundler
  • A tfjs-custom-module CLI (bundled under bin) for analyzing a model and producing a custom, size-optimized bundle containing only the ops that model actually uses

Common Use Cases

  • Client-side inference for pre-trained models - loading a converted TensorFlow/Keras model with tf.loadGraphModel/tf.loadLayersModel to run image classification, object detection, or pose estimation entirely in the visitor’s browser, without sending data to a server
  • In-browser transfer learning - retraining the last layers of an imported model on data captured live from the browser (webcam frames, microphone audio, sensor readings) using the Layers API’s fit()
  • Node.js-side ML without Python - running trained models inside a Node.js backend service that already lives in a JS codebase, avoiding a separate Python inference service
  • Privacy-preserving on-device ML - keeping user data (images, audio, keystrokes) on the client device for inference, useful for regulated or privacy-sensitive applications
  • Educational and interactive ML demos - building live, no-install browser demos (linear regression, MNIST digit recognition) that run and visualize training in real time via the script-tag path

Under The Hood

Architecture The tfjs package itself is a thin composition layer: tfjs/src/index.ts imports and re-exports @tensorflow/tfjs-core, -layers, -converter, and -data, then explicitly imports the CPU and WebGL backend packages so their global registration side effects run, and finally assembles a version object from each sub-package’s version constant. All actual tensor math, autodiff, and kernel dispatch lives in tfjs-core, which this package treats as an opaque dependency. This union-package pattern (a thin re-export wired against a fixed constellation of backends) is what lets the monorepo publish tfjs-core, tfjs-layers, tfjs-backend-webgl, etc. as independently installable packages for bundle-size-conscious consumers while still offering one batteries-included entry point.

Tech Stack Built in TypeScript, compiled with tsc and bundled with Rollup (via @rollup/plugin-typescript, -commonjs, -node-resolve, and Babel for down-leveling to ES5/IE11 targets), with Terser for minification and rollup-plugin-visualizer for bundle analysis. The wider monorepo is orchestrated with Bazel (WORKSPACE, per-package BUILD.bazel files) for incremental, cacheable builds across its dozens of interdependent packages, while this package’s own build/test scripts still run through Yarn. Distribution targets script tags (UMD via jsDelivr/unpkg), ES modules (bundlers), Node (main: dist/tf.node.js), and WeChat mini-programs.

Code Quality Tests are written with Jasmine and executed through Karma against real browsers (Chrome/Firefox, plus BrowserStack for cross-browser and mobile coverage in CI), with karma-typescript compiling TypeScript test files on the fly; index_test.ts asserts that every sub-API (core ops, layers, converter, data, backends) is actually reachable off the composed tf namespace, functioning as an integration smoke test for the union package rather than testing algorithmic logic directly (that lives in the sub-packages). Linting uses TSLint with a custom tslint-no-circular-imports rule, and GitHub Actions workflows (tfjs-ci.yml, nightly/release-branch publish tests) gate merges and releases.

API Design The API deliberately mirrors Python’s TensorFlow/Keras naming (tf.tensor, tf.sequential, model.compile/fit/predict, tf.data.csv), which lowers the learning curve substantially for anyone coming from Python ML work, at the cost of some JavaScript-idiom friction (method chaining on tensors, explicit backend selection, manual memory management via tf.tidy/dispose since JS has no automatic tensor deallocation). Getting started requires only a script tag or a single import with no configuration, though productive use still assumes familiarity with TensorFlow/Keras concepts.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search