javascriptnode.jsnodemonlokijs

requiring loki inMemory database in server.js causes nodemon to constantly restart


Project repo

After installed lokijs I created the following database.js file:

var loki = require('lokijs');

var db = new loki('db.json');

db.addCollection('top').insert([
    { term: 'JavaScript' , style : 'warning' },
    { term: 'Angular 2'  , style : 'danger'  },
    { term: 'NodeJS'     , style : 'success' },
    { term: 'REST'       , style : 'primary' }
]);

db.addCollection('searches');

db.saveDatabase();

As soon as I added require('./database.js'); in my server.js file, nodemon is caught in a restart loop. nodemon main

var express = require('express');

var app = express();

app.set('view engine', 'ejs');
app.set('views', __dirname + '../public/views');

app.use(express.static(__dirname + '/../public'));

require('./database.js');
require('./routes.js')(app);

module.exports = app;

enter image description here


Solution

  • You can add all the json files of lokijs in ignore array of nodemon.json like below

    {
        "ignore": [
            "db1.json",
            "db2.json"
        ]
    }
    

    Now nodemon will not restart when you use saveDatabase function of lokijs