lmaccherone / node-localstorage

A drop-in substitute for the browser native localStorage API that runs on node.js.
MIT License
450 stars 36 forks source link

new LocalStorage(<base_path/desired>) fails when base_path does not exist. #51

Closed WillBartee closed 3 years ago

WillBartee commented 4 years ago

As of v2.1.5, the LocalStorage.init will attempt to create a directory for the location provided, however it will fail if there are multiple directories in the path that do not exist.

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const { LocalStorage } = require('node-localstorage');

const non_existent_directory = path.resolve('./does_not_exist');
const non_existent_subdirectory = path.resolve(path.join(non_existent_directory, 'desired_location'));

const storage = new LocalStorage(non_existent_subdirectory);

Throws the error:

 Error: ENOENT: no such file or directory, mkdir 'C:\Workspaces\my-app\does_not_exist\desired_location'
    at Object.mkdirSync (fs.js:753:3)
    at LocalStorage._init (C:\Workspaces\my-app\node_modules\node-localstorage\LocalStorage.js:183:14)
    at new LocalStorage (C:\Workspaces\my-app\node_modules\node-localstorage\LocalStorage.js:124:12)
    at Object.<anonymous> (C:\Workspaces\my-app\main.js:16:19)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

As of Node.js v10.12:

  • fs Added a recursive option to fs.mkdir and fs.mkdirSync. If this option is set to true, non-existing parent folders will be automatically created. #21875
lmaccherone commented 3 years ago

This is still open because I haven't pushed an update to npm. I'm trying to catch up now and should be able to do that within a few days.