node-persist icon indicating copy to clipboard operation
node-persist copied to clipboard

const storage = require('../node_modules/node-persist'); causes NodeJS to throw error

Open PeterKW opened this issue 4 years ago • 6 comments

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?

image

image

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}); });

PeterKW avatar Feb 14 '21 01:02 PeterKW

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?

akhoury avatar Feb 16 '21 17:02 akhoury

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?

image

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?

PeterKW avatar Feb 16 '21 20:02 PeterKW

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');

akhoury avatar Feb 17 '21 15:02 akhoury

because your code includes both, the first one being commented out

//const storage = require('../node_modules/node-persist');
const storage = require('node-persist');

akhoury avatar Feb 17 '21 15:02 akhoury

Sorry, looks like my reply didn't send.

I tried both of them incase either might work. Neither seemed to make much difference unfortunately

PeterKW avatar Feb 19 '21 01:02 PeterKW

hmm, sorry I am not sure I can help without a test case where I can reproduce it.

akhoury avatar Feb 23 '21 08:02 akhoury