Error installing Monk with NodeJS can't run app

297 Views Asked by At

I'm trying to set up an mongodb connection in my NodeJS app, but I can't run it when requiring monk...

my code:

...
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/app');

...

// Make our db accessible to our router
app.use(function(req,res,next){
    req.db = db;
    next();
});

app.use('/', routes);

Installed both mongodb and monk in the command prompt, in the projects root folder, with npm install --save mongodb and npm install --save monk

But when I try to run my project, I get

C:\Users\asilva\Documents\RSRacingUFRGSsite>node app C:\Users\asilva\Documents\RSRacingUFRGSsite\node_modules\monk\lib\applyMiddlewares.js:24
      monkInstance,
                  ^ SyntaxError: Unexpected token ,
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Users\asilva\Documents\RSRacingUFRGSsite\node_modules\monk\lib\collection.js:1:86)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

I checked the applyMiddlewares.js, see nothing wrong with it.. :

module.exports = function applyMiddleware (middlewares) {
  return function (monkInstance, collection) {
    var chain = []

    var middlewareAPI = {
      monkInstance,
      collection
    }
    chain = middlewares.map(function (middleware) {
      return middleware(middlewareAPI)
    })
    return compose(chain)
  }
}
1

There are 1 best solutions below

1
On

The issue is likely that you're using a Node version that doesn't support ES2015's Object property shorthand.

To fix it, either upgrade Node to v4.8.4 or above or create a pull request for the monk library changing the assignment as such:

module.exports = function applyMiddleware (middlewares) {
  return function (monkInstance, collection) {
    var chain = []

    var middlewareAPI = {
      monkInstance: monkInstance,
      collection: collection
    }

    chain = middlewares.map(function (middleware) {
      return middleware(middlewareAPI)
    })
    return compose(chain)
  }
}

I recommend upgrading to the latest version (v8.4.0 at the time of writing) so that you get access to the nice async/await and promise features, and the many bug fixes & security updates since.