The object from my Node module does not have the behavior I expect - Node.js


I am a newbie with Node and I am trying to learn how to make my modules work. Here is my express app:

var express = require('express');
var app = express();

var client = require('./config/postgres.js');

app.get('/users', function(req, res) {
  var query = client.query('SELECT * FROM users');
  query.on('row', function(row, result) {
  query.on('end', function(result) {

And here is my module that I am trying to require

var pg = require('pg').native;
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/isx';
var client = new pg.Client(connectionString);
module.exports.client = client;

but in my main app, my client has no method 'query'. If I just inline the code that is in the module and I am requireing in, then everything works fine. What is the difference between the 2 ways to access the code?

var client = require('./config/postgres.js');

...sets client equal to the export object in your import. Since the export object has a single client property with the query function,


is what you're looking for.

If you want to export just the client, use;

module.exports = client;