I am attempting to implement the lowdb NPM package for use with node.js, but I am running into permissions errors. I have tried all of the examples listed in the readme located at https://github.com/typicode/lowdb/tree/master/examples, but all are returning the same error.
For ease, here is the specific code I tried running from the node CLI:
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
db.defaults({ posts: [] })
.write()
const result = db.get('posts')
.push({ name: process.argv[2] })
.write()
console.log(result)
I am executing with this command:
$ node cli.js hello
This should be the result:
# [ { title: 'hello' } ]
Instead I receive this error:
[server]$ node cli.js hello
fs.js:549
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: EACCES: permission denied, open 'db.json'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at fs.writeFileSync (fs.js:1156:15)
at FileSync.read (/home/nodeadmin/datadir/estimup/node_modules/lowdb/adapters/FileSync.js:46:9)
at LodashWrapper.db.read (/home/nodeadmin/datadir/estimup/node_modules/lowdb/lib/main.js:32:21)
at module.exports (/home/nodeadmin/datadir/estimup/node_modules/lowdb/lib/main.js:51:13)
at Object.<anonymous> (/home/nodeadmin/datadir/estimup/cli.js:5:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
I have attempted creating the db.json
file manually in advance and gave it chmod 644
permissions, still to no avail.