How to import a module that is in a different folder in Node.js?

Cordial greetings, I hope you are well.

I would like some guidance on how I can import a module that is in another folder.

Here is an image:
module_import

However when importing the module it generates me error:

Error: Cannot find module '../controllers/index.js'
Require stack:

I would be very grateful for your guidance.

40 thoughts on “How to import a module that is in a different folder in Node.js?”

  1. You need to understand, how path works. You are missing one more directory skip in your path.

    const module = require('../../controllers/index.js'); //as you are in route module, you need to go back to controller directory(so ../../path of file)
    

    For more details on path, you can check here.

    Reply
  2. Does your site have a contact page? I’m having a
    tough time locating it but, I’d like to send you an e-mail.

    I’ve got some ideas for your blog you might
    be interested in hearing. Either way, great site and
    I look forward to seeing it improve over time.

    Here is my page; gema ace333

    Reply
  3. First of all I want to say wonderful blog! I had a quick question in which I’d like to ask if you
    don’t mind. I was curious to find out how you
    center yourself and clear your thoughts prior to writing.
    I have had difficulty clearing my thoughts in getting my thoughts out.

    I do take pleasure in writing however it just seems like the first
    10 to 15 minutes are generally wasted just trying to figure out how to begin.
    Any suggestions or hints? Kudos!

    my web-site gem wukong333

    Reply
  4. Saya telah telah menjelajah online lebih dari tiga jam
    hari ini, namun saya tidak pernah menemukan artikel menarik seperti milik
    Anda. It is cukup berharga bagi saya. Dalam pandangan saya, jika semua pemilik web dan blogger membuat konten yang
    baik seperti yang Anda lakukan, net akan menjadi lebih berguna dari sebelumnya.

    Also visit my blog post – Tembak ikan deposit pulsa (gameaco.com)

    Reply
  5. I loved as much as you will receive carried out right here.

    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get got an impatience over that you wish
    be delivering the following. unwell unquestionably come further formerly
    again as exactly the same nearly very often inside case you shield this
    hike.

    Check out my website freeholmes.com

    Reply
  6. My brother suggested I might like this web site.

    He was totally right. This post actually made my day.
    You can not imagine simply how much time I had spent for this information!
    Thanks!

    Here is my web page – sygk100.cn

    Reply
  7. An impressive share! I have just forwarded this onto a colleague who
    was conducting a little homework on this. And he actually bought me dinner because I stumbled upon it for him…
    lol. So allow me to reword this…. Thanks for the meal!!

    But yeah, thanks for spending some time to talk about this subject here on your
    web page.

    Feel free to visit my homepage Latanya

    Reply

Leave a Comment