Skip to main content

@napacitor/preferences

Key-value persistent storage plugin (corresponds to @capacitor/preferences).

Installation​

To use npm:

npm install @napacitor/preferences

To use yarn:

yarn add @napacitor/preferences

Sync native files:

npx nap sync

Usage​

import { Preferences } from '@napacitor/preferences';

// Optional: configure a storage group (subsequent reads/writes/clears target this group; omitted = default storage)
await Preferences.configure({ group: 'session' });

await Preferences.set({ key: 'name', value: 'Max' });
const { value } = await Preferences.get({ key: 'name' }); // 'Max'
await Preferences.remove({ key: 'name' });
await Preferences.clear();

// Capacitor 2 Storage compatibility (Napacitor has no migration source: migrate returns empty results, removeOld is a no-op)
const { migrated, existing } = await Preferences.migrate(); // { migrated: [], existing: [] }
await Preferences.removeOld();

How It Works​

Wraps the APIs described in the .NET MAUI Preferences documentation: Preferences.Default.Get / Set / Remove / Clear (iOS: UserDefaults / Android: SharedPreferences / Windows: ApplicationData). configure({group}) maps to MAUI's sharedName (the Android counterpart is a suite; cross-app sharing has native semantics only on Android) and applies to all subsequent reads/writes/clears; on web it maps to a localStorage key prefix (default napacitor.). MAUI Preferences does not provide key enumeration, so this plugin does not provide keys() (maintain a key list yourself if needed).

Permissions​

Android​

No additional configuration required.

iOS​

No additional configuration required.

Windows​

No additional configuration required.

API​

configure(options)​

Configures the storage group at runtime (subsequent reads/writes/clears target this group).

ParamType
optionsConfigureOptions

Returns: Promise<void>

get(options)​

Reads a value by key.

ParamType
optionsGetOptions

Returns: Promise<GetResult>

set(options)​

Writes a value by key.

ParamType
optionsSetOptions

Returns: Promise<void>

remove(options)​

Removes a key.

ParamType
optionsRemoveOptions

Returns: Promise<void>

clear()​

Clears the current storage group (note: maps directly to Preferences.Clear(sharedName), clearing the whole storage group including data not written by this plugin).

Returns: Promise<void>

migrate()​

Migrates from Capacitor 2 Storage. Napacitor has no migration source, so it always returns empty results.

Returns: Promise<MigrateResult>

removeOld()​

Removes old Capacitor 2 Storage data (Napacitor has no migration source, no-op).

Returns: Promise<void>

Interfaces​

ConfigureOptions​

PropTypeDescription
groupstringStorage group name (maps to MAUI Preferences' sharedName); default storage when omitted.

GetOptions​

PropTypeDescription
keystringKey to read (must not be empty, otherwise INVALID_ARGUMENTS is thrown).

GetResult​

PropTypeDescription
valuestring | nullValue of the key; null when never set or already removed.

SetOptions​

PropTypeDescription
keystringKey to write (must not be empty, otherwise INVALID_ARGUMENTS is thrown).
valuestringValue to write (strings only; JSON.stringify complex objects yourself).

RemoveOptions​

PropTypeDescription
keystringKey to remove (must not be empty, otherwise INVALID_ARGUMENTS is thrown).

MigrateResult​

PropTypeDescription
migratedstring[]Migrated keys. Napacitor has no Capacitor 2 Storage migration source, always empty.
existingstring[]Keys that already existed (same as above, always empty).