Registry / serialization / option

option

JSON →
library2.1.0jsnpmunverified

This package provides a robust implementation of the option (or maybe) type for JavaScript, designed to manage optional values and mitigate the pervasive issues of `null` and `undefined`. It offers a functional approach to handling the presence or absence of a value through `some(value)` and `none` constructs, similar to types found in languages like Rust or Scala. The current stable version is `0.2.4`, which was published approximately nine years ago, indicating that the package is no longer actively maintained. Key differentiators include its rich API for value manipulation, such as `map`, `flatMap`, `filter`, `orElse`, and `valueOrElse`, which promote safer and more expressive code compared to traditional null checks. Its `fromNullable` function also provides a convenient way to convert potentially nullish values into `option` types.

npm install option
INSTALL
IMPORT
SIG · OPTION
O
option
serializationjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

option
const option = require('option');
import option from 'option';
The package is CommonJS-only. There is no official ESM support.
option.some
const someValue = option.some('hello');
import { some } from 'option';
Some is a property of the default export, not a named export. Requires CommonJS.
option.none
const noValue = option.none;
import { none } from 'option';
None is a property of the default export, not a named export. Requires CommonJS.
option.fromNullable
const maybeValue = option.fromNullable(someNullableVar);
A utility function to convert null or undefined to option.none.

Demonstrates creating 'some' and 'none' options, performing checks, safely unwrapping values with `valueOrElse`, and using `fromNullable`.

const option = require("option"); // Creating Option instances const someUser = option.some({ id: 1, name: () => "Alice" }); const noUser = option.none; // Basic checks console.log(`someUser isSome: ${someUser.isSome()}`); // true console.log(`noUser isNone: ${noUser.isNone()}`); // true // Retrieving values safely function greet(userOption) { return userOption.map(user => user.name()) .valueOrElse("Anonymous"); } console.log(`Greeting someUser: Hello ${greet(someUser)}`); // Hello Alice console.log(`Greeting noUser: Hello ${greet(noUser)}`); // Hello Anonymous // Using fromNullable const nullableString = Math.random() > 0.5 ? "A string" : null; const safeOption = option.fromNullable(nullableString); console.log(`From nullable: ${safeOption.valueOrElse("No string provided")}`);
Debug
Known issues
abandonedThe `option` package (mwilliamson/node-options) has not been updated in approximately nine years (last published version 0.2.4). It is considered abandoned and may have unaddressed bugs or security vulnerabilities.
fix
Consider migrating to a more actively maintained option/maybe type library in the JavaScript ecosystem, such as 'fp-ts', 'neverthrow', or similar alternatives that provide better modern JavaScript/TypeScript support and community backing.
affects: <=0.2.4
gotchaAttempting to call `.value()` directly on an `option.none` instance will throw a runtime error. This design choice forces explicit handling of the 'none' case, but can lead to unhandled exceptions if not properly anticipated.
fix
Always use safer methods like `.valueOrElse(defaultValue)`, `.map()`, `.flatMap()`, or `.orElse()` to handle the absence of a value. Wrap direct `.value()` calls in `try...catch` blocks if absolutely necessary, but prefer functional approaches.
affects: >=0.1.0
gotchaThis package is written in CommonJS and does not provide native TypeScript typings or explicit ESM (ECMAScript Modules) support. Integrating it into modern TypeScript or ESM-first projects may require manual type declarations or CJS interoperability layers.
fix
For TypeScript projects, you may need to create a `d.ts` declaration file (e.g., `declare module 'option';`). For ESM, it can be imported using `import * as option from 'option';` in Node.js, but native ESM support is not guaranteed.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: Cannot read properties of null (reading 'value') OR TypeError: Cannot read property 'value' of undefined
Attempting to access `.value()` on an `option.none` instance after it was converted implicitly or explicitly from `null` or `undefined`.
fix
Refactor code to use safe unwrapping methods like `option.valueOrElse(defaultValue)` or `option.map(func).valueOrElse(defaultValue)` instead of direct `.value()` calls on potentially empty options.
Error: value of none()
Directly calling `.value()` on an `option.none` instance, which is explicitly designed to throw an error in this scenario.
fix
Use methods that handle the 'none' case gracefully, such as `.map()`, `.flatMap()`, or `.valueOrElse(defaultValue)` to provide a fallback value or transform the option without directly unwrapping it when it might be empty.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
option — npm install option · libregistry