Unconstrained Function Minimization
fmin is a JavaScript library designed for unconstrained numerical function minimization, offering implementations of several common optimization algorithms. These include the Nelder-Mead method, Gradient Descent, Wolf Line Search, and the Non-Linear Conjugate Gradient method. Currently at version 0.0.4, the package appears to be largely unmaintained; its last known commit was over eight years ago, indicating an abandoned status. While it originally differentiated itself by providing interactive visualizations and an accompanying blog post to explain the algorithms, these are external to the core npm package. Due to its very early development stage (sub-1.0 version) and prolonged inactivity, users should be aware of potential limitations regarding modern JavaScript compatibility, lack of TypeScript type definitions, and unoptimized performance compared to contemporary alternatives. The project has no clear release cadence, and development seems to have ceased.
Common errors
-
ReferenceError: fmin is not defined
cause Attempting to use `fmin` without properly importing or requiring it, or if the module failed to load.fixEnsure you have `const fmin = require('fmin');` at the top of your CommonJS file, or that your bundler correctly handles the import in an ESM context. -
TypeError: fmin.nelderMead is not a function
cause The `fmin` object was imported incorrectly, or `nelderMead` property does not exist on the imported object (e.g., due to a failed or partial import).fixVerify that `const fmin = require('fmin');` correctly assigns the module's export to the `fmin` variable, and that the `nelderMead` function is indeed available as a property on it. Avoid named imports like `import { nelderMead } from 'fmin';` as it's not a named export. -
ReferenceError: require is not defined
cause Trying to use CommonJS `require` syntax in an environment that is pure ESM (e.g., a Node.js ESM module without `createRequire` or a browser environment without a bundler).fixIf in a pure ESM environment, you may need a bundler to process `require` statements or investigate dynamic `import()` if feasible. For browser usage, ensure your code is bundled to resolve `require` calls.
Warnings
- gotcha The `fmin` package is largely unmaintained, with its last commit over eight years ago (as of 2026) and remaining at version 0.0.4. This indicates a lack of ongoing support and potential for unaddressed bugs or security vulnerabilities.
- gotcha The package lacks official TypeScript type definitions, which can lead to poorer developer experience and potential type-related errors when integrating into TypeScript projects.
- gotcha Performance of algorithms may not be optimized for modern JavaScript engines or large-scale optimization problems compared to newer, actively developed libraries. Benchmarking against current alternatives is advisable.
- gotcha The library primarily uses CommonJS module syntax, and direct ESM `import` statements might not work as expected in all environments without a bundler or specific Node.js configuration due to its age and early development stage.
Install
-
npm install fmin -
yarn add fmin -
pnpm add fmin
Imports
- fmin (module object)
import fmin from 'fmin';
const fmin = require('fmin'); - nelderMead
import { nelderMead } from 'fmin';const fmin = require('fmin'); const solution = fmin.nelderMead(lossFunction, initialPoint); - conjugateGradient
import { conjugateGradient } from 'fmin';const fmin = require('fmin'); const solution = fmin.conjugateGradient(lossAndGradientFunction, initialPoint);
Quickstart
function loss(X) {
var x = X[0], y = X[1];
return Math.sin(y) * x + Math.sin(x) * y + x * x + y * y;
}
// Assuming a CommonJS environment or bundled application
const fmin = require('fmin');
const initialGuess = [-3.5, 3.5];
const solution = fmin.nelderMead(loss, initialGuess);
console.log("Minimum found at:", solution.x);
console.log("Value at minimum:", solution.fx);
console.log("Iterations:", solution.iterations);