Machine Learning in JavaScript: Libraries for Node.js — Overview and Examples

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 in JavaScript (Node.js): Libraries and Tools



Although Python remains the dominant language in Data Science, the JavaScript ecosystem (especially Node.js) is actively developing and offers powerful tools for machine learning. This allows frontend and backend developers to integrate ML models directly into web applications, server-side solutions, chatbots, and even IoT devices. In this article, we will review key libraries for Node.js that enable training neural networks, running pre-trained models, and performing classic ML tasks.



1. TensorFlow.js — A Powerful Framework from Google

TensorFlow.js is a complete port of the famous TensorFlow to JavaScript. It works both in the browser and in Node.js, using WebGL or CUDA (via @tensorflow/tfjs-node-gpu) to accelerate computations. The library allows you to load pre-trained models (e.g., MobileNet, PoseNet) and train your own.



Installation:

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


Example: A Simple Neural Network for Regression

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

// Create a simple model: y = 2x - 1const model = tf.sequential();model.add(tf.layers.dense({units: 1, inputShape: [1]}));

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

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

async function train() { await model.fit(xs, ys, {epochs: 250}); model.predict(tf.tensor2d([10], [1, 1])).print(); // Should be ~19}train();

TensorFlow.js is ideal for computer vision, NLP, and time series tasks directly on the server.



2. Brain.js — Neural Networks for Simple Tasks

Brain.js is a lightweight library for creating and training neural networks. It is easy to learn, does not require deep mathematical knowledge, and is great for prototyping. It supports multilayer perceptrons, LSTM, and recurrent networks.



Installation:

npm install brain.js


Example: XOR (Exclusive OR)

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] }], { iterations: 2000 });

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

Brain.js is convenient for classification, prediction, and text analysis (e.g., sentiment analysis).



3. ML.js — A Collection of Classic Algorithms

ML.js is a set of modules for traditional machine learning (without deep neural networks). It includes implementations of KNN, Naive Bayes, SVM, K-means clustering, PCA, decision trees, and much more. Each algorithm is a separate npm package.



Installation (example with KNN):

npm install ml-knn


Example: Fisher's Iris Classification

const KNN = require('ml-knn');

// Training set: [sepal length, sepal width, petal length, petal width]const trainingSet = [[5.1, 3.5, 1.4, 0.2], [7.0, 3.2, 4.7, 1.4], [6.3, 3.3, 6.0, 2.5]];const labels = ['setosa', 'versicolor', 'virginica'];

const knn = new KNN(trainingSet, labels, {k: 3});const prediction = knn.predict([5.9, 3.0, 4.2, 1.5]); // 'versicolor'console.log(prediction);

ML.js is indispensable for quickly implementing statistical methods when neural networks are not needed.



4. Natural — Natural Language Processing (NLP)

Natural is an NLP library for Node.js. It includes tokenization, stemming (Porter, Lancaster), classification (Naive Bayes, logistic regression), TF-IDF, sentiment analysis, and phonetic comparison.



Installation:

npm install natural<                    

Recommendations