''The "path" argument must be of type string. Received null' in electron-json-storage
I'm using eletron-json-storage like this:
settings.js:
const storage = require('electron-json-storage');
const defaultStoragePath = storage.getDefaultDataPath();
// Value: C:\Users\10467\AppData\Roaming\maplateditor\storage
...
defaultStorage() {
console.log("Check defaultStorage value");
console.log(defaultStoragePath);
// C:\Users\10467\AppData\Roaming\maplateditor\storage
storage.setDataPath(defaultStoragePath);
console.log(storage.getDataPath());
// C:\Users\10467\AppData\Roaming\maplateditor\storage
return storage;
}
...
this.defaultStorage().get(...)
And I checked every times variable defaultStoragePath
was sure to set.
But electron-json-storage causes error:
(anonymous) @ VM75 renderer_init.js:93
VM75 renderer_init.js:93 TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null
at validateString (VM19 validators.js:124)
at Object.resolve (VM28 path.js:139)
at mkdirP (VM113 F:\github\MaplatEditor\node_modules\mkdirp\index.js:25)
at VM91 F:\github\MaplatEditor\node_modules\electron-json-storage\lib\storage.js:527
at nextTask (VM93 F:\github\MaplatEditor\node_modules\async\dist\async.js:5327)
at next (VM93 F:\github\MaplatEditor\node_modules\async\dist\async.js:5334)
at VM93 F:\github\MaplatEditor\node_modules\async\dist\async.js:972
at VM91 F:\github\MaplatEditor\node_modules\electron-json-storage\lib\storage.js:524
at nextTask (VM93 F:\github\MaplatEditor\node_modules\async\dist\async.js:5327)
at Object.waterfall (VM93 F:\github\MaplatEditor\node_modules\async\dist\async.js:5337)
Is this a bug?
How can I avoid this?
Environments are:
node.js: 16.13.1
electron: 13.6.6
electron-json-storage: 4.5.0
===
Additional info:
This error not occurred with electron: 11.5.0
The difference of my code between electron: 11.5.0 and electron: 13.6.6 is:
On electron 11.5.0: Call settings.js code by:
const settings = require('electron').remote.require('./settings').init();
On electron 13.6.6: Call settings.js code through preload.js
:
window.settingsBackend = require('./settings');
Maybe this difference causes different results..
Solution 1:[1]
Finally I found the reason...
I call "electron-json-storage" inside of "preload.js".
I don't know "preload.js" is worked on renderer process.
I must use electron-json-storage on main process.
I realized that under the contextIsolation environment, I need to completely rethink my architecture from the nodeIntegration era configuration.
====
I created example for using electron-json-storage under contextIsolation environment for people who will have similar question in future: https://gist.github.com/kochizufan/a467c6b76390c6f1c41260614cb06a5c
This works well.
NOTE: Error handling or avoiding multiple registration of api are totally omitted in this example.
Any points for improvement are welcome.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |