Guide · Open source
How to use brain.js: install, train and run neural networks
Updated · written with AI from the project's own documentation and checked against our catalogue data
brain.js is a JavaScript library for creating and training neural networks, which are programs that learn patterns from example data. It runs in both web browsers and Node.js. This guide shows you how to install it, train a small network, get predictions from it and save a trained network for use on a web page.
Quick start
- 1Install the package. Run npm install brain.js in your project folder. If the install fails on GPU support, check the installation note for your platform.
- 2Prepare training data. Write an array of objects, each with an input and an output, using numbers from 0 to 1.
- 3Create a network. Call new brain.NeuralNetwork with an optional config object, or use the defaults.
- 4Train the network. Pass all training patterns to net.train() in one call.
- 5Run a prediction. Call net.run() with a new input to get an output.
- 6Export the trained network. Use toJSON() or toFunction() to save the trained network for use elsewhere.
Install brain.js with npm
If you have Node.js, install brain.js from npm with the package-manager command below. The README does not specify which Node.js version is required.
The README also lists a CDN option for browser use, loading brain.js with a script tag from unpkg. Installing from npm is the route the README describes first.
npm install brain.jsUnderstand the installation note about GPU support
brain.js depends on a native module, headless-gl, for GPU support. In most cases the npm install should just work. If prebuilt binaries cannot be downloaded from GitHub, you may need to build the native module yourself.
The README lists build requirements per platform. On Ubuntu or Debian it gives an apt command for the system packages. On Windows it names Visual Studio Build Tools 2022 and notes that some npm config commands no longer work in modern npm versions. Check these steps only if the install fails.
- Mac OS X: a supported version of Python and XCode
- Ubuntu/Debian: a GNU C++ environment, libxi-dev, working OpenGL drivers, GLEW and pkg-config
- Windows: a supported version of Python, Microsoft Visual Studio Build Tools 2022
npm rebuildCreate and train a feed-forward network
The README's first example uses NeuralNetwork, a feed-forward network trained with backpropagation. You pass an optional config object. The example sets the hidden layer sizes, the activation function and the threshold, and the defaults are shown in the README.
Training takes an array of patterns, each with an input and an output. The README's XOR example trains on four input and output pairs and then calls run to get a prediction. Output values will be close to, not exactly, the target values.
- hiddenLayers: an array of integers, one per hidden layer size
- activation: supported types are sigmoid, relu, leaky-relu and tanh
- binaryThresh: the threshold setting shown in the defaults
const config = {
binaryThresh: 0.5,
hiddenLayers: [3],
activation: 'sigmoid',
leakyReluAlpha: 0.01,
};
const net = new brain.NeuralNetwork(config);
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.987]Use object inputs and outputs
Inputs and outputs can be arrays of numbers from 0 to 1, or objects with named numeric keys. The README's color contrast example uses r, g and b keys as inputs and color names such as black and white as outputs. Input objects do not need to share the same keys.
The README notes that training is computationally expensive, so it suggests training offline or in a Worker, then plugging the trained network into your site using toFunction() or toJSON().
const net = new brain.NeuralNetwork();
net.train([
{ input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 } },
{ input: { r: 0.16, g: 0.09, b: 0.2 }, output: { white: 1 } },
{ input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 } },
]);
const output = net.run({ r: 1, g: 0.4, b: 0 });Pick a network type for sequence data
The README's table of contents covers recurrent types: RNN, LSTM and GRU, plus RNNTimeStep, LSTMTimeStep and GRUTimeStep. The README's recurrent example uses brain.recurrent.RNN with a config for input size, hidden layers, learning rate and decay rate, then calls train and run.
The README also lists an AE type and a forecast method for sequences. Check the README's sections on these before relying on them, since this guide does not cover their details.
const net = new brain.recurrent.RNN(config);
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] },
]);
let output = net.run([0, 0]);Train offline and run in the browser
Because training is expensive, a common pattern is to train in Node.js or a Worker, then save the result. The README names two methods for this: toJSON() saves the trained network as data, and toFunction() gives you a standalone function you can call without the library's training code.
The README also mentions a standalone function section and a train stream, which are useful for larger workflows. Read those sections of the README for exact usage.
- Train on your own machine or in a Worker, not on the page
- Export with toJSON() or toFunction()
- Load the exported network on the page and call it with new inputs
What people use it for
- Classifying inputs, such as sorting colors into light or dark categories, on a web page
- Learning how neural networks train and predict, using small example datasets
- Training a model offline and loading it into a browser with toJSON or toFunction
- Trying recurrent networks such as RNN, LSTM or GRU on sequence data
Frequently asked questions
How do I install brain.js?
Run npm install brain.js in your project. The README lists this as the npm install method and also mentions a CDN script tag for browser use.
What do I need to run brain.js?
The README lists Node.js as the prerequisite but does not specify a version. GPU support depends on a native module, headless-gl, which may need build tools on some systems.
Is brain.js free to use?
brain.js is released under the MIT licence, as shown in the repository details. Check the LICENSE file in the repository for the full terms.
Why does npm install brain.js fail?
The README says the GPU native module may need building from source if prebuilt binaries cannot be downloaded from GitHub. It lists platform build requirements, and running npm rebuild after installing them is the README's suggested step.
Is brain.js a replacement for TensorFlow.js?
The README does not compare brain.js with other frameworks. It describes brain.js as a library for classic network types such as feed-forward, RNN, LSTM and GRU. Projects that need the latest model types may need a different tool, such as TensorFlow.js, which is listed as a comparable project.
Is brain.js still maintained?
brain.js is a continuation of the harthur/brain project, which the README says is no longer maintained. Check the repository's recent activity before starting a new project.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
brain.js suits JavaScript developers, students and small web projects that need simple pattern recognition with classic network types. It is less suited to large-scale deep learning, Python-based research or projects that need modern model types such as large language models, where TensorFlow.js or another tool may be a better fit.
Install options & downloads