node-persist
node-persist copied to clipboard
error: Unhandled Rejection at: Promise[object Promise] reason:Error: EISDIR: illegal operation on a directory, read
In case there is a folder in the same directory as the cache file, then this folder is accessed as a file which lead to the ugly error above. Easy workaround: put all cache files into their own folder.
This happens when the cleanup function is triggered.
readFile (node_modules\node-persist\src\local-storage.js:302) fs.readdir (node_modules\node-persist\src\local-storage.js:286) [ async function ] (Unknown Source:undefined) fs.readdir (node_modules\node-persist\src\local-storage.js:278) (anonymous function) (fs.js:135) [ FSREQWRAP ] (Unknown Source:undefined) init (inspector_async_hook.js:22) emitInitNative (async_hooks.js:131) fs.readdir (fs.js:897) fs.exists (node_modules\node-persist\src\local-storage.js:278) cb (fs.js:312) [ FSREQWRAP ] (Unknown Source:undefined) init (inspector_async_hook.js:22) emitInitNative (async_hooks.js:131) fs.exists (fs.js:310) Promise (node_modules\node-persist\src\local-storage.js:275) readDirectory (node_modules\node-persist\src\local-storage.js:273) data (node_modules\node-persist\src\local-storage.js:116) keys (node_modules\node-persist\src\local-storage.js:120) removeExpiredItems (node_modules\node-persist\src\local-storage.js:238)
Thanks for posting. I did not realize that adding a directory within my "/.node-persist/storage" would cause this error. It took me a while to realize it was coming from node-persist. Removing the extra folder I created within the storage directory seems to fix the issue.
ok you can use forgiveParseErrors: true
in the init options to ignore unrecognized files and directories in your storage dir
in some cases, you (or some other service) might add non-valid storage files to your storage dir, i.e. Google Drive, make this true if you'd like to ignore these files and not throw an error
https://github.com/simonlast/node-persist#async-initoptions-callback
let me know if that doesn't work
I recently ran into the same problem in my project with a subfolder causing problems. Also, any other file in the cache directory other than one created by node-persist will also cause a similar unhandled exception. My biggest concern isn't about the unhandled exceptions, but the problem is that none of the cached files are being removed.
To provide a little more detail:
const NodePersist = require('node-persist');
process.on('unhandledRejection', (err, p) => {
console.log('Unhandled Rejection: %s', err.message);
if (p) {
p.catch((pErr) => console.log(`Promise Exception: ${pErr}`));
}
console.log(err.stack);
});
const cacheOptions = {
dir: path.normalize('./.cache/folder1'),
ttl: 86400 * 1000, // one day
expiredInterval: 1*60*1000 // cleanup expired cache every minute
};
const myCache = NodePersist.create(cacheOptions);
myCache.init();
If .cache/folder1 only contains files, then things work as expected. However, if a subfolder is created inside folder1 (./.cache/folder1/folder2), then during the clean up phase (expiredInterval), the unhandledRejection is triggered and no expired entries are removed from the .cache/folder1 directory.
Console Output:
Unhandled Rejection: EISDIR: illegal operation on a directory, read
Promise Exception: Error: EISDIR: illegal operation on a directory, read
Also, if there is another file in the cache location (echo 'Hello World > ./.cache/folder1/file) that was not created by node-persist, then an unhandled exception is also triggered and expired entries are not removed.
Console Output:
Unhandled Rejection: [node-persist][readFile] C:\Projects\BlackFishDashboard_Review\back\.cache\KernelRegression\New Text Document does not look like a valid storage file!
Promise Exception: Error: [node-persist][readFile] C:\Projects\BlackFishDashboard_Review\back\.cache\KernelRegression\New Text Document does not look like a valid storage file!
IMHO, I think that directories should always be skipped and that only files in the specified directory should be processed (since these are the only files that it should have been created by a node-persist instance). Invalid storage files could throw an error if desired, but the promise rejection should be caught within the node-persist module. The forgiveParseErrors: true
option could be used to suppress those error prints if the user desires. In all cases, the expired files found should still be removed.
I ran into this issue as well and deleting the folder from .node-persist/storage
fixed it. Thanks for documenting this.