Machine Learning on JavaScript (Node.js): Libraries for ML

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning on JavaScript (Node.js): Libraries and Tools



Although Python remains the dominant language for machine learning, JavaScript (especially the Node.js platform) is rapidly gaining popularity in this field. This is due to the ability to run models directly in the browser, stack unification (full-stack JS), and the emergence of powerful libraries for working with neural networks, linear algebra, and statistics. In this article, we will review key machine learning libraries for Node.js, their capabilities, and usage examples.



1. TensorFlow.js — the flagship of machine learning in JS



TensorFlow.js is a port of Google's popular framework, adapted for JavaScript. It allows you to train models from scratch, run pre-trained models, and perform inference both in Node.js and in the browser. The library supports GPU acceleration via WebGL (in the browser) or CUDA (via a Node.js addon).



Installation:

npm install @tensorflow/tfjs @tensorflow/tfjs-node


Example: Linear Regression

const tf = require('@tensorflow/tfjs-node');

// Training dataconst xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);const ys = tf.tensor2d([2, 4, 6, 8], [4, 1]);

// Create modelconst model = tf.sequential();model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Compilemodel.compile({optimizer: 'sgd', loss: 'meanSquaredError'});

// Trainmodel.fit(xs, ys, {epochs: 200}).then(() => { // Predict model.predict(tf.tensor2d([5], [1, 1])).print();});


Key Features:

  • Full support for neural networks (convolutional, recurrent, transformers)
  • Loading models from Keras (Python) and TensorFlow Hub
  • Working with computation graphs and automatic differentiation
  • Built-in visualization tools (tfjs-vis)


2. Brain.js — Simplicity and Convenience



Brain.js is one of the simplest libraries for creating neural networks in JavaScript. It is ideal for beginners and small projects. It supports feedforward and recurrent networks (LSTM, GRU), as well as working with time series.



Installation:

npm install brain.js


Example: XOR Classification

const brain = require('brain.js');const net = new brain.NeuralNetwork();

net.train([ {input: [0, 0], output: [0]}, {input: [0, 1], output: [1]}, {input: [1, 0], output: [1]}, {input: [1, 1], output: [0]}]);

const output = net.run([1, 0]); // ~0.98console.log(output);


Features:

  • Minimal entry barrier — just a few lines of code
  • Built-in data normalization support
  • Ability to save and load trained models in JSON
  • Support for recurrent networks (LSTMTimeStep, GRUTimeStep)


3. Synaptic — Modular Architecture



Synaptic is an open-source library that offers a flexible architecture for building neural networks. It has no external dependencies (except pure JS) and allows you to create arbitrary network topologies, including multilayer perceptrons, long short-term memory networks (LSTM), and even capsule networks.



Installation:

npm install synaptic


Example: Creating a Perceptron

const synaptic = require('synaptic');

const { Layer, Network, Trainer } = synaptic;

const inputLayer = new Layer(2);const hiddenLayer = new Layer(3);const outputLayer = new Layer(1);

inputLayer.project(hiddenLayer);hiddenLayer.project(outputLayer);

const myNetwork = new Network({ input: inputLayer, hidden: [hiddenLayer], output: outputLayer});

const t

Recommendations