使用module.exports(Node.js)时出现意外的标识符

我有我正在使用以下模块连接到我的数据库:

module.exports = { var sequelize = new Sequelize('db', 'rt', 'pw', { host: "localhost", port: 3306, socketPath: '/var/run/mysqld/mysqld.sock', dialect: 'mysql' }) } 

然后在主文件中,

 var configDB = require('./config/database.js'); 

但不幸的是,这将返回以下错误:

 /config/database.js:3 var sequelize = new Sequelize('db', 'rt', 'pw', { ^^^^^^^^^ SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:69:16) at Module._compile (module.js:432:25) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:349:32) at Function.Module._load (module.js:305:12) at Module.require (module.js:357:17) at require (module.js:373:17) at Object.<anonymous> (/server.js:14:16) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) 

我是否正确使用exportsfunction? 导出模块中的每个对象都会发生此错误。

编辑:以下返回cannot call method .authenticate of undefined ,即使模块似乎导出没有错误。

 configDB.sequelize // connect to our database .authenticate() .complete(function(err) { if (!!err) { console.log('Unable to connect to the database:', err) } else { console.log('Connection has been established successfully.') } }) 

你在对象文字中使用了不正确的语法。 我不确定你想要完成什么(具体地说,你打算如何在你的主文件中使用configDB ),但是你已经有了一些奇怪的对象字面语法和函数语法的混合。 也许你想要的东西如下所示:

 var sequelize = new Sequelize('db', 'rt', 'pw', { host: "localhost", port: 3306, socketPath: '/var/run/mysqld/mysqld.sock', dialect: 'mysql' }); module.exports = sequelize; 

编辑:你误解了一些关于资源如何存储和传递在JavaScript中的基本事情,考虑到你目前的结构,在我看来,你需要用configDBreplacedatabase.sequelize

 var configDB = require('./config/database.js'); configDB .authenticate() .complete(function(err) { if (!!err) { console.log('Unable to connect to the database:', err) } else { console.log('Connection has been established successfully.') } })