Shawn
Shawn

Reputation: 5260

node.js require results undefined

I am new to node.js. I have the following code

module aws.js
  ....
  const awssvc = { dynamoQuery }
  module.exports = { awssvc }

module A.js
  const { awssvc } = require( ./index )
  ....
  module.export = { a }

module B.js
  const { awssvc } = require( ./index )
  ....
  module.export = { b }

index.js
const { awssvc } = require('./aws');
const { a } = require('./A');
const { b} = require('./B');

module.exports = { awssvc, a, b} 

In A.js when executing awssvc.dynamoQuery, I got TypeError: Cannot read property 'dynamoQuery' of undefined.

What have I missed?

Or what should or should not go into index.js?

Upvotes: 1

Views: 250

Answers (1)

Lawrence
Lawrence

Reputation: 347

Make sure you have equals signs for the deconstruction assignments

const { awssvc } require( ./index ) should be const { awssvc } = require( ./index )

Also, change module.export { a } to module.exports = { a }

It also looks like you're trying to import awssvc from the index.js file, it should be requiring from aws.js, So in module A.js const { awssvc } = require(./index) should be const { awssvc } = require ('./aws.js')

Upvotes: 1

Related Questions