Home > OS >  require index results undefined
require index results undefined

Time:02-05

I have two folders next to each other as it follows ..

// Folder 1 
users
-services
-index
-otherThings

= services
exports.A = (a,b,c)=> { // do something }
exports.B = (a,b,c)=> { // do something }

= index.js
const services= require('./services');
const otherThings= require('./otherThings');
module.exports = { otherThings, services};


// Folder 2
middlewares
-is-auth
-index.js

= is-auth 
const { services } = require('../users');
// services here is undefined
// when i require the full index object it is an empty object
const indexObject = require('../users')
console.log(indexObject) // {}

the weird thing is when using services required above i am getting suggestions of the folder 1 services (A,B functions) but they are undefined !

CodePudding user response:

  1. console log services in folder 1 if it is coming there.
  2. check the hierarchy of your folder structures when importing and check the paths also,

other wise the code seems right.

edit 1:

  1. not sure what exactly services type are, but it seems only function A and B are there, if you just need those functions you can export them individually and try,

if that works then you can back track to, why services was not working in the first place,

  1. if not, then just try any random variable try importing if it doesn't work then it's may be related to hierarchy.

edit 2:

  • i think i got it, if my assumption is right users is folder and services is file, then you need the path as require(../users/services);

edit 3:

then check out this link require folder as a module

  • Related