node-persist
node-persist copied to clipboard
const storage = require('../node_modules/node-persist'); causes NodeJS to throw error
Any idea why it couldn't find node-persist and breaks unless I comment out requiring node-persist? Or are there any debugging steps I could take next?
const express = require('express'); const path = require('path'); const randomId = require('random-id'); //const storage = require('../node_modules/node-persist'); const storage = require('node-persist'); const app = express(), bodyParser = require("body-parser"); port = 3080;
// place holder for the data const forms = []; //https://github.com/simonlast/node-persist/blob/master/examples/counter/counter.js const forms2 = [];
app.use(bodyParser.json()); app.use(express.static(process.cwd()+ '/my-app/build'));
app.get('/api/forms', (req, res) => { console.log('api/forms called!!!!!!!'); /* if (forms == []){ (async () => { await storage.init({logging: true, ttl: ttl}); forms2 = await storage.getItem('forms'); if (! forms2) { await storage.setItem('forms', forms); } forms = await storage.getItem('forms'); console.log('forms is ' + forms); })(); } */ res.json(forms); });
app.post('/api/form', (req, res) => { const form = req.body.form; form.id = randomId(10); console.log('Adding use:::::::::::', form); forms.push(form); res.json("form addedd"); });
app.get('/', (req,res) => { res.sendFile(process.cwd()+ '/my-app/build/index.html'); });
app.listen(port, () => {
console.log(Server listening on the port::${port}
);
});
honestly it looks like you haven't installed it?
npm install node-persist
or
yarn add node-persist
or maybe you've done npm install
but it's not in your package.json?
Thanks @akhoury
In the first screenshot node-persist folder can be seen in the node_modules folder so I'm presuming that means node-persist should be installed?
honestly it looks like you haven't installed it?
npm install node-persist
or
yarn add node-persist
or maybe you've done
npm install
but it's not in your package.json?
so let get this straight, when you do this, this causes an error
const storage = require('../node_modules/node-persist');
but this doesn't?
const storage = require('node-persist');
because your code includes both, the first one being commented out
//const storage = require('../node_modules/node-persist');
const storage = require('node-persist');
Sorry, looks like my reply didn't send.
I tried both of them incase either might work. Neither seemed to make much difference unfortunately
hmm, sorry I am not sure I can help without a test case where I can reproduce it.