Redux-persist-node-storage is an adapter for the Redux Persist library, enabling state persistence in Node.js environments. It achieves this by implementing Redux Persist's required storage interface (`setItem`, `getItem`, `removeItem`, `getAllKeys`) using `node-localstorage`. The package provides a `localStorage`-like API for Node.js, making `redux-persist` usable in server-side applications, Electron apps, or other Node.js contexts where browser `localStorage` is unavailable. Data is stored on disk in a user-specified directory. The current stable version is 2.0.0, released in December 2017. Due to its age and lack of recent updates, its release cadence is effectively non-existent, and it primarily serves a niche requirement for Node.js-specific Redux state persistence.
npm install redux-persist-node-storageVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `redux-persist-node-storage` with `redux-persist` in a Node.js environment. It defines a simple Redux store with a user reducer, configures the `AsyncNodeStorage` to save to a local directory, and initializes the persisted store, logging the rehydrated state. It also shows basic interaction and a purge option.
Migrate your `redux-persist` configuration to use `persistReducer` and `persistStore` with a `persistConfig` object that explicitly includes the `storage` property. Remove `autoRehydrate` from your store enhancers.
Monitor disk usage for your configured storage directory. If large amounts of data need to be persisted, consider increasing the quota for `node-localstorage` during instantiation or using a different storage solution. Implement `try...catch` blocks around persistence operations to handle `QuotaExceededError` gracefully.
Ensure that the directory provided to `AsyncNodeStorage` (e.g., `/tmp/storageDir`) exists and that the user running the Node.js process has sufficient read/write permissions for that directory. Use `path.join(process.cwd(), '...')` for reliable path resolution.
For very large or complex state objects, consider selective persistence using `whitelist` or `blacklist` in `redux-persist` config, or utilizing `redux-persist` transforms to store only necessary data. Avoid storing excessively large objects directly.
Ensure the directory path passed to `new AsyncNodeStorage('/path/to/dir')` is an absolute, valid, and writable path. Use `path.join(process.cwd(), './your-storage-dir')` for robust path handling.Ensure your `persistConfig` object explicitly includes `storage: new AsyncNodeStorage('/path/to/dir')` (or `storage: new NodeStorage('/path/to/dir')` for synchronous). You must instantiate the storage adapter.Verify that your root reducer is wrapped by `persistReducer(persistConfig, rootReducer)`, and that `persistStore(store)` is called. Also, ensure the `key` in `persistConfig` is unique and the `storage` adapter is correctly instantiated and passed. Check for any `persistor.purge()` calls being inadvertently triggered.
If your Redux state shape has changed between application versions, consider using a `stateReconciler` like `autoMergeLevel2` or implementing Redux Persist Migrations to handle schema changes gracefully. Purging the old persisted state (`persistor.purge()`) can also resolve immediate rehydration issues, but users will lose their previous state.